Spring Security 4: Acquiring User ID and Roles

Spring Security provides JSP custom tags for accessing user id and roles

Update POM file for Spring Security JSP Tag Library

pom.xml

1
2
3
4
5
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${springsecurity.version}</version>
</dependency>

Add Spring Security JSP Tag Library to JSP page

home.jsp:

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

Display User ID

home.jsp:

1
User: <security:authentication property="principal.username"/>

Display User Roles

home.jsp:

1
Role(s): <security:authentication property="principal.authorities"/>

Note: Spring Security will automatically add a prefix for the roles. For example: if the role is “EMPLOYEE”, then it will display “[ROLE_EMPLOYEE]”