本文介紹了使用Spring安全刷新令牌調(diào)用失敗,需要OAuth2,錯(cuò)誤為:UserDetailsService的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在使用Spring Security OAuth2進(jìn)行授權(quán)。當(dāng)嘗試刷新令牌時(shí),我得到一個(gè)錯(cuò)誤:UserDetailsService is required
(有趣的是,我只在Unix機(jī)器上得到這個(gè)錯(cuò)誤,而不是在Windows上)。我使用的是Spring OAuth2 2.0.7版。
由于某種原因,DefaultTokenService
中的AuthenticationManager
不是空的,它會(huì)嘗試對(duì)用戶進(jìn)行身份驗(yàn)證,以檢查他是否仍然存在。我認(rèn)為它被初始化是因?yàn)橐恍㏒pring安全與Spring OAuth2配置問題。
我沒有使用任何自定義UserDetailsService
,因此在這一點(diǎn)上它不應(yīng)該對(duì)用戶進(jìn)行身份驗(yàn)證。然而,當(dāng)我調(diào)試它時(shí),我發(fā)現(xiàn)它試圖使用WebSecurityConfigurerAdapter
中的一個(gè),結(jié)果出現(xiàn)了這個(gè)錯(cuò)誤。即使我提供了我的定制虛擬UserDetailsService
,它也沒有使用那個(gè)虛擬對(duì)象,而是嘗試使用另一個(gè)為空的虛擬對(duì)象。我是不是漏掉了什么?我找不到為什么會(huì)發(fā)生這種情況?
這是我的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");
}
}
推薦答案
授權(quán)服務(wù)器終結(jié)點(diǎn)需要UserDetailsService
。在OAuth2Config
類中配置用戶詳細(xì)信息服務(wù),如下所示:
@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
}
這篇關(guān)于使用Spring安全刷新令牌調(diào)用失敗,需要OAuth2,錯(cuò)誤為:UserDetailsService的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,