2017-02-22 48 views
-1

我剛剛從Spring MVC開始的新手(不幸的是,文檔並沒有多大幫助),我仍然在摸索着使用Jasypt PasswordEncrypter加密的地方通過註冊表格收到的密碼。在用戶模型密碼設置器中直接使用它還是一種不好的做法,還是應該使用服務?另外,我相信這可以與Spring Security集成以將加密器用於身份驗證?謝謝。正確的組件在哪裏加密在Spring MVC

回答

0

在認證

使用authenticationProvider`編碼密碼認證後

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserDetailsService userDetailsService; 

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

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

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

在用戶創建

編碼在userService

@Autowired 
private PasswordEncoder passwordEncoder; 

然後

user.setPassword(passwordEncoder.encode(userDto.getPassword())); 

來源:Registration with Spring Security – Password Encoding

Another example

此外,確保定義輸入要保護

+1

密碼是的,你將需要操作的用戶控制器像註冊。檢查http://www.baeldung.com/registration-with-spring-mvc-and-spring-security –

相關問題