java - 使用 spring security 和 oauth2 刷新 token 调用失败,错误为 : UserDetailsService is required

标签 java spring spring-security spring-security-oauth2 spring-oauth2

我正在使用 Spring Security OAuth2 进行授权。尝试刷新 token 时出现错误:UserDetailsS​​ervice is required(有趣的是,我只在 unix 机器上出现此错误,而在 windows 上没有)。我正在使用 Spring OAuth2 版本 2.0.7。

由于某些原因,DefaultTokenService 中的 AuthenticationManager 不为空,它会尝试对用户进行身份验证以检查他是否仍然存在。我认为它被初始化是因为一些 spring security 与 spring oauth2 配置问题。

我没有使用任何自定义 UserDetailsS​​ervice,因此此时不应验证用户。但是,当我对其进行调试时,我发现它尝试使用 WebSecurityConfigurerAdapter 中的一个并出现此错误。即使我提供了我的自定义虚拟 UserDetailsS​​ervice,它也没有使用那个,而是尝试使用另一个为 null 的。我在这里错过了什么吗?我不知道为什么会这样?

这是我的 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();
    }
}

这是我的 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");
    }
}

最佳答案

授权服务器端点需要 UserDetailsS​​ervice。在您的 OAuth2Config 类中配置用户详细信息服务,如下所示:

@Autowired
private UserDetailsService userDetailsService;

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

你也可以在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
}

关于java - 使用 spring security 和 oauth2 刷新 token 调用失败,错误为 : UserDetailsService is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43842659/

相关文章:

Java:基本数学错误?

java - REST WS 的编码解码变音符号

security - 带有 session Cookie 的 Spring Security RememberMe 服务

java - 如何制作 forEach

java - 了解 Java JDBC 错误

java - 调用不是为您的类编写的 JNI 方法

java - Spring-websockets : Spring security authorization not working inside websockets

java - org.springframework.web.servlet.PageNotFound :1108

spring - JSP页面无法在spring boot embedded undertow上显示

java - 如何修复 java.io.InvalidClassException : org. springframework.security.core.context.SecurityContextImpl