2

我有一个完全工作的 Spring Security。我需要在页面上打印用户名,控制器不适合,因为这个页面是其他页面模板的一部分。

我已经尝试了很多类似于这个的选项:

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
    xmlns:form="http://www.springframework.org/tags/form"
    xmlns:display="http://displaytag.sf.net" version="2.0"
    xmlns:sec="http://www.springframework.org/security/tags">
    <jsp:directive.page contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" session="true" />
    <jsp:output doctype-root-element="html"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        omit-xml-declaration="true" />
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body>
...
<sec:authorize access="isAuthenticated()">
    <sec:authentication property="principal.username"></sec:authentication>
</sec:authorize>
...
</body>
    </html>
</jsp:root>

但是代码不打印,也没有错误。我尝试对不同的用户进行身份验证,并将代码放在不同的页面上,但这对我没有帮助。如何解决?

4

2 回答 2

4

试试这个,这对我有用。

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

<sec:authorize var="loggedIn" access="isAuthenticated()" />
<c:choose>
    <c:when test="${loggedIn}">
            <%= request.getUserPrincipal().getName() %>        
    </c:when>
    <c:otherwise>
    </c:otherwise>
</c:choose>
于 2013-07-23T15:06:20.740 回答
0

尝试这个 :

<sec:authentication property="principal.username"/>
于 2013-07-23T14:25:43.140 回答