gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:36:59 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com