4

我們有一個專用的授權服務器來擴展AuthorizationServerConfigurerAdapter,我們在這裏設置了權限覆蓋void configure(ClientDetailsS​​erviceConfigurer clients)方法。spring啓動基於OAuth2角色的授權

@Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    @Value('${oauth.clientId}') 
    private String clientId 

    @Value('${oauth.secret:}') 
    private String secret 

    @Value('${oauth.resourceId}') 
    private String resourceId 

    @Autowired 
    @Qualifier('authenticationManagerBean') 
    private AuthenticationManager authenticationManager 

    @Bean 
    public JwtAccessTokenConverter accessTokenConverter() { 
     return new JwtAccessTokenConverter(); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
     oauthServer.checkTokenAccess("permitAll()") 
     oauthServer.allowFormAuthenticationForClients() 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authenticationManager(authenticationManager) 
       .accessTokenConverter(accessTokenConverter()) 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient(clientId) 
       .secret(secret) 
       .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
       .authorities("USER", "ADMIN") 
       .scopes("read", "write", "trust") 
       .resourceIds(resourceId) 
    } 

現在如何使用資源服務器中的權限來進行基於角色的授權。 我們可以通過授權服務器生成的令牌進行認證。 需要幫助。

回答

6

在資源服務器中,您應該擴展ResourceServerConfigurerAdapter以配置requestMatchers併爲每個資源設置角色。

@Configuration 
@EnableResourceServer 
public class OAuth2Config extends ResourceServerConfigurerAdapter { 

    @Value("${keys.public}") 
    private String publicKey; 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
       .requestMatchers() 
       .antMatchers("/**") 
       .and() 
       .authorizeRequests() 
       .antMatchers("/service1/**").access("#oauth2.hasScope('ADMIN')") 
       .antMatchers("/service2/**").access("#oauth2.hasScope('USER')"); 
    } 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.tokenStore(tokenStore()); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new JwtTokenStore(jwtAccessTokenConverter()); 
    } 

    @Bean 
    public JwtAccessTokenConverter jwtAccessTokenConverter() { 
     JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter(); 
     tokenConverter.setVerifierKey(publicKey); 
     return tokenConverter; 
    } 
} 
+0

我認爲它將與'#oauth2.clientHasRole('ADMIN')'一起使用。這對我有效 –

1

您已收到來自auth服務器的令牌。您現在可以使用該令牌向auth服務器發出另一個請求來檢索用戶對象。這個json對象將包含角色(權限)。 該請求如下所示。

curl -H "Authorization: Bearer 2a953581-e9c9-4278-b42e-8af925f49a99" 
    http://localhost:9999/uaa/user 

爲了做到這一點,您需要創建用戶服務端點並實現UserDetailsS​​ervice。

@RequestMapping("/user") 
public Principal user(Principal user) { 
    return user; 
} 
    @Bean 
    UserDetailsService userDetailsService..... 

創建的角色列表和org.springframework.security.core.userdetailsin的UserDetailsS​​ervice.User設置如下。

AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"));