Obtaining information about the logged in user
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I currently have a special service to obtain information about the logged in user. I have an interface
/**
* The service operates on the data of an authorised user.
*/
public interface AuthorizationService
User getUser();
String getUserId();
String getUsername();
boolean isLogged();
and implementation
@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService
private final UserSearchService userSearchService;
/**
* @inheritDoc
*/
@Override
public User getUser()
return userSearchService.getUserByUsername(this.getUsername());
/**
* @inheritDoc
*/
@Override
public String getUserId()
return userSearchService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()).getId();
...
and for example, when I use the method to create messages, I do so
@Override
public String createMessage(
@NotNull @Valid final SendMessageDTO sendMessageDTO
) throws ResourceNotFoundException, ResourceConflictException
log.info("Called with sendMessageDTO ", sendMessageDTO);
final UserEntity user = this.findUser(this.authorizationService.getUserId());
final MessageEntity message = this.sendMessageDtoToMessageEntity(sendMessageDTO);
message.setSender(user);
...
private UserEntity findUser(final String id) throws ResourceNotFoundException
return this.userRepository
.findByUniqueIdAndEnabledTrue(id)
.orElseThrow(() -> new ResourceNotFoundException("No user found with id " + id));
However, I do not know if this is a good way to obtain information about the logged-in user?
Instead of a DTO
User
object, I could return anentity
(I have a rule that I do not return entity service methods) and then I would not have to use thefindUser ()
method to search for a user. What do you think about it?Is my way of getting a DTO
User
logged in user or ID a safe way?
java spring
add a comment |Â
up vote
2
down vote
favorite
I currently have a special service to obtain information about the logged in user. I have an interface
/**
* The service operates on the data of an authorised user.
*/
public interface AuthorizationService
User getUser();
String getUserId();
String getUsername();
boolean isLogged();
and implementation
@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService
private final UserSearchService userSearchService;
/**
* @inheritDoc
*/
@Override
public User getUser()
return userSearchService.getUserByUsername(this.getUsername());
/**
* @inheritDoc
*/
@Override
public String getUserId()
return userSearchService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()).getId();
...
and for example, when I use the method to create messages, I do so
@Override
public String createMessage(
@NotNull @Valid final SendMessageDTO sendMessageDTO
) throws ResourceNotFoundException, ResourceConflictException
log.info("Called with sendMessageDTO ", sendMessageDTO);
final UserEntity user = this.findUser(this.authorizationService.getUserId());
final MessageEntity message = this.sendMessageDtoToMessageEntity(sendMessageDTO);
message.setSender(user);
...
private UserEntity findUser(final String id) throws ResourceNotFoundException
return this.userRepository
.findByUniqueIdAndEnabledTrue(id)
.orElseThrow(() -> new ResourceNotFoundException("No user found with id " + id));
However, I do not know if this is a good way to obtain information about the logged-in user?
Instead of a DTO
User
object, I could return anentity
(I have a rule that I do not return entity service methods) and then I would not have to use thefindUser ()
method to search for a user. What do you think about it?Is my way of getting a DTO
User
logged in user or ID a safe way?
java spring
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I currently have a special service to obtain information about the logged in user. I have an interface
/**
* The service operates on the data of an authorised user.
*/
public interface AuthorizationService
User getUser();
String getUserId();
String getUsername();
boolean isLogged();
and implementation
@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService
private final UserSearchService userSearchService;
/**
* @inheritDoc
*/
@Override
public User getUser()
return userSearchService.getUserByUsername(this.getUsername());
/**
* @inheritDoc
*/
@Override
public String getUserId()
return userSearchService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()).getId();
...
and for example, when I use the method to create messages, I do so
@Override
public String createMessage(
@NotNull @Valid final SendMessageDTO sendMessageDTO
) throws ResourceNotFoundException, ResourceConflictException
log.info("Called with sendMessageDTO ", sendMessageDTO);
final UserEntity user = this.findUser(this.authorizationService.getUserId());
final MessageEntity message = this.sendMessageDtoToMessageEntity(sendMessageDTO);
message.setSender(user);
...
private UserEntity findUser(final String id) throws ResourceNotFoundException
return this.userRepository
.findByUniqueIdAndEnabledTrue(id)
.orElseThrow(() -> new ResourceNotFoundException("No user found with id " + id));
However, I do not know if this is a good way to obtain information about the logged-in user?
Instead of a DTO
User
object, I could return anentity
(I have a rule that I do not return entity service methods) and then I would not have to use thefindUser ()
method to search for a user. What do you think about it?Is my way of getting a DTO
User
logged in user or ID a safe way?
java spring
I currently have a special service to obtain information about the logged in user. I have an interface
/**
* The service operates on the data of an authorised user.
*/
public interface AuthorizationService
User getUser();
String getUserId();
String getUsername();
boolean isLogged();
and implementation
@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService
private final UserSearchService userSearchService;
/**
* @inheritDoc
*/
@Override
public User getUser()
return userSearchService.getUserByUsername(this.getUsername());
/**
* @inheritDoc
*/
@Override
public String getUserId()
return userSearchService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()).getId();
...
and for example, when I use the method to create messages, I do so
@Override
public String createMessage(
@NotNull @Valid final SendMessageDTO sendMessageDTO
) throws ResourceNotFoundException, ResourceConflictException
log.info("Called with sendMessageDTO ", sendMessageDTO);
final UserEntity user = this.findUser(this.authorizationService.getUserId());
final MessageEntity message = this.sendMessageDtoToMessageEntity(sendMessageDTO);
message.setSender(user);
...
private UserEntity findUser(final String id) throws ResourceNotFoundException
return this.userRepository
.findByUniqueIdAndEnabledTrue(id)
.orElseThrow(() -> new ResourceNotFoundException("No user found with id " + id));
However, I do not know if this is a good way to obtain information about the logged-in user?
Instead of a DTO
User
object, I could return anentity
(I have a rule that I do not return entity service methods) and then I would not have to use thefindUser ()
method to search for a user. What do you think about it?Is my way of getting a DTO
User
logged in user or ID a safe way?
java spring
asked Apr 5 at 22:55
user166339
111
111
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
Usually the logged in user and it's common used information is stored in the session. And the session is usually wrapped in a Context
class (like SecurityContextHolder
itself). This is done because you can decouple your code from the session or from the servlet api (or whatever api you're using). It also makes it easier to test.
Most of web pages do show your user information when you're logged in, e.g. your avatar on this page on the top. So for every request you do not have to look up the user's information.
I'd introduce a Context
class which provides a setUser
and getUser
method.
Beside that:
AuthorizationService
: Authorization means: Is the user allowed to do x. (While authentication means: Is the user really the user he pretends to be). Or in other words: The name of the type is misleading.getUser()
: This method callsthis.getUsername()
-> I don't see the code of this one, bit I assume it looks up the username bySecurityContextHolder
. This means, theAuthorizationService
is tightly coupled Spring Security, which is bad. Same forgetUserId()
-> it's better to pass the arguments and put the responsibility to the presentation layer (which includes security usually)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Usually the logged in user and it's common used information is stored in the session. And the session is usually wrapped in a Context
class (like SecurityContextHolder
itself). This is done because you can decouple your code from the session or from the servlet api (or whatever api you're using). It also makes it easier to test.
Most of web pages do show your user information when you're logged in, e.g. your avatar on this page on the top. So for every request you do not have to look up the user's information.
I'd introduce a Context
class which provides a setUser
and getUser
method.
Beside that:
AuthorizationService
: Authorization means: Is the user allowed to do x. (While authentication means: Is the user really the user he pretends to be). Or in other words: The name of the type is misleading.getUser()
: This method callsthis.getUsername()
-> I don't see the code of this one, bit I assume it looks up the username bySecurityContextHolder
. This means, theAuthorizationService
is tightly coupled Spring Security, which is bad. Same forgetUserId()
-> it's better to pass the arguments and put the responsibility to the presentation layer (which includes security usually)
add a comment |Â
up vote
1
down vote
Usually the logged in user and it's common used information is stored in the session. And the session is usually wrapped in a Context
class (like SecurityContextHolder
itself). This is done because you can decouple your code from the session or from the servlet api (or whatever api you're using). It also makes it easier to test.
Most of web pages do show your user information when you're logged in, e.g. your avatar on this page on the top. So for every request you do not have to look up the user's information.
I'd introduce a Context
class which provides a setUser
and getUser
method.
Beside that:
AuthorizationService
: Authorization means: Is the user allowed to do x. (While authentication means: Is the user really the user he pretends to be). Or in other words: The name of the type is misleading.getUser()
: This method callsthis.getUsername()
-> I don't see the code of this one, bit I assume it looks up the username bySecurityContextHolder
. This means, theAuthorizationService
is tightly coupled Spring Security, which is bad. Same forgetUserId()
-> it's better to pass the arguments and put the responsibility to the presentation layer (which includes security usually)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Usually the logged in user and it's common used information is stored in the session. And the session is usually wrapped in a Context
class (like SecurityContextHolder
itself). This is done because you can decouple your code from the session or from the servlet api (or whatever api you're using). It also makes it easier to test.
Most of web pages do show your user information when you're logged in, e.g. your avatar on this page on the top. So for every request you do not have to look up the user's information.
I'd introduce a Context
class which provides a setUser
and getUser
method.
Beside that:
AuthorizationService
: Authorization means: Is the user allowed to do x. (While authentication means: Is the user really the user he pretends to be). Or in other words: The name of the type is misleading.getUser()
: This method callsthis.getUsername()
-> I don't see the code of this one, bit I assume it looks up the username bySecurityContextHolder
. This means, theAuthorizationService
is tightly coupled Spring Security, which is bad. Same forgetUserId()
-> it's better to pass the arguments and put the responsibility to the presentation layer (which includes security usually)
Usually the logged in user and it's common used information is stored in the session. And the session is usually wrapped in a Context
class (like SecurityContextHolder
itself). This is done because you can decouple your code from the session or from the servlet api (or whatever api you're using). It also makes it easier to test.
Most of web pages do show your user information when you're logged in, e.g. your avatar on this page on the top. So for every request you do not have to look up the user's information.
I'd introduce a Context
class which provides a setUser
and getUser
method.
Beside that:
AuthorizationService
: Authorization means: Is the user allowed to do x. (While authentication means: Is the user really the user he pretends to be). Or in other words: The name of the type is misleading.getUser()
: This method callsthis.getUsername()
-> I don't see the code of this one, bit I assume it looks up the username bySecurityContextHolder
. This means, theAuthorizationService
is tightly coupled Spring Security, which is bad. Same forgetUserId()
-> it's better to pass the arguments and put the responsibility to the presentation layer (which includes security usually)
answered Apr 11 at 16:21
slowy
1,796110
1,796110
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191364%2fobtaining-information-about-the-logged-in-user%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password