-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
nobodyiam
merged 7 commits into
apolloconfig:master
from
hezhangjian:jdbc-user-manager-postgre
Mar 26, 2023
+41
−23
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f99d645
[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with …
ce89e63
upper case keyword
f460ee2
extract method
a10494b
use MessageFormat
a585c70
in line methods
bc6a93e
fix sql
385607c
Merge branch 'master' into jdbc-user-manager-postgre
nobodyiam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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)) | ||
.authoritiesByUsernameQuery(authoritiesQuerySql(openQuote, closeQuote)) | ||
.getUserDetailsService(); | ||
|
||
jdbcUserDetailsManager.setUserExistsSql(usersExistsSql(openQuote, closeQuote)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?