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