本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7580943
建立时间:2006年5月29日




[Hibernate]Acegi+hibernate 动态实现基于角色的权限管理(4)
软件技术,  电脑与网络

lhwork 发表于 2006/6/13 11:36:54

3 基于hibernate的用户验证 acegi 默认的 的 用户验证是 通过UserDetailsService 接口 实现的 也就是说我们只要实现了 它的loadUserByUsername 方法。 1 2 public UserDetails loadUserByUsername(String username) 3         throws UsernameNotFoundException, DataAccessException; 以下是我的实现   1 package sample.auth;   2   3 import java.util.ArrayList;   4 import java.util.Iterator;   5 import java.util.List;   6 import java.util.Set;   7   8 import org.acegisecurity.GrantedAuthority;   9 import org.acegisecurity.GrantedAuthorityImpl;  10 import org.acegisecurity.userdetails.User;  11 import org.acegisecurity.userdetails.UserDetails;  12 import org.acegisecurity.userdetails.UserDetailsService;  13 import org.acegisecurity.userdetails.UsernameNotFoundException;  14 import org.springframework.dao.DataAccessException;  15  16 import sample.auth.cache.AuthorityBasedUserCache;  17 import sample.dao.IBaseDao;  18 import sample.mappings.role.Role;  19 import sample.utils.MisUtils;  20  21 public class HibernateDaoImpl implements UserDetailsService{  22  23          24         private String rolePrefix = "";  25         private boolean usernameBasedPrimaryKey = false;  26     private AuthorityBasedUserCache cache;  27  28     private IBaseDao baseDao;  29  30         public String getRolePrefix() {  31                 return rolePrefix;  32         }  33          34         public void setRolePrefix(String rolePrefix) {  35                 this.rolePrefix = rolePrefix;  36         }  37          38         public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {  39                 UserDetails user = getUsersByUsernameQuery(username);  40                 if(user == null) return null;  41                  42                 GrantedAuthority[] arrayAuths =getAuthoritiesByUsernameQuery(username);  43              if (arrayAuths.length == 0) {  44                     throw new UsernameNotFoundException("User has no GrantedAuthority");  45                 }  46                47              return new User(username, user.getPassword(), user.isEnabled(),  48                      true, true, true, arrayAuths);  49         }  50  51         /**  52         * 根据用户名查找用户  53         * @param username  54         * @return  55         * @throws DataAccessException  56         */  57         public UserDetails getUsersByUsernameQuery(String username)throws DataAccessException {  58                         sample.mappings.user.User misUser = (sample.mappings.user.User)baseDao.loadByKey(sample.mappings.user.User.class,"name",username);  59                         if(misUser != null)  60                         {  61                         org.acegisecurity.userdetails.UserDetails user =  62                         new User(misUser.getName(),misUser.getPassword(),MisUtils.parseBoolean(misUser.getEnable()),true,true,true,getAuthoritiesByUsernameQuery(username));  63                         return user;  64                         }else  65                         return null;         66                 }  67          68         /**  69           * 根据用户名查找角色  70           * @param username  71           * @return GrantedAuthority[] 用户角色  72           * @throws DataAccessException  73           */  74         public GrantedAuthority[] getAuthoritiesByUsernameQuery(String username)  75                 throws DataAccessException {  76                 sample.mappings.user.User misUser  77                 = (sample.mappings.user.User)baseDao.loadByKey(sample.mappings.user.User.class,"name",username);  78  79         if(misUser != null){  80                 GrantedAuthority[] grantedAuthoritys = cache.getAuthorityFromCache(misUser.getName());  81          82                 if(grantedAuthoritys == null){  83                  84                         Set roles =     misUser.getRoles();  85                         Iterator it = roles.iterator();  86                  87                         List list = new ArrayList();  88                         while(it.hasNext() ){  89          90                                 GrantedAuthorityImpl gai = new GrantedAuthorityImpl(  ((Role)it.next()).getName()  );  91                                 list.add(gai);  92                                 }  93                         grantedAuthoritys =(GrantedAuthority[]) list.toArray(new  GrantedAuthority[0]);  94                         cache.putAuthorityInCache(misUser.getName(),grantedAuthoritys);  95                         return grantedAuthoritys;  96                  97                 }  98                return grantedAuthoritys;  99         } 100 101                 return null; 102 } 103 104         public IBaseDao getBaseDao() { 105                 return baseDao; 106         } 107 108         public void setBaseDao(IBaseDao baseDao) { 109                 this.baseDao = baseDao; 110         } 111 112         public AuthorityBasedUserCache getCache() { 113                 return cache; 114         } 115 116         public void setCache(AuthorityBasedUserCache cache) { 117                 this.cache = cache; 118         } 119 120 } 121 通过以上对acegi 的 处理,足以满足我们目前在spring下基于RBAC的动态权限管理。同时在对频繁的数据库查询上使用了Ehcache作为缓存,在性能上有了很大的改善。代码 下载:http://www.blogjava.net/Files/limq/acegi-2.rar 可能不好打开,需要重新开一个IE


阅读全文(5479) | 回复(1) | 编辑 | 精华
 


回复:Acegi+hibernate 动态实现基于角色的权限管理(4)
软件技术,  电脑与网络

wcplyy(游客)发表评论于2007/4/19 9:56:54

能否告诉下联系方式,请教下ACEGI一些问题


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.078 second(s), page refreshed 144756946 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号