Current user information in spring Security 1: If you get a tag library that can use spring security in a JSP page
Introducing tags into the page
| 1 |
<%@ taglib prefix="SEC" uri="http// Www.springframework.org/security/tags " %> |
And then:
| 1 |
<div> username : <sec:Authentication Property ="name"/></div> |
To display the current user.
2: If you want to get in the program
Look at the Internet a lot of writing is in the program to write such code
| 1 |
userdetails userdetails = (userdetails securitycontextholder.< Span class= "CRAYON-E" >getcontext (.< Span class= "CRAYON-E" >getauthentication ( . Getprincipal |
But I found in the actual application that the obtained authentication is null. Carefully read the source code discovery, if you want to use the above code to obtain the current user, must be in the spring
Security filter execution, or the Org.springframework.security.web.context.SecurityContextPersistenceFilter class will be executed at the end of the filter chain
Call Securitycontextholder.clearcontext () and empty the Securitycontextholder, so you get null. After spring security certification,
Security will store a Securitycontextimpl object in the session with various data from the current user
| 1234567891011121314151617181920 |
Securitycontextimpl securitycontextimpl = (securitycontextimpl) Request . GetSession(). GetAttribute("Spring_security_context"); //Login nameSystem. Out. println("Username:" + securitycontextimpl. Getauthentication(). GetName()); //Login password, unencryptedSystem. Out. println("Credentials:" + securitycontextimpl. Getauthentication(). Getcredentials()); webauthenticationdetails details = (webauthenticationdetails) Securitycontextimpl . Getauthentication(). Getdetails(); //Get access addressSystem. Out. println("remoteaddress" + details. Getremoteaddress()); //Get SessionIDSystem. Out. println("SessionId" + details. GetSessionID()); //Get the permissions that the current user haslist<grantedauthority> authorities = (list< Grantedauthority>) securitycontextimpl . Getauthentication(). Getauthorities(); for ( Grantedauthority grantedauthority : authorities) { System. Out. println("authority" + grantedauthority. Getauthority()); } |
Spring security Gets the current user