-
-
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
[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with postgre #4790
Conversation
cd23991
to
e9276db
Compare
Codecov Report
@@ Coverage Diff @@
## master #4790 +/- ##
============================================
+ Coverage 47.90% 47.97% +0.07%
- Complexity 1701 1703 +2
============================================
Files 346 346
Lines 10694 10697 +3
Branches 1066 1066
============================================
+ Hits 5123 5132 +9
+ Misses 5255 5249 -6
Partials 316 316
... and 1 file with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
A new error occurred when I tried to create the app.
|
@nobodyiam It's a hibernate error. I think it's no relationship with this patch. That's make JdbcUserDetailsManager compat with postgre.
|
The sql provided in #4782 is wrong, after I corrected it, another error occurred after adding an item
|
return jdbcUserDetailsManager; | ||
} | ||
|
||
@Profile("postgre") |
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.
This duplicated the logic which doesn't look elegant as we may need to duplicate one for other database like h2, is there a better way to handle the sql difference?
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.
I didn't find a better way since we are direct using jdbc. Maybe we can refract this to use jpa. But I have no idea how to achieve this.
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.
It looks like the only difference is the quoted identifier, MySQL is ` while pg and h2 is ".
We may retrieve the Dialect
and get the identifier from it?
@Bean
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
}
...
}
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.
@nobodyiam Fixed
@nobodyiam Thanks for your feedback. I think I will resolve it later out of this pr :) |
e9276db
to
339ed41
Compare
"select Username,Authority from `Authorities` where Username = ?") | ||
.getUserDetailsService(); | ||
.passwordEncoder(passwordEncoder).dataSource(datasource) | ||
.usersByUsernameQuery("SELECT " + openQuote + "Username" + closeQuote + "," + openQuote + "Password" + closeQuote + "," + openQuote + "Enabled" + closeQuote + " FROM " + openQuote + "Users" + closeQuote + " where " + openQuote + "Username" + closeQuote + " = ?") |
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.
This reduces the code readability a lot, maybe we could embed some placeholders in the SQL and replace the placeholders with openQuote and closeQuote? e.g.
"select ^Username$, ^Password$, ^Enabled$ from ^Users$ where ^Username$ = ?".replace('^', openQuote).replace('$', closeQuote);
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.
it sounds like a specific trick, I can't say it's better. I think hibernate uses 'openQuote' way. We can rename the variable to openQ, closeQ to reduce the line length. I prefer no to change, but I can change if you insist.
Please let me know what you think
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.
Or we may extract some method to do the job? The main purpose is to improve the code readability.
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.
How about we use the built-in MessageFormat to format the SQL?
.usersByUsernameQuery(MessageFormat.format("select {0}Username{1},{0}Password{1},{0}Enabled{1} from {0}Users{1} where {0}Username{1} = ?", openQuote, closeQuote));
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.
@nobodyiam Fixed
f97f6f9
to
a10494b
Compare
.authoritiesByUsernameQuery(authoritiesQuerySql(openQuote, closeQuote)) | ||
.getUserDetailsService(); | ||
|
||
jdbcUserDetailsManager.setUserExistsSql(usersExistsSql(openQuote, closeQuote)); |
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.
How about we simply inline the statement here as the method is not used elsewhere.
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)) |
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?
|
||
jdbcUserDetailsManager.setUserExistsSql(MessageFormat.format("SELECT {0}Username{1} FROM {0}Users{1} WHERE {0}Username{1} = ?", openQuote, closeQuote)); | ||
jdbcUserDetailsManager.setCreateUserSql(MessageFormat.format("INSERT INTO {0}Users{1} ({0}Username{1}, {0}Password{1}, {0}Enabled{1}) values (?,?,?)", openQuote, closeQuote)); | ||
jdbcUserDetailsManager.setUpdateUserSql(MessageFormat.format("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)", openQuote, closeQuote)); |
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.
do we need to quote id
? btw, is it necessary to use the correct case like Id
?
...tal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java
Outdated
Show resolved
Hide resolved
...tal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java
Outdated
Show resolved
Hide resolved
...tal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java
Outdated
Show resolved
Hide resolved
...tal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java
Outdated
Show resolved
Hide resolved
ff68b70
to
bc6a93e
Compare
@nobodyiam PTAL again. Thanks |
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.
LGTM
Brief changelog
Use profile to make JdbcUserDetailsManager compat with postgre
Follow this checklist to help us incorporate your contribution quickly and easily:
mvn clean test
to make sure this pull request doesn't break anything.CHANGES
log.