Spring Security: manual setUserPrincipal

In a web application with Spring MVC and Spring Security.

Is there a way to set UserPrincipal manually?

I need to switch to another user by the administrative part of my web application. In my controller, is it possible to install requester in a program? To connect as if I were someone else.

Similar: request.setUserPrincipal (). getName ()

+6
source share
1 answer

I made it so that automatically register people after registration. It seems to do what you are looking for:

Authentication authentication = new UsernamePasswordAuthenticationToken( userService.loadUserByUsername(u.getUserName()), null, AuthorityUtils.createAuthorityList("ROLE_USER")); SecurityContextHolder.getContext().setAuthentication(authentication); 

I grab my user from the service. This should implement the org.springframework.security.core.userdetails.UserDetails interface.

I think this should give you a good idea of ​​what to do. I remember it took me a while to put this together from the documents.

+4
source

Source: https://habr.com/ru/post/903595/


All Articles