如何在春季启动时为jdbcAuthentication passwordEncoder配置jasypt

时间:2020-08-27 13:40:01

标签: java spring spring-boot spring-security

我需要将PasswordEncoder配置为接受jasypt StandardPBEEncoder类型,而不是BCryptPasswordEncoder。
下面是我引用的代码:

@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=?");

}

我需要使用pbeEncryptor代替bCryptPasswordEncoder作为PasswordEncoder。 有可能吗?

1 个答案:

答案 0 :(得分:0)

所以最终我解决了这个问题。我在下面的同一类中为PasswordEncoder创建了一个bean:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

完整代码如下:

@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类包含用于初始化StandardPBEStringEncryptor的代码。