【问题标题】:Spring Boot OAuth2春季启动 OAuth2
【发布时间】:2017-06-24 22:48:11
【问题描述】:

我正在努力让我的资源服务器与我的授权服务器通信。

这是我的授权服务器的配置。

@Configuration
@EnableAuthorizationServer
public class AuthorisationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("app1").secret("password").authorizedGrantTypes("authorization_code", "refresh_token", "password", "client_credentials")
                .scopes("openid");
    }

}

这是我对资源服务器的配置:

@SpringBootApplication
@EnableResourceServer
@RestController
public class ResourceServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ResourceServerApplication.class, args);
    }

    @RequestMapping("/hello")
    public String home() {
        return "Hello World!";
    }
}

这是资源服务器的 application.yml:

security:
  oauth2:
    client:
      id: app1
      client-secret: password
      access-token-uri: http://localhost:9999/oauth/token
      user-authorization-uri: http://localhost:9999/oauth/authorize

我可以使用以下方式从我的 AS 请求令牌:

$ curl app1:password@localhost:9999/uaa/oauth/token -d grant_type=client_credentials
{"access_token":"5e74b3fd-41d2-48a7-a21c-31edf52bcb63","token_type":"bearer","expires_in":43199,"scope":"openid"}

然后我使用令牌通过以下方式访问我的资源 API

$ curl -H "Authorization: Bearer 5e74b3fd-41d2-48a7-a21c-31edf52bcb63" localhost:8080/hello
{"error":"invalid_token","error_description":"Invalid access token: 5e74b3fd-41d2-48a7-a21c-31edf52bcb63"}

但是我得到无效的令牌并且在日志中没有任何指示,任何人都可以帮忙,我的感觉是资源服务器的配置错误。

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 spring-cloud


    【解决方案1】:

    你的资源服务器的 application.yml 应该包含这样的配置

    security:
      oauth2:
        resource:
          user-info-uri: http://localhost:9999/user
    

    这个用户端点应该像这样在 AS 中公开用户详细信息

    @RequestMapping(value = "/user",
            method = RequestMethod.GET)
    public Principal user(Principal user) {
        return user;
    }
    

    这取自spring security documentation

    更新:确保不使用 @EnableWebMvc。这在使用 Spring 安全性的应用程序的情况下不起作用,因为它是 filter based

    【讨论】:

    • 不需要用户信息 uri。我们正在使用包含用户详细信息的 JWT 令牌。
    猜你喜欢
    • 2016-01-28
    • 2018-08-01
    • 2017-09-11
    • 2015-04-18
    • 2015-08-22
    • 2015-09-18
    • 2015-03-20
    • 2018-01-24
    • 2016-08-22
    相关资源
    最近更新 更多