gpt4 book ai didi

java - Spring Oauth2。 DaoAuthenticationProvider 中未设置密码编码器

转载 作者:IT老高 更新时间:2023-10-28 13:52:46 25 4
gpt4 key购买 nike

我对 Spring Oauth 和 Spring Security 还是很陌生。我正在尝试在我的项目中使用 client_credentials 流程。现在我设法使用我自己的 CustomDetailsS​​ervice 以便从我系统中已经存在的数据库中获取 client_id 和密码( secret )。唯一的问题是我无法更改 AuthorizationServer 使用的 DaoAuthenticationProvider 中的密码编码器 - 它默认设置为 PlaintextPasswordEncoder。我无法配置它,例如 SHAPasswordEncoder。它总是使用明文编码器。我可能不太了解流程,因为我是 Spring 的新手。

这是我的一些代码(没有 DaoAuthenticationProvider 的配置):

SecurityConfig.java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final String RESOURCE_ID = "restservice";

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/register/**");

}

@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}

@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService());
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new ShaPasswordEncoder();
}

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired
private MyCustomClientDetailsService myCustomClientDetailsService;

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

@Bean
public ResourceServerTokenServices defaultTokenServices() {
final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}

@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(myCustomClientDetailsService);
}

@Bean
public MyCustomClientDetailsService detailsService() {
return new MyCustomClientDetailsService();
}
}

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

...
}
}

还有自定义的ClientDetailsS​​ervice类:

public class MyCustomClientDetailsService implements ClientDetailsService {

@Autowired
private UserService userService;

@Override
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

User fan = userService.getFan(clientId);

if (fan == null) {
throw new NoSuchClientException("No client with requested id: " + clientId);
}

BaseClientDetails details = new BaseClientDetails(clientId, restservice, "write", "client_credentials", "USER");

details.setClientSecret(fan.getEncodedPassword());

return details;
}
}

从我的 UserService 获取的 encodedPassword 始终是错误的凭据,因为 DaoAuthenticationProvider 默认设置了 PlaintextPasswordEncoder。

我在那里缺少什么?是否可以在用于检查凭据的 DaoAuthenticationProvider 中设置密码编码器?还是我必须编写自己的 AuthenticationProvider,才能按照我想要的方式进行检查?

最佳答案

我发现问题的解决方案是在 AuthorizationServerConfigurerAdapter

上覆盖 configure
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.passwordEncoder(passwordEncoder);
}

关于java - Spring Oauth2。 DaoAuthenticationProvider 中未设置密码编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26013251/

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