使用Acegi 1.0获取用户信息

使用Acegi 1.0获取用户信息

使用Acegi 1.0获取用户信息,

import  org.acegisecurity.Authentication;
import  org.acegisecurity.context.SecurityContext;
import  org.acegisecurity.context.SecurityContextHolder;
import  org.acegisecurity.ui.WebAuthenticationDetails;
import  org.acegisecurity.userdetails.UserDetails;
public   class  ThreadLocaTest  {
 
 
/** */ /**
    * 取得当前用户名
    * 
    
*/

   
public   static  String getUsername() {
    SecurityContext ctx 
=  SecurityContextHolder.getContext();
     
if (ctx != null ) {
      Authentication auth 
=  ctx.getAuthentication();   
         
if (auth != null ) {
           Object principal
= auth.getPrincipal();
           
if (principal  instanceof  UserDetails)  {
             
return  ((UserDetails)principal).getUsername();
           }
else {
             
return  principal.toString();         
         }

       }

     }

     
return   null ;
   }

   
/** */ /**  
    * 取得当前用户密码
    * 
    
*/

   
public   static  String getPassword() {
    SecurityContext ctx 
=  SecurityContextHolder.getContext();
     
if (ctx != null ) {   
         Authentication auth
= ctx.getAuthentication();
         
if (auth != null ) {
           Object principal
= auth.getPrincipal();
           
if (principal  instanceof  UserDetails)  {
             
return  ((UserDetails)principal).getPassword();
           }
else {
             
return   null ;
           }

         }

       
     }

     
return   null ;
   }

   
/** */ /**
    * 取得当前用户SessionId
    * 
    
*/

   
public   static  String getSessionID() {
    SecurityContext ctx 
=  SecurityContextHolder.getContext();
     
if (ctx != null ) {
       
if (ctx  instanceof  SecurityContext) {
        SecurityContext sc
= (SecurityContext)ctx;
         Authentication auth
= sc.getAuthentication();
         
if (auth != null ) {
           Object details
= auth.getDetails();
           
if (details  instanceof  WebAuthenticationDetails)  {
             
return  ((WebAuthenticationDetails)details).getSessionId();
           }
else {
             
return   null ;
           }

         }

       }

     }

     
return   null ;
   }

}


你可能感兴趣的:(使用Acegi 1.0获取用户信息)