春季启动OAuth2

问题描述:

我很努力让我的资源服务器与我的授权服务器通话。春季启动OAuth2

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

@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:[email protected]: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"} 

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

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

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

该用户终端应该像这样

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

这是从spring security documentation

采取暴露用户的详细信息更新:确保不使用@EnableWebMvc。这对于使用Spring安全性的应用程序来说不起作用,因为它是filter based

+0

没有必要使用user-info-uri。我们正在使用包含用户详细信息的JWT令牌。 – Jack