Java 如何在spring boot中为jdbcAuthentication passwordEncoder配置jasypt
我需要将PasswordEncoder配置为接受jasypt StandardPBEEncoder类型,而不是BCryptPasswordEncoder。Java 如何在spring boot中为jdbcAuthentication passwordEncoder配置jasypt,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我需要将PasswordEncoder配置为接受jasypt StandardPBEEncoder类型,而不是BCryptPasswordEncoder。 以下是我引用的代码: @Configuration @EnableWebSecurity public class DemoSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; @Autowired p
以下是我引用的代码:
@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=?");
}
我需要使用pbeencyptor而不是bCryptPasswordEncoder作为密码编码器。
有可能吗?所以我终于解决了这个问题。我在同一个类中为PasswordEncoder创建了一个bean,如下所示:
@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());
}
};
}
@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());
}
};
}
完成代码如下:
@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());
}
};
}
@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类包含初始化StandardPBEStringEncryptor的代码