Spring security Gets the current user

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.