【问题标题】:How to match old password while changing password in spring security?spring security中更改密码时如何匹配旧密码?
【发布时间】:2017-04-16 21:50:14
【问题描述】:

我在基于 Java 的 Web 应用程序中使用 Spring Security。我需要创建一个更改密码屏幕,用户必须输入旧密码才能确认。

我需要检查用户输入的旧密码是否与数据库中的旧密码匹配。

我在春季安全中如何做到这一点。

以下是我的 Spring Security java 配置。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private AccessDecisionManager accessDecisionManager;

    @Bean
    @Autowired
    public AccessDecisionManager accessDecisionManager(AccessDecisionVoterImpl accessDecisionVoter) {
        List<AccessDecisionVoter<?>> accessDecisionVoters = new ArrayList<AccessDecisionVoter<?>>();
        accessDecisionVoters.add(new WebExpressionVoter());
        accessDecisionVoters.add(new AuthenticatedVoter());
        accessDecisionVoters.add(accessDecisionVoter);
        UnanimousBased accessDecisionManager = new UnanimousBased(accessDecisionVoters);
        return accessDecisionManager;
    }

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        PasswordEncoder passwordEncoder = new PasswordEncoder();
        passwordEncoder.setStringDigester(stringDigester());
        return passwordEncoder;
    }

    @Bean
    public PooledStringDigester stringDigester() {
        PooledStringDigester psd = new PooledStringDigester();

        psd.setPoolSize(2);
        psd.setAlgorithm("SHA-256");
        psd.setIterations(1000);
        psd.setSaltSizeBytes(16);
        psd.setSaltGenerator(randomSaltGenerator());

        return psd;
    }

    @Bean
    public RandomSaltGenerator randomSaltGenerator() {
        RandomSaltGenerator randomSaltGenerator = new RandomSaltGenerator();
        return randomSaltGenerator;
    }

另外,当创建新用户时,我将他的密码设置如下。

user.setPassword(passwordUtils.encryptUserPassword(user.getPassword()));


@Component("passwordUtil")
public class PasswordUtils {

    @Autowired
    private PooledStringDigester _stringDigester;

    public String encryptUserPassword(String originalPassword) {
        String encryptedPassword = _stringDigester.digest(originalPassword);
        return encryptedPassword;
    }
}

【问题讨论】:

    标签: java spring-mvc spring-security change-password


    【解决方案1】:

    使用用户名/电子邮件/用户ID查询存储在数据库中的密码,最后在PasswordUtils中编写一个使用PooledStringDigester.matches的函数

    public boolean isPasswordsMatch(String newPassword, String passwordFromDb) {
        return _stringDigester.matches(newPassword, passwordFromDb);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2013-10-18
      相关资源
      最近更新 更多