How to configure passwordEncoder in spring security config if i use Md5PasswordEncoder for password encryption?

817
February 20, 2017, at 6:09 PM
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;
    }

I need to use org.springframework.security.authentication.encoding.Md5PasswordEncoder for my password encryption. but I don't know how to configure passwordEncoder() in Spring security configuration

Answer 1
@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();
}
Answer 2

Not sure what your problem is. Md5PasswordEncoder has an emtpy constructor so you can simply

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

And then pass it to your AuthenticationProvider (for example 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>
Answer 3
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        PasswordEncoder encoder = new Md5PasswordEncoder();
        return encoder;
    }
Answer 4

Security Config

                    @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);
            }
        }
READ ALSO
Increase limits with Jetty: URI is too large &gt;8192

Increase limits with Jetty: URI is too large >8192

I am running a Java webservice (JAX-RS) with command: java -jar wsjar

1888
How to add missing columns in a multi-schema based multi-tenant web app using eclipselink

How to add missing columns in a multi-schema based multi-tenant web app using eclipselink

I'm developing a multi-tenant web app with "Shared Database/Separate Schemas" approach using java, jpa(eclipselink), mysqlMy persistence file looks like:

363
Is this a bad solution for reversing a linked list? [on hold]

Is this a bad solution for reversing a linked list? [on hold]

For reversing a linked list, this works but I am not satisfied, how can I do better? This seems like a bad solution to be honest, taking up space every loop

236
Java - Error creating a functional interface with an

Java - Error creating a functional interface with an

I have the below test class in my java code:

374