Exception 使用spring安全性从jsp访问用户名

Exception 使用spring安全性从jsp访问用户名,exception,spring-security,Exception,Spring Security,如果他登录,我想输出用户名。 从@Controller我可以访问: @RequestMapping("/success") public String success(Model model) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); //get logged in usernam

如果他登录,我想输出用户名。 从@Controller我可以访问:

@RequestMapping("/success")
    public String success(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
        model.addAttribute("name", name);
        return "success";
    }
如果我在jsp上使用名称,就会看到键入的名称

但是如果我在这个jsp中写

 <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    <sec:authentication property="principal.username"/>

由于您有使用用户名和密码的Authentication说明,主要是它本身就是用户名。试用

<sec:authentication property="principal"/>

Why everywhere write?好吧,如果您有一个简单的用户名和密码,用户名可以作为字符串直接存储在principal中。但是,如果您有额外的详细信息,如公司名称、部门等以及用户名,您可以将自定义对象分配给主体,因此在这种情况下,您将使用主体用户名
<sec:authentication property="principal"/>