Python 官方文档:入门教程 => 点击学习
目录在Bean中获取用户信息在Controller中获取用户信息通过 Interface 获取用户信息在jsP页面中获取用户信息在Bean中获取用户信息 Authentication
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
return currentUserName;
}
spring Security
框架提供了多种 AuthenticationToken
的派生类,根据自己的应用场景,可以对 SecurityContextHolder
里面的 AuthenticationToken
进行类型转换,如下:
UsernamePassWordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
// details里面存放了当前登录用户的详细信息
User userDetails = (User) authenticationToken.getDetails();
PS. AuthenticationToken的类型转换同样适用于下面提到的Principal类。
import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.WEB.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@RestController
public class SecurityController {
@GetMapping(value = "/username")
public String currentUserName(Principal principal) {
return principal.getName();
}
}
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@RestController
public class SecurityController {
@GetMapping("/username")
@ResponseBody
public String currentUserName(Authentication authentication) {
return authentication.getName();
}
}
import java.security.Principal;
import javax.servlet.Http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@RestController
public class SecurityController {
@GetMapping("/username")
public String currentUserNameSimple(HttpServletRequest request) {
Principal principal = request.getUserPrincipal();
return principal.getName();
}
}
注意:IAuthenticationFacade 这个接口在springboot2.1.0中已不存在了
通过 Interface 获取其实和第一种在 Bean 中获取用户信息是一样的,都是访问 SecurityContextHolder 获取的,只是进行了封装。
public interface IAuthenticationFacade {
Authentication getAuthentication();
}
@Component
public class AuthenticationFacade implements IAuthenticationFacade {
@Override
public Authentication getAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
}
下面是使用方法:
@RestController
public class SecurityController {
@Autowired
private IAuthenticationFacade authenticationFacade;
@GetMapping("/username")
public String currentUserNameSimple() {
Authentication authentication = authenticationFacade.getAuthentication();
return authentication.getName();
}
}
要使用 Spring Security 的标签特性,首先要在 JSP 页面引入 Security 的 tag:
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
通过以下方式可以获取到当前登录用户:
<security:authorize access="isAuthenticated()">
authenticated as <security:authentication property="principal.username" />
</security:authorize>
到此这篇关于详解Spring Security中获取当前登录用户的详细信息的几种方法的文章就介绍到这了,更多相关Spring Securit获取登录用户信息内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 详解SpringSecurity中获取当前登录用户的详细信息的几种方法
本文链接: https://www.lsjlt.com/news/148270.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0