2016-12-02 6 views
-1

私はJavaベースのWebアプリケーションで春のセキュリティを使用しています。私は、ユーザーが確認するために古いパスワードを入力する必要がある変更パスワード画面を作成する必要があります。春のセキュリティでパスワードを変更しているときに古いパスワードを一致させるにはどうすればよいですか?

ユーザーが入力した古いパスワードがdbの古いパスワードと一致するかどうかを確認する必要があります。

どのように私は春のセキュリティでそれを行うのですか?

以下は私の春のセキュリティ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; 
    } 
} 

答えて

1

パスワードは、ユーザー名/メール/ユーザーIDを使用してDBに格納し、最終的にPooledStringDigester.matches

public boolean isPasswordsMatch(String newPassword, String passwordFromDb) { 
    return _stringDigester.matches(newPassword, passwordFromDb); 
} 
を使用しています PasswordUtilsで関数を記述し、クエリ
関連する問題