Java 春天2。未在DaoAuthenticationProvider中设置密码编码器

Java 春天2。未在DaoAuthenticationProvider中设置密码编码器,java,spring,spring-security,oauth-2.0,Java,Spring,Spring Security,Oauth 2.0,我对SpringOAuth和SpringSecurity还很陌生。我正在尝试在我的项目中使用客户端\u凭据流。现在,我设法使用我自己的CustomDetailsService,以便从系统中已经存在的数据库中获取客户端id和密码(secret)。唯一的问题是,我无法更改AuthorizationServer使用的DaoAuthenticationProvider中的密码编码器-它默认设置为PlaintextPasswordEncoder。我无法配置它的方式,它将使用例如SHAPasswordEnc

我对SpringOAuth和SpringSecurity还很陌生。我正在尝试在我的项目中使用客户端\u凭据流。现在,我设法使用我自己的CustomDetailsService,以便从系统中已经存在的数据库中获取客户端id和密码(secret)。唯一的问题是,我无法更改AuthorizationServer使用的DaoAuthenticationProvider中的密码编码器-它默认设置为PlaintextPasswordEncoder。我无法配置它的方式,它将使用例如SHAPasswordEncoder。它总是使用纯文本编码器。我可能不太了解流程,因为我是春天的新手

以下是我的一些代码(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 {

    ...
}
}
以及自定义ClientDetails服务类:

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;
}
}
从my UserService获取的encodedPassword始终是一个错误的凭据,因为默认情况下DaoAuthenticationProvider设置了一个PlaintextPasswordEncoder

我错过了什么?
是否可以在用于检查此处凭据的DaoAuthenticationProvider中设置密码编码器?或者我必须编写我自己的AuthenticationProvider,以我想要的方式检查它吗?

如果您只想用另一个pass编码器配置spring身份验证,请使用此配置



注意:-在创建用户过程中,您需要使用相同的密码编码器类加密用户密码。

我发现解决此问题的方法是覆盖
授权服务器ConfigureRadapter上的
配置

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

我不是在做同样的事情,而是在使用基于Java的配置。我正在设置authenticationProvider:auth.authenticationProvider(daoAuthenticationProvider());以前配置为使用SHAPasswordEncoder的。还是我错了?你可以配置任何密码编码器。请记住,应该使用相同的passwordEncoder类encode方法对用户密码进行加密。在XML配置中,我们可以引用另一个passwordEncoder,但问题是我尝试在DaoAuthenticationProvider中设置编码器,但根本没有设置它-我调试了它。我知道在创建用户时需要使用相同的编码器,但这不是这里的问题。我遇到了完全相同的问题。您是否找到了修复方法?请注意,我还必须在
配置(AuthenticationManagerBuilder auth)中设置它
重写
websecurityConfigureAdapter
这样的子类:
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder)
否则只对客户端密码进行编码,而不对用户密码进行编码。感谢您的解决方案。我已经调查了好几个星期了