在具有 Spring MVC 和 Spring Security 的 Web 应用程序中。
有没有办法手动设置 UserPrincipal?
我需要通过我的 web 应用程序的管理部分切换到另一个用户。在我的控制器中,是否可以在请求中设置用户主体?就像我是其他人一样连接。
像这样:request.setUserPrincipal().getName()
在具有 Spring MVC 和 Spring Security 的 Web 应用程序中。
有没有办法手动设置 UserPrincipal?
我需要通过我的 web 应用程序的管理部分切换到另一个用户。在我的控制器中,是否可以在请求中设置用户主体?就像我是其他人一样连接。
像这样:request.setUserPrincipal().getName()
我做过这样的事情来让人们在注册后自动登录。似乎它会做你正在寻找的东西:
Authentication authentication = new UsernamePasswordAuthenticationToken(
userService.loadUserByUsername(u.getUserName()), null,
AuthorityUtils.createAuthorityList("ROLE_USER"));
SecurityContextHolder.getContext().setAuthentication(authentication);
我正在从服务中获取我的用户。这必须实现org.springframework.security.core.userdetails.UserDetails接口。
我认为这应该让您对需要做什么有一个很好的了解。我记得我花了一段时间从文档中拼凑起来。