[原创]使用Acegi时获取用户信息的几个函数
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
;
}
posted on 2006-03-03 16:17
一餐三碗
阅读(2787)
评论(4)
编辑
收藏
所属分类:
Tips
评论
#
re: [原创]使用Acegi时获取用户信息的几个函数
2006-03-03 19:17
dylan
回复
更多评论
ContextHolder从0.9开始已经取消了
#
re: [原创]使用Acegi时获取用户信息的几个函数
2006-03-05 13:06
一餐三碗
回复
更多评论
@dylan
呵呵,我这个是针对0.8.3版本的,在新的版本里应该也有对应的方法。
#
re: [原创]使用Acegi时获取用户信息的几个函
2006-06-16 16:57
liunq
回复
更多评论
呵呵
#
re: [原创]使用Acegi时获取用户信息的几个函数
2007-01-01 13:26
itspy
回复
更多评论
1.0有如下方法
SecurityContext ctx = SecurityContextHolder.getContext();
Authentication auth = ctx.getAuthentication();
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理
相关文章:
[转贴]禁止windows自动更新后自动重启
[转载]Ajax based login using Acegi
解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题
[原创]使用Acegi时获取用户信息的几个函数
[转载]How to get (almost) 3 GB heap on Windows!
JAVA开门
导航
BlogJava
首页
新随笔
联系
聚合
管理
统计
随笔 - 18
文章 - 0
评论 - 85
引用 - 0
随笔分类
(18)
■
Tips(5)
(rss)
■
教程(3)
(rss)
■
新闻+评论(10)
(rss)
随笔档案
(18)
■
2007年5月 (1)
■
2007年4月 (1)
■
2007年3月 (2)
■
2007年2月 (2)
■
2006年12月 (1)
■
2006年9月 (1)
■
2006年8月 (1)
■
2006年6月 (1)
■
2006年3月 (1)
■
2006年2月 (1)
■
2005年12月 (2)
■
2005年11月 (4)
最新随笔
1. [推荐]开发者眼中的Silverlight和Flash
2. InfoQ中文网站发布!
3. Oracle 开放Toplink的全部源代码!
4. OpenNMS获得“网络和IT管理平台”金奖!
5. OpenTerracotta 开源的JVM集群解决方案
6. [转载]ONJava上的Hibernate 3 Annotations简易教程
7. [转载] Terracotta open sources JVM clustering
8. [转贴]禁止windows自动更新后自动重启
9. [转载]Ajax based login using Acegi
10. 解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题
搜索
积分与排名
积分 - 87010
排名 - 654
最新评论
1. re: 采用jxl实现数据库结果集导出到excel文件
撒旦法
--撒地方
2. re: [原创]实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架
评论内容较长,点击标题查看
--困惑----能帮我看看为什么吗?
3. re: [原创]实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架
福娃免费空间 地址
http://h.8wa.com
--adsf
4. re: [原创]实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架
评论内容较长,点击标题查看
--joseph
5. re: [原创]实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架
使用acegi做安全框架,web性能会有很大影响,我的项目加上后,勉强通过100并发,如果不加acegi,400并发轻松通过,有人有这方面的经验吗?
--fogu
阅读排行榜
1. [原创]实战Acegi:使用Acegi作为基于Spring框架的WEB应用的安全框架(50001)
2. 解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题(7246)
3. 采用jxl实现数据库结果集导出到excel文件(6702)
4. [原创]使用Acegi时获取用户信息的几个函数(2787)
5. Oracle 开放Toplink的全部源代码!(2450)