我们一般在登录成功之后,就跳转到了首页,在首页的头部会显示登录用户的用户名,那么这个咋实现呢?

实现

因为有这个框架的标签库,所以使用标签库就可以实现这个用户名的显示

在页面上面导入标签的规则

		<span class="hidden-xs">
			<%--<security:authentication property="principal.username" />--%>
				<security:authentication property="name" />
					</span>

后端这样写


    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(Integer[] ids, Integer userId){
   
        //从后台获取到当前认证通过后的用户名
        SecurityContextHolder.getContext().getAuthentication().getName();
        String username = ((SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        userService.addRoleToUser(userId, ids);
        return "redirect:toAddRolePage?success="+true+"&id="+userId;
    }

这样前段就会显示