我怎样才能把盐传给春天?

时间:2017-05-05 09:01:04

标签: java spring spring-boot spring-security oauth-2.0

我在spring boot中实现了OAuth 2.0。当用户传递他的用户名/密码时,spring会尝试通过散列密码并将其与我传递的已经散列的密码进行比较来对其进行身份验证。但是,Spring总是错过盐,所以它总是会返回糟糕的凭据。

如何将盐传递给Spring?

这是我的UserDAO类:

@Service
public class UserDAO implements UserDetailsService, SaltSource{

private LoginDetails user;
private UserDetailsImpl userDetailsImpl;

@Autowired
private LoginDetailsManager loginDetailsManager;

@Override
public UserDetails1 loadUserByUsername(String username) throws UsernameNotFoundException {

    System.out.println("Get user");
    user = loginDetailsManager.getByUsername(username);
    System.out.println(user.toString());
    if (user == null) {
        throw new UsernameNotFoundException(
                "User " + username + " not found.");
    }

    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USER");
    List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    grantedAuthorities.add(grantedAuthority);

    String password = user.getPasswordHash();
    String salt = user.getSalt();
    userDetailsImpl = new UserDetailsImpl(user.getUsername(), user.getPasswordHash(), salt, grantedAuthorities);

    return new UserDetailsImpl(
            user.getUsername(),
            user.getPasswordHash(),
            salt,
            grantedAuthorities);
}
}

以下是我的AuthorizationServer类:

@Configuration
@EnableAuthorizationServer
protected class AuthorizationApplication extends AuthorizationServerConfigurerAdapter {

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

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;


    @Bean
    protected AuthorizationCodeServices getAuthorizationCodeServices() {
        return new JdbcAuthorizationCodeServices(dataSource);
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        AuthorizationCodeServices services = getAuthorizationCodeServices();
        JdbcTokenStore tokenStore = getTokenStore();
        endpoints
                .userDetailsService(userDetailsService)
                .authorizationCodeServices(services)
                .authenticationManager(authenticationManager)
                .tokenStore(tokenStore)
                .approvalStoreDisabled();
    }

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

通过一些调试,我发现Spring从SaltSource(org.springframework.security.authentication.dao.SaltSource)获取了它的盐。我无法弄清楚如何配置该来源。

0 个答案:

没有答案