【发布时间】:2021-03-13 11:29:24
【问题描述】:
我在 Spring 中配置 Oauth2 身份验证服务器时遇到问题,我有我的自定义 AuthenticationProvider 并且我正在定义我自己的 AuthenticationManager,但每次我向“/oauth/token”请求令牌时,我看到Spring一直在Spring定义的ProviderManager中注入和调用默认的DaoAuthenticationProvider。
这是我的配置类:
@Configuration
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
private AuthenticationManager authenticationManager;
public AuthorizationServerConfiguration(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Bean
PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
return converter;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private AuthenticationProvider authenticationProvider;
public WebSecurityConfig(AuthenticationProvider authenticationProvider) {
this.authenticationProvider = authenticationProvider;
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return new ProviderManager(authenticationProvider);
}
}
提前致谢
编辑
@Service
public class CustomAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
private ADCustomerService adCustomerService;
public CustomAuthenticationProvider(ADCustomerService adCustomerService) {
this.adCustomerService = adCustomerService;
}
@Override
protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
}
@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
return adCustomerService.retrieveUser(username, authentication);
}
}
【问题讨论】:
-
您的自定义身份验证提供程序在哪里?
-
@silentsudo 它在我的项目中,并带有“@Service”注释
-
@silentsudo 刚刚在原始帖子中添加了课程
-
我试图帮助你提出一个建议,但我会推荐使用keycloak.org
标签: java spring oauth-2.0 authorization