简体   繁体   中英

How to configure jasypt for jdbcAuthentication passwordEncoder in Spring Boot

I need to configure PasswordEncoder to accept jasypt StandardPBEEncoder type instead of BCryptPasswordEncoder. Below is the code which I am using:

@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

@Autowired
private StandardPBEStringEncryptor pbeEncryptor;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {

 auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder)
     .usersByUsernameQuery("select USERNAME,USERPASS PASSWORD,USER_BLOCK ENABLED from TABLE_LOGIN_MASTER where USERNAME=?")
     .authoritiesByUsernameQuery("select USERNAME, 'ROLE_'||ROLE_VALUE AUTHORITY from TAB_LOGIN_ROLE where USERNAME=?");

}

I need to use pbeEncryptor instead of bCryptPasswordEncoder as PasswordEncoder. Is it possible?

So finally I got this solved. I created a bean for PasswordEncoder in the same class as below:

@Bean
public PasswordEncoder passwordEncoder() {
    return new PasswordEncoder() {

        ManagePassword mp = new ManagePassword();

        @Override
        public boolean matches(CharSequence rawpasswd, String encodedPassword) {
            // TODO Auto-generated method stub

            return mp.decrypt(encodedPassword).equals(rawpasswd.toString());
        }

        @Override
        public String encode(CharSequence rawpasswd) {
            // TODO Auto-generated method stub
            return mp.encrypt(rawpasswd.toString());
        }
    };

}

Complete code as below:

@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {

auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
    .usersByUsernameQuery("select USERNAME,USERPASS PASSWORD,USER_BLOCK ENABLED from TABLE_LOGIN_MASTER where USERNAME=?")
    .authoritiesByUsernameQuery("select USERNAME, 'ROLE_'||ROLE_VALUE AUTHORITY from TAB_LOGIN_ROLE where USERNAME=?");

}

@Bean
public PasswordEncoder passwordEncoder() {
    return new PasswordEncoder() {

        ManagePassword mp = new ManagePassword();

        @Override
        public boolean matches(CharSequence rawpasswd, String encodedPassword) {
            // TODO Auto-generated method stub

            return mp.decrypt(encodedPassword).equals(rawpasswd.toString());
        }

        @Override
        public String encode(CharSequence rawpasswd) {
            // TODO Auto-generated method stub
            return mp.encrypt(rawpasswd.toString());
        }
    };

}

ManagePassword class contains code to initialize the StandardPBEStringEncryptor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM