【问题标题】:spring boot OAuth2 role based authorizationspring boot OAuth2 基于角色的授权
【发布时间】:2015-12-29 13:05:01
【问题描述】:

我们有一个扩展 AuthorizationServerConfigurerAdapter 的专用授权服务器,我们在其中设置了覆盖 void configure(ClientDetailsS​​erviceConfigurer clients) 方法的权限。

    @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)
    }

现在如何使用资源服务器中的权限进行基于角色的授权。 我们能够通过授权服务器生成的令牌进行身份验证。 需要帮助。

【问题讨论】:

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


    【解决方案1】:

    在资源服务器中,您应该扩展 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;
        }
    }
    

    【讨论】:

    • 我认为它适用于#oauth2.clientHasRole('ADMIN')。这对我有用
    【解决方案2】:

    您已收到来自身份验证服务器的令牌。您现在可以使用该令牌向身份验证服务器发出另一个请求以检索用户对象。此 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.....
    

    角色列表在UserDetailsS​​ervice.User中的org.springframework.security.core.userdetails中创建和设置如下。

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

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2016-05-13
      • 2015-04-21
      • 2020-09-13
      • 1970-01-01
      • 2016-07-01
      • 2022-01-01
      • 2020-06-16
      相关资源
      最近更新 更多