如果我使用Md5PasswordEncoder进行密码加密,如何在spring security config中配置passwordEncoder?

时间:2017-02-20 09:01:33

标签: java spring spring-mvc spring-boot

Encryption


Md5PasswordEncoder md5PasswordEncoder =new Md5PasswordEncoder();
        md5PasswordEncoder.encodePassword(userRegistrationInfo.getPassword(),AppConstants.MD5_PASSWORD_ENCODER_SALT);




Spring Security Configuration

@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

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

我需要使用org.springframework.security.authentication.encoding.Md5PasswordEncoder进行密码加密。但我不知道如何在Spring安全配置中配置passwordEncoder()

6 个答案:

答案 0 :(得分:5)

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(customUserDetailsService)
                .passwordEncoder(passwordEncoder());
    }
}



@Bean
public PasswordEncoder passwordEncoder(){
    //implements PasswordEncoder and overide encode method with the MD5 protocol
    return new MD5PasswordEncoder();
}

答案 1 :(得分:4)

安全配置

                    @Autowired
                    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
                    }


            @Bean
                public PasswordEncoder passwordEncoder(){
                    PasswordEncoder encoder = new FlasherPasswordEncoder();
                    return encoder;
                }

PasswordEncoder MyOwn Implementation

        package com.flasher.config;

        import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
        import org.springframework.security.crypto.password.PasswordEncoder;

        public class FlasherPasswordEncoder implements PasswordEncoder {

            @Override
            public String encode(CharSequence rawPassword) {
                return new Md5PasswordEncoder().encodePassword(rawPassword.toString(), AppConstants.MD5_PASSWORD_ENCODER_SALT);

            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                return new Md5PasswordEncoder().encodePassword(rawPassword.toString(), AppConstants.MD5_PASSWORD_ENCODER_SALT)
                        .equals(encodedPassword);
            }

        }

答案 2 :(得分:1)

不确定您的问题是什么。 Md5PasswordEncoder有一个emtpy构造函数,所以你可以简单地

<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder ">
</bean>

然后将其传递给您的AuthenticationProvider(例如DaoAuthenticationProvider)

<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <property name="userDetailsService">
        <ref bean="yourUserDetailsService"/>
    </property>
    <property name="passwordEncoder">
        <ref bean="passwordEncoder"/>
    </property>
</bean>

更新:op评论说,他正在使用盐。 这还取决于您的身份验证提供程序。如果您使用DaoAuthenticationProvider,则可以使用setSaltSource设置盐源。只需在config中添加另一个属性,引用salt-source-bean即可。

答案 3 :(得分:0)

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        PasswordEncoder encoder = new Md5PasswordEncoder();
        return encoder;
    }

答案 4 :(得分:0)

Spring Security 5已被删除Md5PasswordEncoder。如果要使用MD5编码,则可以自定义:


var vm = new Vue({
  el: '#vue-instance',
  data: {
    inventory: [
      {name: 'MacBook Air', price: 1000},
      {name: 'MacBook Pro', price: 1800},
      {name: 'Lenovo W530', price: 1400},
      {name: 'Acer Aspire One', price: 300}
    ]
  }
});

答案 5 :(得分:0)

@Bean
public PasswordEncoder passwordEncoder(){
//MD5 encoder implementation
return new MD5PasswordEncoder();
}

将以上代码粘贴在 SecurityConfig类下的以下代码下方:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) 
throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder());
}