【问题标题】:Proper component where to encrypt in Spring MVC在 Spring MVC 中加密的正确组件
【发布时间】:2017-02-22 19:29:23
【问题描述】:

我是刚开始使用 Spring MVC 的新手(不幸的是,文档对我没有多大帮助),我仍在摸索在哪里使用 Jasypt PasswordEncrypter 来加密通过注册表单收到的密码。直接在用户模型密码设置器中执行此操作是一种不好的做法,还是应该使用服务?另外,我相信这可以与 Spring Security 集成以使加密器也用于身份验证?谢谢。

【问题讨论】:

    标签: java spring-mvc spring-security


    【解决方案1】:

    验证时

    使用 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

    另外,请确保为要保护的密码定义输入

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2015-12-21
    • 2018-02-19
    • 2011-03-31
    • 2016-08-21
    • 2016-07-28
    • 2011-11-26
    相关资源
    最近更新 更多