知识大全 Spring框架下实现基于组的用户权限管理

Posted 权限

篇首语:书到用时方恨少,事非经过不知难。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Spring框架下实现基于组的用户权限管理相关的知识,希望对你有一定的参考价值。

Spring框架下实现基于组的用户权限管理  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

在几乎所有的web应用中都需要对访问者(用户)进行权限管理 因为我们希望某些页面只对特定的用户开放 以及某些操作只有符合身份的用户才能进行 这之中涉及到了身份验证和权限管理 只有单用户系统和多用户单权限系统才不需要权限管理   在本文中 使用了基于组的权限管理 并在Spring框架下利用HandlerInterceptorAdapter和Hibernate进行实现   User的结构是: public class User  private int id; private String name; private String password; private Set<String> groups = new HashSet<String>();

  UserGroup表:   user:intgroup:String使用联合主键 在Java中没有对应的类   Hibernate映射文件是: <hibernate mapping auto import= true default lazy= false > <class name= net ideawu User table= User > <cache usage= read write /> <id name= id column= id >  <generator class= native /> </id> <property name= name column= name /> <property name= password column= password />  <set name= groups table= UserGroup cascade= save update lazy= false >   <key column= user />  <element column= `group` type= string /> </set> </class></hibernate mapping> 

  一切的身份验证交给一个继承HandlerInterceptorAdapter的类来做:

   import springframework web servlet handler HandlerInterceptorAdapter;import springframework web util UrlPathHelper;import springframework util AntPathMatcher;import springframework util PathMatcher; public class AuthorizeInterceptor extends HandlerInterceptorAdapter  private UrlPathHelper urlPathHelper = new UrlPathHelper();  private PathMatcher pathMatcher = new AntPathMatcher();  private Properties groupMappings; /** * Attach URL paths to group */  public void setGroupMappings(Properties groupMappings)   this groupMappings = groupMappings;  public boolean preHandle(HttpServletRequest request HttpServletResponse response Object handler) throws Exception   String url = urlPathHelper getLookupPathForRequest(request);  String group = lookupGroup(url);  // 找出资源所需要的权限 即组名   if(group == null)    // 所请求的资源不需要保护    return true;     // 如果已经登录 一个User实例被保存在session中   User loginUser = (User)request getSession() getAttribute( loginUser );   ModelAndView mav = new ModelAndView( system/authorizeError );  if(loginUser == null)   mav addObject( errorMsg 你还没有登录! );   throw new ModelAndViewDefiningException(mav);  else   if(!loginUser getGroups(ntains(group))     mav addObject( errorMsg 授权失败! 你不在 <b> + group + </b> 组! );    throw new ModelAndViewDefiningException(mav);     return true;     /* * 查看   springframework web servlet handler AbstractUrlHandlerMapping lookupHandler()  * Ant模式的最长子串匹配法  */ private String lookupGroup(String url)   String group = groupMappings getProperty(url);   if (group == null)    String bestPathMatch = null;   for (Iterator it = this groupMappings keySet(erator();it hasNext();)     String registeredPath = (String) it next();    if (this pathMatcher match(registeredPath url) && (bestPathMatch == null || bestPathMatch length() <= registeredPath length()))      group = this groupMappings getProperty(registeredPath);     bestPathMatch = registeredPath;           return group;   

  下面我们需要在Spring的应用上下文配置文件中设置: <bean id= authorizeInterceptor class= net ideawu AuthorizeInterceptor >  <property name= groupMappings >  <value>   <! Attach URL paths to group >    /admin/*=admin  </value> </property></bean><bean id= simpleUrlHandlerMapping class= springframework web servlet handler SimpleUrlHandlerMapping >  <property name= interceptors >   <list>   <ref bean= authorizeInterceptor /> </list> </property> <property name= mappings >   <value>   /index do=indexController /browse do=browseController /admin/removeArticle do=removeArticleController   </value> </property></bean> 

cha138/Article/program/Java/ky/201311/28877

相关参考

知识大全 基于Spring框架的WebSphere应用开发

基于Spring框架的WebSphere应用开发  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 Spring MVC框架的高级配置(下)

SpringMVC框架的高级配置(下)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  上文介绍S

知识大全 PB用户权限管理[1]

PB用户权限管理[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  方案一多用户多用户方法 &

知识大全 PB用户权限管理[2]

PB用户权限管理[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  动态菜单的实现可以分为以下

知识大全 SSH集成框架下真正实现Spring AOP拦截功能

SSH集成框架下真正实现SpringAOP拦截功能  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 经典的用户权限管理数据结构分析设计

  实现业务系统中的用户权限管理设计篇  B/S系统中的权限比C/S中的更显的重要C/S系统因为具有特殊的客户端所以访问用户的权限检测可以通过客户端实现或通过客户端+服务器检测实现而B/S中浏览器是每

知识大全 Oracle用户权限表的管理方法

Oracle用户权限表的管理方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  我们将通过介绍命

知识大全 cvs用户权限管理的一种最简单的实现

  目标假设wink系统有一个haishion用户我们有个开发人员neo和smith这个土人都需要可读可写还有一个oracle用户只能看    前提安装完cvsnt配置advanced里不选择usel

知识大全 实现一个简单的struts和spring框架

  apache终于发布了struts的正式版strutsGA所以我也该从webwork迁移至strutsstruts基本上就是webwork的翻版所以迁移过程倒是很简单只需要修改下配置文件和一些包名

知识大全 Spring框架概述

Spring框架概述  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  SPRING框架——由来和发