使用Spring Security OAuth2刷新令牌调用失败,并出现以下错误:UserDetailsS​​ervice是必需的

Fil*_*nik 5 java spring spring-security spring-security-oauth2 spring-oauth2

我正在使用Spring Security OAuth2进行授权。尝试刷新令牌时,出现错误:(UserDetailsService is required有趣的是,我仅在UNIX计算机上而不是Windows上收到此错误)。我正在使用Spring OAuth2版本2.0.7。

由于某种原因,中的AuthenticationManagerin DefaultTokenService不为空,它尝试对用户进行身份验证以检查其是否仍然存在。我认为它由于一些春季安全性与春季oauth2配置问题而被初始化。

我没有使用任何自定义UserDetailsService,因此此时它不应该对用户进行身份验证。但是,当我调试它时,我看到它尝试使用中的一个WebSecurityConfigurerAdapter并得到此错误。即使我提供了自定义的虚拟对象UserDetailsService,它也没有使用该虚拟对象,而是尝试使用另一个虚拟对象,这是空的。我在这里想念什么吗?我不知道为什么会这样?

这是我的Oauth2配置

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private MySpringTokenStore tokenStore;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private MyClientDetailsServiceImpl clientDetailsService;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore);
        endpoints.authenticationManager(authenticationManager)
          .approvalStoreDisabled();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(clientDetailsService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients();
    }

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Spring安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
        .authorizeRequests()
            .antMatchers("/myRest/events/**", "/events/**", "/events", "/myRest/events").permitAll() 
            .antMatchers("/login.jsp", "/login").permitAll() 
        .and()
            .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")).disable()
            .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/myRest/events")).disable()
        .sessionManagement().sessionFixation().none();
        // @formatter:on
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/index*", "/myRest/events/**", "/events/**", "/myRest/events", "/events", "/swagger/**", "/kibana/**",
            "/elastic/**", "/version/**", "/api-docs/**", "/js/**", "/oauth/uncache_approvals", "/oauth/cache_approvals");
    }
}
Run Code Online (Sandbox Code Playgroud)

Vij*_*ana 9

授权服务器端点需求UserDetailsService。在您的OAuth2Config课程中,配置用户详细信息服务,如下所示:

@Autowired
private UserDetailsService userDetailsService;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(tokenStore);
    endpoints.userDetailsService(userDetailsService);
    endpoints.authenticationManager(authenticationManager)
      .approvalStoreDisabled();
}
Run Code Online (Sandbox Code Playgroud)

您也可以在中配置它WebSecurityConfigurerAdapter

@Autowired
private AuthorizationServerEndpointsConfiguration endpoints;

@Override
protected void configure(HttpSecurity http) throws Exception {

    if (!endpoints.getEndpointsConfigurer().isUserDetailsServiceOverride()) {
        UserDetailsService userDetailsService = http.getSharedObject(UserDetailsService.class);
        endpoints.getEndpointsConfigurer().userDetailsService(userDetailsService);
    }

    // @formatter:off
    http
    .authorizeRequests()
        .antMatchers("/myRest/events/**", "/events/**", "/events", "/myRest/events").permitAll() 
        .antMatchers("/login.jsp", "/login").permitAll() 
    .and()
        .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")).disable()
        .csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/myRest/events")).disable()
    .sessionManagement().sessionFixation().none();
    // @formatter:on
}
Run Code Online (Sandbox Code Playgroud)

  • 不,那不是真的。Oauth2配置不需要该服务,也可以留空。我也尝试提供该服务的实现,但它也引发了相同的异常。我已经解决了这个问题。问题在于配置的顺序(因为它们共享一些对象)。我已将 OAuth2 配置的顺序设置为(例如)100,将 Web Security 配置设置为 101。这样就可以了。 (2认同)