[原创]使用Acegi时获取用户信息的几个函数 Acegi框架
程序员文章站
2022-03-14 21:52:34
...
Acegi框架通过ThreadLocal存放用户信息,因此可以通过以下函数获取相关信息:
/**
* 取得当前用户名
*
@return
*/
public static String getUsername(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object principal
=auth.getPrincipal();
if(principal instanceof UserDetails) {
return ((UserDetails)principal).getUsername();
}
else{
return principal.toString();
}
}
}
}
return null;
}
/**
* 取得当前用户密码
*
@return
*/
public static String getPassword(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object principal
=auth.getPrincipal();
if(principal instanceof UserDetails) {
return ((UserDetails)principal).getPassword();
}
else{
return null;
}
}
}
}
return null;
}
/**
* 取得当前用户session id
*
@return sessionid or null
*/
public static String getSessionID(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object details
=auth.getDetails();
if(details instanceof WebAuthenticationDetails) {
return ((WebAuthenticationDetails)details).getSessionId();
}
else{
return null;
}
}
}
}
return null;
}
* 取得当前用户名
*
@return
*/
public static String getUsername(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object principal
=auth.getPrincipal();
if(principal instanceof UserDetails) {
return ((UserDetails)principal).getUsername();
}
else{
return principal.toString();
}
}
}
}
return null;
}
/**
* 取得当前用户密码
*
@return
*/
public static String getPassword(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object principal
=auth.getPrincipal();
if(principal instanceof UserDetails) {
return ((UserDetails)principal).getPassword();
}
else{
return null;
}
}
}
}
return null;
}
/**
* 取得当前用户session id
*
@return sessionid or null
*/
public static String getSessionID(){
Context context
=ContextHolder.getContext();
if(context!=null){
if(context instanceof SecureContext){
SecureContext sc
=(SecureContext)context;
Authentication auth
=sc.getAuthentication();
if(auth!=null){
Object details
=auth.getDetails();
if(details instanceof WebAuthenticationDetails) {
return ((WebAuthenticationDetails)details).getSessionId();
}
else{
return null;
}
}
}
}
return null;
}
下一篇: php如何获取客户端ip地址