Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with postgre #4790

Merged
merged 7 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Apollo 2.2.0
* [Misc dependency updates](https://github.com/apolloconfig/apollo/pull/4784)
* [Fix the problem that the deletion failure of the system rights management page does not prompt](https://github.com/apolloconfig/apollo/pull/4803)
* [Fix the issue of the system permission management page retrieving non-existent users](https://github.com/apolloconfig/apollo/pull/4802)
* [[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with postgre](https://github.com/apolloconfig/apollo/pull/4790)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@
import com.ctrip.framework.apollo.portal.spi.springsecurity.ApolloPasswordEncoderFactory;
import com.ctrip.framework.apollo.portal.spi.springsecurity.SpringSecurityUserInfoHolder;
import com.ctrip.framework.apollo.portal.spi.springsecurity.SpringSecurityUserService;

import java.text.MessageFormat;
import java.util.Collections;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
Expand Down Expand Up @@ -111,27 +117,35 @@ public LogoutHandler logoutHandler() {
}

@Bean
public static JdbcUserDetailsManager jdbcUserDetailsManager(PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth, DataSource datasource) throws Exception {
public static JdbcUserDetailsManager jdbcUserDetailsManager(
PasswordEncoder passwordEncoder,
AuthenticationManagerBuilder auth,
DataSource datasource,
EntityManagerFactory entityManagerFactory) throws Exception {
char openQuote = '`';
char closeQuote = '`';
try {
SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap(
SessionFactoryImplementor.class);
Dialect dialect = sessionFactory.getJdbcServices().getDialect();
openQuote = dialect.openQuote();
closeQuote = dialect.closeQuote();
} catch (Throwable ex) {
//ignore
}
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)");
.passwordEncoder(passwordEncoder).dataSource(datasource)
.usersByUsernameQuery(usersQuerySql(openQuote, closeQuote))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we also inline these 2?

.authoritiesByUsernameQuery(authoritiesQuerySql(openQuote, closeQuote))
.getUserDetailsService();

jdbcUserDetailsManager.setUserExistsSql(usersExistsSql(openQuote, closeQuote));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we simply inline the statement here as the method is not used elsewhere.

jdbcUserDetailsManager.setCreateUserSql(usersCreateSql(openQuote, closeQuote));
jdbcUserDetailsManager.setUpdateUserSql(usersUpdateSql(openQuote, closeQuote));
jdbcUserDetailsManager.setDeleteUserSql(usersDeleteSql(openQuote, closeQuote));
jdbcUserDetailsManager.setCreateAuthoritySql(authoritiesCreateSql(openQuote, closeQuote));
jdbcUserDetailsManager.setDeleteUserAuthoritiesSql(authoritiesDeleteSql(openQuote, closeQuote));
jdbcUserDetailsManager.setChangePasswordSql(changePasswordSql(openQuote, closeQuote));

return jdbcUserDetailsManager;
}
Expand All @@ -146,6 +160,52 @@ public UserService springSecurityUserService(PasswordEncoder passwordEncoder,

}

private static String usersCreateSql(char openQuote, char closeQuote) {
String template = "INSERT INTO {0}Users{1} ({0}Username{1}, {0}Password{1}, {0}Enabled{1}) values (?,?,?)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String usersDeleteSql(char openQuote, char closeQuote) {
String template = "DELETE FROM {0}Users{1} WHERE id = (SELECT u.id FROM (SELECT id FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String usersUpdateSql(char openQuote, char closeQuote) {
String template = "UPDATE {0}Users{1} SET {0}Password{1} = ?, {0}Enabled{1} = ? WHERE id = (SELECT u.id FROM (SELECT id FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String usersExistsSql(char openQuote, char closeQuote) {
String template = "SELECT {0}Username{1} FROM {0}Users{1} WHERE {0}Username{1} = ?";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String usersQuerySql(char openQuote, char closeQuote) {
String template = "SELECT {0}Username{1}, {0}Password{1}, {0}Enabled{1} FROM {0}Users{1} WHERE {0}Username{1} = ?";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String authoritiesCreateSql(char openQuote, char closeQuote) {
String template = "INSERT INTO {0}Authorities{1} ({0}Username{1}, {0}Authority{1}) values (?,?)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String authoritiesDeleteSql(char openQuote, char closeQuote) {
String template = "DELETE FROM {0}Authorities{1} WHERE id in (SELECT a.id FROM (SELECT id FROM {0}Authorities{1} WHERE {0}Username{1} = ?) AS a)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String changePasswordSql(char openQuote, char closeQuote) {
String template = "UPDATE {0}Users{1} SET {0}Password{1} = ? WHERE id = (SELECT u.id FROM (SELECT id FROM {0}Users{1} WHERE {0}Username{1} = ?) AS u)";
return MessageFormat.format(template, openQuote, closeQuote);
}

private static String authoritiesQuerySql(char openQuote, char closeQuote) {
String template = "SELECT {0}Username{1}, {0}Authority{1} FROM {0}Authorities{1} WHERE {0}Username{1} = ?";
return MessageFormat.format(template, openQuote, closeQuote);
}


@Order(99)
hezhangjian marked this conversation as resolved.
Show resolved Hide resolved
@Profile("auth")
@Configuration
Expand Down Expand Up @@ -342,10 +402,13 @@ public PasswordEncoder passwordEncoder() {

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

@Bean
Expand Down