I have a fully working Spring Security. I need to print the user name on the page, the controller is not suitable, because this page is part of a template for other pages.
I've tried a lot of options similar to this one:
<?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>
But the code does not print, no errors too. I tried to authenticate different users, and place the code on different pages, but it did not help me. How to fix it?
Try this, this is working for me.
<%@ 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>