2017-02-07 60 views
1

我很努力讓我的資源服務器與我的授權服務器通話。春季啓動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"} 

但是我得到無效的令牌,並沒有在日誌中表示,任何人都可以請大家幫忙,我的感覺是這是對資源服務器的錯誤配置。

回答

0

你的資源服務器的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