关于权限控制,一开始感觉比较难,后来先是接触了Spring Security 学起来也比较吃力,再是学习了Shiro,感觉简单很多。

总体来说这些框架,主要做了两个事情

Authentication身份认证/登录,验证用户是不是拥有相应的身份;

Authorization授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限;

还做了一些事情,我也没有深入,主要就这么。

从以下配置文件可以看出。

<beans:bean id="myFilter" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">

  1.         <beans:property name="authenticationManager" ref="autheticationManager"></beans:property>
  2.         <beans:property name="accessDecisionManager" >
  3.             <beans:bean class="org.springframework.security.access.vote.AffirmativeBased">
  4.                 <beans:property name="decisionVoters">
  5.                     <beans:list>
  6.                         <beans:bean class="org.springframework.security.access.vote.RoleVoter" />
  7.                         <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
  8.                     </beans:list>
  9.                 </beans:property>
  10.             </beans:bean>
  11.         </beans:property>
  12.         <!-- resourceService在applicationContext*.xml中或注解定义 -->
  13.         <beans:property name="securityMetadataSource" ref="securityMetadataSource" />
  14.     </beans:bean>

这里有一个authenticationManager是做身份认证的,有一个accessDecisionManager是做授权管理的。还有一个是securityMetadataSource这里主要做的从数据库取出资源与角色的关系,为第二个授权做准备,授权又是根据用户的身份找出他的角色,根据角色判断是否有访问资源的权限。

先介绍这个 securityMetadataSource这里需要实现FilterInvocationSecurityMetadataSource接口,大致如下:

