feat: security utils and setting userid on jwt#28910
feat: security utils and setting userid on jwt#28910OAllanFernando wants to merge 10 commits intojhipster:mainfrom
Conversation
|
Avoid this behavior setting this as solution This aproch can mitigate data transfer, clowd does not need to be our associates, rigth ? The previos code can be used to force the server, if the data is in a different server |
|
@OAllanFernando Can this PR be merged? |
|
Hi, @duongxinh2003 |
|
@OAllanFernando Yes, I am currently customizing the JWT token by adding claims similar to yours. I also hope there will be an update that allows extracting the userId directly from the JWT, and I have found this commit. Hopefully, this PR will be merged, or at least there will be a future update that enables retrieving the userId. |
|
Hello @OAllanFernando . Hopefully this gets merged, I'm also having the same issue on one of my projects |
|
Not having this feature yet is a pain in the ass... Thankfully someone is trying to implement it :) |
|
Tests are not passing. |
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
generators/spring-boot/templates/src/main/java/_package_/security/SecurityUtils.java.ejs
Outdated
Show resolved
Hide resolved
generators/spring-boot/templates/src/main/java/_package_/security/SecurityUtils.java.ejs
Outdated
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| Optional<<%= user.persistClass %>> userOptional = userService.getUserWithAuthoritiesByLogin(authentication.getName()); | ||
| Long userId = userOptional.map(User::getId).orElse(null); |
There was a problem hiding this comment.
userId should be passed through parameters due to reactive implementation.
There was a problem hiding this comment.
about this, only the integration of the id in the token or should the whole implementation be in the reactive?
I did only on the integration, as you said
and what would be the reactive?
There was a problem hiding this comment.
We provide reactive and imperative implementations.
So the createToken signature should be:
public String createToken(Authentication authentication, <%= user.primaryKey.type %> userId, boolean rememberMe) {
And userId retrieval should be in authorize method and then pass to createToken.
There was a problem hiding this comment.
we pass userService to the constructor in reactive line 86, so I inserted the search for the id in both possibilities line 91 and 123, in
this case we no longer need it, the reactive condition on createToken
<%_ if (!reactive) { _%>
if (userId != null) {
claimsBuilder.claim(USER_ID_CLAIM, userId);
}
<%_ } _%>
Rigth?
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
generators/spring-boot/templates/src/main/java/_package_/security/SecurityUtils.java.ejs
Outdated
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
generators/spring-boot/templates/src/main/java/_package_/security/SecurityUtils.java.ejs
Outdated
Show resolved
Hide resolved
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
| validity = now.plus(this.tokenValidityInSeconds, ChronoUnit.SECONDS); | ||
| } | ||
|
|
||
| <%_ if (reactive) { _%> |
There was a problem hiding this comment.
| <%_ if (reactive) { _%> | |
| <%_ if (!reactive) { _%> |
There was a problem hiding this comment.
Is this still necessary? Or do I leave the id only for !reactive flow?
generators/spring-boot/templates/src/main/java/_package_/security/SecurityUtils.java.ejs
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| Optional<<%= user.persistClass %>> userOptional = userService.getUserWithAuthoritiesByLogin(authentication.getName()); | ||
| Long userId = userOptional.map(User::getId).orElse(null); |
There was a problem hiding this comment.
We provide reactive and imperative implementations.
So the createToken signature should be:
public String createToken(Authentication authentication, <%= user.primaryKey.type %> userId, boolean rememberMe) {
And userId retrieval should be in authorize method and then pass to createToken.
...ators/spring-boot/templates/src/main/java/_package_/web/rest/AuthenticateController.java.ejs
Outdated
Show resolved
Hide resolved
…est/AuthenticateController.java.ejs
…ity/SecurityUtils.java.ejs
…ity/SecurityUtils.java.ejs
|
@OAllanFernando there is a cleaner way to implement using UserDetails. |
|
Superseded by #29108 |
this makes possible recover the current user id by the jwt, its useful and can save requests