Spring Security Application Development (12) Get logged-in user information

Source: Internet
Author: User
Tags session id

1.1. get logged in user information

when using In Spring Security applications, you can obtain information about a logged-on user through the SecurityContext interface. an instance of the securitycontext interface is getcontext () through The static method of the securitycontextholder get.

through SecurityContext can obtain An instance of the authentication interface, which can be obtained through the authentication interface:

Principal: The protagonist, typically an instance of an userdetails interface, is the User object by default.

Credentials: Voucher, usually a password.

Authorities: A list of roles owned by the user.

Details:webauthenticationdetails, including information such as IP address and Session ID .

A typical code to display the above user login information is as follows:

@Controller @requestmapping ("Home") Public classHomeController {@RequestMapping ("/") PublicModelandview Index () {Modelandview mv=NewModelandview (); Mv.addobject ("Message", "hello,welcome!"); SecurityContext SC=Securitycontextholder.getcontext (); Mv.addobject ("SC", SC); Object principal=sc.getauthentication (). Getprincipal ();if(Principalinstanceofuserdetails) {userdetails Userdetails=(userdetails) principal;mv.addobject ("Userdetails", userdetails);  for(grantedauthority authority:userDetails.getAuthorities ()) {System.out.println (authority.getauthority () )+" , " +Authority.getclass (). Getcanonicalname ());}}Else{String username=(String) principal.tostring (); Mv.addobject ("Username", username);} Mv.setviewname ("Home/index");returnMV;}} 

The JSP page is as follows:

<c:ifTest= "${username!=null}"><P>Username:${username}</P></c:if> <P><BR/>SecurityContext: ${sc.getclass ()}<BR/>authentication: ${sc.authentication.getclass ()}<BR/>Credentials: ${sc.authentication.credentials}<BR/>Details: ${sc.authentication.details}</P> <P> <c:ifTest= "${userdetails!=null}">userdetails: ${userdetails.getclass ()}<Table><TR><TD>Username</TD><TD>${userdetails.username}</TD></TR><TR><TD>Password</TD><TD>${userdetails.password}</TD></TR></Table>Authorities:${userdetails.authorities}<ul><C:foreachvar= "Item"Items= "${userdetails.authorities}"Varstatus= "Index" > <Li>${index.index}/${index.count}:${item.authority},${item.getclass ()}</Li></C:foreach></ul></c:if> 

The results of the operation are as follows:

Securitycontext:class Org.springframework.security.core.context.SecurityContextImpl

Authentication:class Org.springframework.security.authentication.UsernamePasswordAuthenticationToken

Credentials:

Details:org.sprin[email protected]b364:remoteipaddress:0:0:0:0:0:0:0:1; Sessionid:de77cc038c592f5c301c605654436bee

Userdetails:class Org.springframework.security.core.userdetails.User

Username Zhangsan

Password

Authorities: [Role_test, Role_test2, Role_user]

0/1:role_test,class org.springframework.security.core.authority.SimpleGrantedAuthority

1/2:role_test2,class org.springframework.security.core.authority.SimpleGrantedAuthority

2/3:role_user,class org.springframework.security.core.authority.SimpleGrantedAuthority

Spring Security Application Development (12) Get logged-in user information

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.