Spring boot OAuth2 получает исключение при доступе при попытке получить ресурсы с помощью токена доступа - PullRequest
1 голос
/ 29 апреля 2020

Я пытаюсь реализовать приложение весенней загрузки oauth2. Я получил токен доступа и обновил токен sh, используя / token API. Но когда я пытаюсь получить доступ к своим остальным API, используя токен доступа в качестве токена носителя, я получаю сообщение об ошибке отказа в доступе.

Вот мой код

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    private MyUserDetailsService myUserDetailsService;

    @Autowired 
    private DataSource dataSource;

    @Autowired
    private OAuth2ClientDetailsService oAuth2ClientDetailsService;

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

    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(oAuth2ClientDetailsService);
    }

    @Override
      public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager).accessTokenConverter(defaultAccessTokenConverter())
          .userDetailsService(myUserDetailsService);
      }

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

      @Bean
      public JwtAccessTokenConverter defaultAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
      }
}






@Configuration
    @EnableResourceServer
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
        private static final String RESOURCE_ID = "resource-server-rest-api";
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) {
            resources.resourceId(RESOURCE_ID);
        }
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/**").authorizeRequests().anyRequest().authenticated();
        }
    }


    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(securedEnabled = true)
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Autowired
        private DataSource dataSource;

        @Autowired 
        private MyUserDetailsService myUserDetailsService;

        /*@Bean
        public TokenStore tokenStore() {
            return new JdbcTokenStore(dataSource);
        }*/

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

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
              .and().authorizeRequests().antMatchers("/oauth/token")
              .permitAll().anyRequest().authenticated();
        }

        /*@Autowired
          public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(authenticationProvider());
          }

        @Bean
          public DaoAuthenticationProvider authenticationProvider() {
            DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
            provider.setUserDetailsService(myUserDetailsService);
            return provider;
          }*/

        @Override
        @Bean
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/device_registration");
        }

        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(myUserDetailsService);
        }

        /*@Autowired // here is configuration related to spring boot basic authentication
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication() // static users
                    .withUser("dummy_user").password("password").roles("USER").and().withUser("dummy_user1")
                    .password("password").roles("USER");
        }*/
    }

Я использую oauth2

<dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.5.RELenter code hereEASE</version>
        </dependency>** 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...