[原创]使用Acegi时获取用户信息的几个函数

Acegi框架通过ThreadLocal存放用户信息,因此可以通过以下函数获取相关信息:

ExpandedBlockStart.gif ContractedBlock.gif    /** */ /**
InBlock.gif   * 取得当前用户名
InBlock.gif   * 

@return
ExpandedBlockEnd.gif   
*/

ExpandedBlockStart.gifContractedBlock.gif  
public   static  String getUsername() dot.gif {
InBlock.gif    Context context
=ContextHolder.getContext();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(context!=null)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(context instanceof SecureContext)dot.gif{
InBlock.gif        SecureContext sc
=(SecureContext)context;
InBlock.gif        Authentication auth
=sc.getAuthentication();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(auth!=null)dot.gif{
InBlock.gif          Object principal
=auth.getPrincipal();
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if(principal instanceof UserDetails) dot.gif{
InBlock.gif            
return ((UserDetails)principal).getUsername();
ExpandedSubBlockStart.gifContractedSubBlock.gif          }
elsedot.gif{
InBlock.gif            
return principal.toString();
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return null;
ExpandedBlockEnd.gif  }

ExpandedBlockStart.gifContractedBlock.gif  
/** */ /**
InBlock.gif   * 取得当前用户密码
InBlock.gif   * 

@return
ExpandedBlockEnd.gif   
*/

ExpandedBlockStart.gifContractedBlock.gif  
public   static  String getPassword() dot.gif {
InBlock.gif    Context context
=ContextHolder.getContext();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(context!=null)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(context instanceof SecureContext)dot.gif{
InBlock.gif        SecureContext sc
=(SecureContext)context;
InBlock.gif        Authentication auth
=sc.getAuthentication();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(auth!=null)dot.gif{
InBlock.gif          Object principal
=auth.getPrincipal();
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if(principal instanceof UserDetails) dot.gif{
InBlock.gif            
return ((UserDetails)principal).getPassword();
ExpandedSubBlockStart.gifContractedSubBlock.gif          }
elsedot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return null;
ExpandedBlockEnd.gif  }

ExpandedBlockStart.gifContractedBlock.gif  
/** */ /**
InBlock.gif   * 取得当前用户session id
InBlock.gif   * 

@return sessionid or null
ExpandedBlockEnd.gif   
*/

ExpandedBlockStart.gifContractedBlock.gif  
public   static  String getSessionID() dot.gif {
InBlock.gif    Context context
=ContextHolder.getContext();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(context!=null)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if(context instanceof SecureContext)dot.gif{
InBlock.gif        SecureContext sc
=(SecureContext)context;
InBlock.gif        Authentication auth
=sc.getAuthentication();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(auth!=null)dot.gif{
InBlock.gif          Object details
=auth.getDetails();
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if(details instanceof WebAuthenticationDetails) dot.gif{
InBlock.gif            
return ((WebAuthenticationDetails)details).getSessionId();
ExpandedSubBlockStart.gifContractedSubBlock.gif          }
elsedot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return null;
ExpandedBlockEnd.gif  }

None.gif
33487.html

一餐三碗 2006-03-03 16:17 发表评论


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值