/**

  1.      * resourceMa
  2.      * resourceMap 存储的资源为<Url,Collection<ConfigAttribute>>,其中Collection<ConfigAttribute>为资源对应的角色集合,其构造方式为new SecurityConfig("ROLE_" + role.getRoleId().toString())
  3.      * resourceMap 里面存储的内容为<1URL,对应得角色的集合>
  4.      * 加载所有的资源与角色的关系
  5.      */
  6.     public void init() throws Exception {
  7.         resourceMap = new HashMap<String,Collection<ConfigAttribute>>();
  8.         List<Resource> resources = resourceDao.findAll();
  9.         
  10.         for(Resource item:resources) {
  11.         String url = item.getUrlpath();
  12.             if(url != null) {
  13.                  List<Role> roles = roleDao.getRolesByResourceId(item.getResourceid());
  14.                 if(roles !=null && roles.size()>0) {
  15.                     if(resourceMap.containsKey(item.getUrlpath())) {
  16.                         Collection<ConfigAttribute> old = resourceMap.get(item.getUrlpath());
  17.                         old.addAll(listToCollection(roles));
  18.                     } else {
  19.                         resourceMap.put(url, listToCollection(roles));
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.     }
  25.     
  26.     /**
  27.      * 将角色转化为ConfigAttribute
  28.      * @param roles
  29.      * @return
  30.      */
  31.     private Collection<ConfigAttribute> listToCollection(Collection<Role> roles) {
  32.         Collection<ConfigAttribute> list = new ArrayList<ConfigAttribute>();
  33.         for(Role role :roles) {
  34.             ConfigAttribute configAttribute = new SecurityConfig("ROLE_"+role.getRoleid().toString());
  35.             //list.add(new SecurityConfig("ROLE_" + role.getRoleid().toString()));
  36.             list.add(configAttribute);
  37.         }
  38.         return list;
  39.         
  40.     }
  41.  
  42.     /**
  43.      * 将资源与权限的对应关系Map转化为Security需要的Collection
  44.      */
  45.     @Override
  46.     public Collection<ConfigAttribute> getAllConfigAttributes() {
  47.         Set<ConfigAttribute> allAttributes = new HashSet<ConfigAttribute>();
  48.         for(Map.Entry<String, Collection<ConfigAttribute>>  entry:resourceMap.entrySet()) {
  49.             allAttributes.addAll(entry.getValue());
  50.         }
  51.         return allAttributes;
  52.     }  //返回所请求资源所需要的权限   public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException { String requestUrl = ((FilterInvocation) object).getRequestUrl(); System.out.println("requestUrl is " + requestUrl); if(resourceMap == null) { try { init(); } catch (Exception e) { e.printStackTrace(); } } return resourceMap.get(requestUrl); } 

上面前几个方法是在程序启动的时候执行,最后一个方法是在你要访问某个资源的时候自动调用的最终返回的是 Collection<ConfigAttribute>  权限集合。

accessDecisionManager授权管理,根据上面返回的权限集合,和用户登录存储的用户权限进行对比判断用户是否有访问权限,自己写授权管理的话需要实现AccessDecisionManager 接口,如下:

public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {

  1.         if(configAttributes == null) {
  2.             return;
  3.         }
  4.         //所请求的资源拥有的权限(一个资源对多个权限)
  5.         Iterator<ConfigAttribute> iterator = configAttributes.iterator();
  6.         while(iterator.hasNext()) {
  7.             ConfigAttribute configAttribute = iterator.next();
  8.             //访问所请求资源所需要的权限
  9.             String needPermission = configAttribute.getAttribute();
  10.             System.out.println("needPermission is " + needPermission);
  11.             //用户所拥有的权限authentication
  12.             for(GrantedAuthority ga : authentication.getAuthorities()) {
  13.                 if(needPermission.equals(ga.getAuthority())) {
  14.                     return;
  15.                 }
  16.             }
  17.         }
  18.         //没有权限让我们去捕捉
  19.         throw new AccessDeniedException(" 没有权限访问!");

}

authenticationManager身份认证,主要包含登陆这些,框架一般会给我们提供form表单提交的处理接口,我们需要实现接口和配置一些相关配置。

<http auto-config="true" access-denied-page="/accessDenied.jsp" use-expressions="true">

  1.         <form-login login-page="/index.jsp" login-processing-url="/j_spring_security_check"
  2.         always-use-default-target="true" authentication-failure-url="/index.jsp?login_error=true"
  3.         default-target-url="/frame.jsp"
  4.         />
  5.                 
  6.         <logout logout-success-url="/index.jsp"/>
  7.         <intercept-url pattern="/style/**" filters="none" />
  8.         <intercept-url pattern="/js/**" filters="none" />
  9.          <intercept-url pattern="/index.jsp" filters="none" />
  10.         <intercept-url pattern="/login.action" filters="none"/>
  11.         <intercept-url pattern="/frame.jsp" access="isAuthenticated()"/>
  12.         <anonymous />
  13.         <session-management invalid-session-url="/accessDenied.jsp">
  14.             <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
  15.         </session-management>
  16.         <!-- 将自己的过滤器加入到过滤器链中, 放在FILTER_SECURITY_INTERCEPTOR之前-->
  17.         <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR"/>
  18.     </http>
  19.     
  20.     <authentication-manager alias="autheticationManager">
  21.         <!-- 使用自定义UserDetailsService -->
  22.         <authentication-provider user-service-ref="userService"/>

</authentication-manager>

以上是Spring Security的配置,还需要在userService 登录验证里面实现UserDetailsService 接口。Shiro实现方式如下 :

Subject currentUser = SecurityUtils.getSubject();

  1.         System.out.println(EncryptUtils.encryptMD5(user.getPassword()));
  2.         UsernamePasswordToken token = new UsernamePasswordToken(
  3.                 user.getUsercode(), EncryptUtils.encryptMD5(user.getPassword()));
  4.         token.setRememberMe(true);
  5.         try {
  6.             currentUser.login(token);
  7.         } catch (AuthenticationException e) {
  8.             modelView.addObject("message", "login errors");
  9.             modelView.setViewName("/login");
  10.             e.printStackTrace();
  11.             

}

accessDecisionManager

权限控制框架Spring Security 和Shiro 的总结的更多相关文章

  1. 了解权限控制框架shiro 之实际应用.

    Apache Shiro 1.权限控制分为 a.粗粒度 URL 级别权限控制     b.细粒度方法级别权限控制 2.使用shiro进行权限控制主要有四种主要方式 : a. 在程序中 通过 Subje ...

  2. 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示

    1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...

  3. Spring Security和Shiro的比较和使用

    https://blog.csdn.net/it_java_shuai/article/details/78054951 Spring Security和Shiro的比较和使用 2017年09月21日 ...

  4. Spring Boot + Spring Cloud 实现权限管理系统 (Spring Security 版本 )

    技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro 也足够好用并且简单,但对于 Spring 官方主推的安全框架 Spring Security,用户群也是甚大的,所以我们这 ...

  5. Flex-Security权限控制框架

    转自:http://code.google.com/p/flex-security/ flex UI组件权限控制框架 一.快速开始 1) 下载并添加flex_security.swf在你的flex l ...

  6. WPF权限控制框架——【4】抛砖引玉

    写第一篇"权限控制框架"系列博客是在2021-01-29,在这不到一个月的时间里,收集自己零碎的时间,竟然写出了一个"麻雀虽小,五脏俱全"的权限控制框架:对于一 ...

  7. 权限控制框架Shiro简单介绍及配置实例

    Shiro是什么 http://shiro.apache.org/ Apache Shiro是一个非常易用的Java安全框架,它能提供验证.授权.加密和Session控制.Shiro非常轻量级,而且A ...

  8. shiro权限控制(一):shiro介绍以及整合SSM框架

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  9. 第十九章 动态URL权限控制——《跟我学Shiro》

    目录贴:跟我学Shiro目录贴 用过Spring Security的朋友应该比较熟悉对URL进行全局的权限控制,即访问URL时进行权限匹配:如果没有权限直接跳到相应的错误页面.Shiro也支持类似的机 ...

随机推荐

  1. ORACLE case when then

    Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...

  2. iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点

    1.首先建立这个三个参数 @property (nonatomic,strong)CBCentralManager * manager; @property (nonatomic,strong)CBP ...

  3. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  4. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...

  5. 关于eclipse(64位)下aptana插件安装报错问题解决

    最近一直没有写过js,换了新电脑以后,eclipse下的aptana插件也没有装过,这几天要写js想重新把aptana装上,但是不知怎的,link方式.在线安装方式还有离线包下载下来利用eclipse ...

  6. JS 操作日期

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  7. 关于mysqli 连接数不能正确释放的解决方案

    /** * 析构函数 */ //解决重复链接的问题 private $db_handler = null; function __destruct() { Log::logWrite($this-&g ...

  8. xsoup,Jsoup

    Xsoup 0.2.0 Xsoup 的详细介绍:请点这里 Xsoup 的下载地址:请点这里 https://github.com/code4craft/xsoup http://www.oschina ...

  9. Summer Holiday(强联通入度最小点)

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. MongoDB(三)mongoDB下载和安装

    软件下载 下载mongodb最新的包:http://www.mongodb.org/downloads 下载mongodb可视化界面,mongoVUE:http://download.csdn.net ...