Spring Boot+Spring Security:获取用户信息和session并发控制 - 第19篇

一、获取当前用户信息

1.1 从页面上显示当前登陆的用户名

 <h1>欢迎使用Spring Security!
      当前登录账号:<label th:text="${name}"></label>
,通过标签设置: <label sec:authentication="name"></label>
</h1>

1.2 在程序中获得当前登陆用户对应的对象

	@GetMapping({"","/","/index"})
	public String index(Model model) {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if("anonymousUser".equals(principal)) {
			model.addAttribute("name","anonymous");
		}else {
			User user = (User)principal;
			model.addAttribute("name",user.getUsername())

猜你喜欢

转载自blog.csdn.net/linxingliang/article/details/104925335