Skip to content

Commit

Permalink
Make JdbcUserDetailsManager compat with postgre
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangJian He committed Mar 11, 2023
1 parent ee028dd commit cd23991
Showing 1 changed file with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,56 @@ public LogoutHandler logoutHandler() {
return new DefaultLogoutHandler();
}

@Bean
public static JdbcUserDetailsManager jdbcUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
@Profile("mysql")
@Bean(name = "jdbcUserDetailsManager")
public static JdbcUserDetailsManager jdbcMysqlUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
JdbcUserDetailsManager jdbcUserDetailsManager = auth.jdbcAuthentication()
.passwordEncoder(passwordEncoder).dataSource(datasource)
.usersByUsernameQuery("select Username,Password,Enabled from `Users` where Username = ?")
.authoritiesByUsernameQuery(
"select Username,Authority from `Authorities` where Username = ?")
.getUserDetailsService();
.passwordEncoder(passwordEncoder).dataSource(datasource)
.usersByUsernameQuery("select Username,Password,Enabled from `Users` where Username = ?")
.authoritiesByUsernameQuery(
"select Username,Authority from `Authorities` where Username = ?")
.getUserDetailsService();

jdbcUserDetailsManager.setUserExistsSql("select Username from `Users` where Username = ?");
jdbcUserDetailsManager
.setCreateUserSql("insert into `Users` (Username, Password, Enabled) values (?,?,?)");
.setCreateUserSql("insert into `Users` (Username, Password, Enabled) values (?,?,?)");
jdbcUserDetailsManager
.setUpdateUserSql("update `Users` set Password = ?, Enabled = ? where id = (select u.id from (select id from `Users` where Username = ?) as u)");
.setUpdateUserSql("update `Users` set Password = ?, Enabled = ? where id = (select u.id from (select id from `Users` where Username = ?) as u)");
jdbcUserDetailsManager.setDeleteUserSql("delete from `Users` where id = (select u.id from (select id from `Users` where Username = ?) as u)");
jdbcUserDetailsManager
.setCreateAuthoritySql("insert into `Authorities` (Username, Authority) values (?,?)");
.setCreateAuthoritySql("insert into `Authorities` (Username, Authority) values (?,?)");
jdbcUserDetailsManager
.setDeleteUserAuthoritiesSql("delete from `Authorities` where id in (select a.id from (select id from `Authorities` where Username = ?) as a)");
.setDeleteUserAuthoritiesSql("delete from `Authorities` where id in (select a.id from (select id from `Authorities` where Username = ?) as a)");
jdbcUserDetailsManager
.setChangePasswordSql("update `Users` set Password = ? where id = (select u.id from (select id from `Users` where Username = ?) as u)");
.setChangePasswordSql("update `Users` set Password = ? where id = (select u.id from (select id from `Users` where Username = ?) as u)");

return jdbcUserDetailsManager;
}

@Profile("postgre")
@Bean(name = "jdbcUserDetailsManager")
public static JdbcUserDetailsManager jdbcPostgreUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
JdbcUserDetailsManager jdbcUserDetailsManager = auth.jdbcAuthentication()
.passwordEncoder(passwordEncoder).dataSource(datasource)
.usersByUsernameQuery("SELECT \"Username\", \"Password\", \"Enabled\" FROM \"Users\" WHERE \"Username\" = ?")
.authoritiesByUsernameQuery(
"SELECT \"Username\", \"Authority\" FROM \"Authorities\" WHERE \"Username\" = ?")
.getUserDetailsService();

jdbcUserDetailsManager.setUserExistsSql("SELECT \"Username\" FROM \"Users\" WHERE \"Username\" = ?");
jdbcUserDetailsManager
.setCreateUserSql("INSERT INTO \"Users\" (\"Username\", \"Password\", \"Enabled\") VALUES (?,?,?)");
jdbcUserDetailsManager
.setUpdateUserSql("UPDATE \"Users\" SET \"Password\" = ?, \"Enabled\" = ? WHERE \"id\" = (SELECT u.\"id\" FROM (SELECT \"id\" FROM \"Users\" WHERE \"Username\" = ?) AS u)");
jdbcUserDetailsManager.setDeleteUserSql("DELETE FROM \"Users\" WHERE \"id\" = (SELECT u.\"id\" FROM (SELECT \"id\" FROM \"Users\" WHERE \"Username\" = ?) AS u)");
jdbcUserDetailsManager
.setCreateAuthoritySql("INSERT INTO \"Authorities\" (\"Username\", \"Authority\") VALUES (?,?)");
jdbcUserDetailsManager
.setDeleteUserAuthoritiesSql("DELETE FROM \"Authorities\" WHERE \"id\" IN (SELECT a.\"id\" FROM (SELECT \"id\" FROM \"Authorities\" WHERE \"Username\" = ?) AS a)");
jdbcUserDetailsManager
.setChangePasswordSql("UPDATE \"Users\" SET \"Password\" = ? WHERE \"id\" = (SELECT u.\"id\" FROM (SELECT \"id\" FROM \"Users\" WHERE \"Username\" = ?) AS u)");

return jdbcUserDetailsManager;
}
Expand Down Expand Up @@ -340,12 +368,22 @@ public PasswordEncoder passwordEncoder() {
return SpringSecurityAuthAutoConfiguration.passwordEncoder();
}

@Profile("mysql")
@Bean
@ConditionalOnMissingBean(JdbcUserDetailsManager.class)
public JdbcUserDetailsManager jdbcUserDetailsManager(PasswordEncoder passwordEncoder,
public JdbcUserDetailsManager jdbcMysqlUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
return SpringSecurityAuthAutoConfiguration
.jdbcUserDetailsManager(passwordEncoder, auth, datasource);
.jdbcMysqlUserDetailsManager(passwordEncoder, auth, datasource);
}

@Profile("postgre")
@Bean
@ConditionalOnMissingBean(JdbcUserDetailsManager.class)
public JdbcUserDetailsManager jdbcPostgreUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
return SpringSecurityAuthAutoConfiguration
.jdbcPostgreUserDetailsManager(passwordEncoder, auth, datasource);
}

@Bean
Expand Down

0 comments on commit cd23991

Please sign in to comment.