I need to read the id of the currently logged in user in order to filter DB results based on it. How to get it and in which part of code should this be retrieved (resources, services)?
This is not an issue, please use Stackoverflow for questions.
I guess it is an issue / should be an issue if there is no built in function to get the currently logged in user Id.
No its not an issue, there are multiple mechanism you can use either spring
security APIs or fetch user id from DB, there are more than 1 ways to do
this. Use stack overflow for these. Check spring security documentation or
spring data jpa docs
Thanks & regards,
Deepu
On 15 Aug 2016 13:44, "acrolink" [email protected] wrote:
I guess it is an issue / should be an issue if there is no built in
function to get the currently logged in user Id.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/jhipster/generator-jhipster/issues/3988#issuecomment-239783917,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDlF9G9-8GM7_x793Gwb9RRQ1RTy8LEks5qgFE2gaJpZM4JkPoW
.
Guys :+1: I have already figured out how to do that.. But I think there should be a built in function that returns the id not the login name. As for stack overflow, I hope they will grant me soon back the ability to post questions, I simply cannot understand how their banning system works. Thank you.
Please consider submitting a PR if you can describe your use case with more details.
@acrolink could you please at least share what you figured out? Since you bothered to create the issue in the first place, it would be a good way to contribute back to casual users arriving on this page through search engines. :)
@acrolink, look at this issue. Here's my code to get current user by id
:
public static Long getCurrentUserId() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String id = null;
if (authentication != null)
if (authentication.getPrincipal() instanceof UserDetails)
id = ((UserDetails) authentication.getPrincipal()).getUsername();
else if (authentication.getPrincipal() instanceof String)
id = (String) authentication.getPrincipal();
try {
return Long.valueOf(id != null ? id : "1"); //anonymoususer
} catch (NumberFormatException e) {
return 1L;
}
}
Also return String.valueOf(id)
as first param of security User
's constructor in UserDetailsService
.
There were a pull request #2115 just about this issue a year ago, but the code disappeared since.
@delverdev are you shure it's working for you?
I need to insert user into entity 'orderOpened' at create of new record..
In OrderOpenedResource in POST section I added:
User user=new User(); user=UserRepository.findOneById(getCurrentUserId()).get(); orderOpened.setUser(user);
For me it catches NumberFormatException
That works for me:
User user=new User(); user=UserRepository.findOneByLogin(getCurrentUserLogin()).get(); orderOpened.setUser(user);
public String getCurrentUserLogin() {
org.springframework.security.core.context.SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String login = null;
if (authentication != null)
if (authentication.getPrincipal() instanceof UserDetails)
login = ((UserDetails) authentication.getPrincipal()).getUsername();
else if (authentication.getPrincipal() instanceof String)
login = (String) authentication.getPrincipal();
return login;
// try {
// return Long.valueOf(id != null ? id : "1"); //anonymoususer
// } catch (NumberFormatException e) {
// return 6L;
// }
}
@shadowrider-pl, I wouldn't spam in this issue, because it's closed and my code rather a tip than a solution. So I will summarize:
Yes, it works for me. NumberFormatException
is obviously at the Long::parseLong
method. UserDetailsService::loadUserByUsername
should return User (Spring user), that has String.valueOf(id)
instead of login
at first constructor's argument. Otherwise, the problem in your code. Keep in mind, that it is only util method that should return long. So debug that method to define what returns from it.
Most helpful comment
@acrolink, look at this issue. Here's my code to get current user by
id
:Also return
String.valueOf(id)
as first param of securityUser
's constructor inUserDetailsService
.