Unable to access resources with access_token : spring boot Oauth2

后端 未结 5 1160
南方客
南方客 2020-12-19 04:00

I am trying to implement Oauth2 in my existing application.Initially I have added spring security and then tried to add oauth2, After adding configuration I am able to gener

5条回答
  •  醉酒成梦
    2020-12-19 04:29

    You should use hasRole directly on your antmatcher instead of a string inside the access() function. This will evaluate the hasRole correctly and correctly determine that the user has access to the requested resource.

    This will result in the following code for ResourceServer.java:

    @Configuration
    @EnableResourceServer
    public class ResourceServer extends ResourceServerConfigurerAdapter {
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.anonymous().disable()
                    .requestMatchers().antMatchers("/patients/**").and().authorizeRequests()
                    .antMatchers("/patient/**").hasRole('USER')
                    .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
        }
    
    }
    

提交回复
热议问题