【发布时间】:2017-10-06 03:52:49
【问题描述】:
我正在使用 Spring Security OAuth2 进行授权。尝试刷新令牌时出现错误:UserDetailsService is required(有趣的是,我仅在 unix 机器上而不是在 Windows 上收到此错误)。我正在使用 Spring OAuth2 2.0.7 版。
由于某种原因,DefaultTokenService 中的AuthenticationManager 不为空,它会尝试对用户进行身份验证以检查他是否仍然存在。我认为它被初始化是因为一些 spring security 与 spring 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();
}
}
这是我的 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");
}
}
【问题讨论】:
标签: java spring spring-security spring-security-oauth2 spring-oauth2