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;
}