Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证、授权、加密、会话管理、与Web集成、缓存等功能。我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是靠查询数据获取列表拼接展示的,还有的是及时的判断权限的问题的,现在有了Shiros了,我们就可以统一的进行设置权限问题,Shrios的实现也是很简单的,下面让我们来看看具体实现步骤

web.xml配置

  • 因为我们是与spring进行集成的,而spring的基本就是web项目的xml文件。所以我们在web.xml中配置shiros的过滤拦截。正常情况下,我们需要将shiro的filter配置在所有的filter前面,当然和encodingFilter这个filter是不区分前后的。因为两者互相不影响的。

    <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
    <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由servlet container管理 -->
    <param-name>targetFilterLifecycle</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter> <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
  • 好了,到这里shiro就集成到spring项目里面去了。What?对的,你妹看错,就这么简单,shiro就这一步就集成到项目里了。集成式集成了,但是想要完整的实现效果当然我们还是需要继续的往下配置的。在这里请记住我这里的shiroq过滤器的名字叫shiroFilter(后面有用的)。下面我们的shiro需要到spring的配置文件application.xml文件里去配置,在我的项目的我的spring配置文件是spring-service.xml。而在spring-service.xml中又引入了spring-shiro.xml,也就是说最后shiro的配置是配置在spring-shiro.xml文件中。 
    最终源码在最后下载
  • spring-shiro.xml

    • 这里我们将来看看spring-shiro.xml的配置,这里我采取倒叙的方式讲解,我觉的倒叙更加的有助于我们理解代码。首先我们还记得在web.xml中配置的那个filter吧,名字shiroFilter,对spring-shiro.xml配置文件就是通过这个filter展开的。首先我们在web.xml配置的过滤器实际上是配置ShiroFilterFactoryBean,所以在这里需要将ShiroFilterFactoryBean定义为shiroFilter
    • <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
      <!-- Shiro的核心安全接口,这个属性是必须的 -->
      <property name="securityManager" ref="securityManager"/>
      <!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录下的"/login.html"页面 -->
      <property name="loginUrl" value="/login.html"/>
      <!-- 登录成功后要跳转的连接 -->
      <property name="successUrl" value="/index.html"/>
      <!-- 用户访问未对其授权的资源时,所显示的连接 -->
      <!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp,然后用[玄玉]登录后访问/admin/listUser.jsp就看见浏览器会显示unauthor.jsp -->
      <property name="unauthorizedUrl" value="/login.html"
      />
      <!-- Shiro连接约束配置,即过滤链的定义 -->
      <!-- 此处可配合我的这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 -->
      <!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
      <!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->
      <!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
      <property name="filterChainDefinitions">
      <value>
      /statics/**=anon
      /login.html=anon
      /sys/schedule.html=perms[sys:schedule:save]
      /sys/login=anon
      /captcha.jpg=anon
      /**=authc
      </value>
      </property>
      </bean>
      • 具体的上面的代码注释已经解释的很清楚了,在这里主要讲解下filterChainDefinitions里面的设置属性。里面的value就是我们控制的页面权限设置。filterChainDefinitions的原则是按顺序查找一旦查找到符合的页面要求就不在继续查找了。所以我们需要将有通配符的页面设置在最后。上面配置中有/sys/schedule.html=perms[sys:schedule:save] 
        意思就是说访问schedule.html这个页面前提是你得有sys:schedule:save这个权限。至于这个权限在哪里配置。在这里先透露一下。在Realm中获取
      • 在上面的配置我们securityManager属性是shiro 安全核心配置接口,这里需要我们自己填写,这里的配置就是需要我们实现我们的认证,因为不同的项目我们认证权限肯定是不一样的。所以这也是shiro给我们唯一为数不多的代码编写的接口,我们只需要在这接口提供我们自己的认证和角色权限分配就行了。
      • <!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->
        <!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->
        <!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->
        <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userRealm"/>
        </bean>
      • 那么这里有出现了一个realm,这个realm就是我们实现权限和认证的地方。我们只需要通过spring将我们的实现类指定为realm即可
      • <bean id="userRealm" class="io.renren.shiro.UserRealm"/>
      • 在讲UserRealm之前我们先来看看shir默认的realm逻辑是咋样的
        • 通常我们只需要继承AuthorizingRealm(授权),因为AuthorizingRealm里面继承了AuthenticatingRealm(认证),所以我们只需要继承AuthorizingRealm(授权),我们就可以重写授权和认证两个方法了,这两个方法里面就实现权限管理操作。
          • 首先来看看在认证登录中我们有哪些值得注意的地方

          • doGetAuthenticationInfo中实现登录认证出现的几种异常

            • UnknownAccountException:获取的user为空
            • LockedAccountException :此用户被锁住了
            • IncorrectCredentialsException : 密码不正确(建议提示为 用户名或密码错误。安全考虑)
            • ExcessiveAttemptsException : 密码错误次数太多(现在很多网站上都有相关的操作)
            • 最后通过用户名+明文密码+Reaml中的getName进行用户信息组装
          • 登录认证就这几点注意,其次就是权限分配了,doGetAuthorizationInfo(授权),在doGetAuthorizationInfo里我们通过PrincipalCollection这个身份集合,当我们只配置了一个Reaml的时候我们可以通过PrincipalCollection中的getPrimaryPrincipal方法获得刚刚传入的Reaml(用户名)就行了,但是当我们配置了多个Reaml的时候可以通过PrincipalCollection中的getRealmNames获取所有的Reaml的用户名就行了。

          • 然后通过用户名去数据库获取权限菜单。最后返回一个带有角色和权限的 SimpleAuthorization的信息,意思就是一下角色具有哪些权限。如果就一个角色的时候也可以不指定角色,分别通过setStringPermissions(指定权限)+setRoles(指定角色)

          • 到这里shiro的配置就完成了。

          • 另外还有一点shiro的配置是处理shiro的生命周期和shiro的注解的启用的,这里就不解释了,直接上代码

          • <!-- Shiro生命周期处理器 -->
            <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- AOP式方法级权限检查 -->
            <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
            <property name="proxyTargetClass" value="true" />
            </bean>
            <!-- 开启shiro注解 -->
            <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
            <property name="securityManager" ref="securityManager"/>
            </bean>

            spring-shiro.xml源码

          • <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 继承自AuthorizingRealm的自定义Realm,即指定Shiro验证用户登录的类为自定义的UserRealm.java -->
            <bean id="userRealm" class="io.renren.shiro.UserRealm"/> <!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->
            <!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->
            <!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->
            <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <property name="realm" ref="userRealm"/>
            </bean> <!-- Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 -->
            <!-- Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 -->
            <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <!-- Shiro的核心安全接口,这个属性是必须的 -->
            <property name="securityManager" ref="securityManager"/>
            <!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录下的"/login.html"页面 -->
            <property name="loginUrl" value="/login.html"/>
            <!-- 登录成功后要跳转的连接 -->
            <property name="successUrl" value="/index.html"/>
            <!-- 用户访问未对其授权的资源时,所显示的连接 -->
            <!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp,然后用[玄玉]登录后访问/admin/listUser.jsp就看见浏览器会显示unauthor.jsp -->
            <property name="unauthorizedUrl" value="/login.html"
            />
            <!-- Shiro连接约束配置,即过滤链的定义 -->
            <!-- 此处可配合我的这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 -->
            <!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
            <!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->
            <!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
            <property name="filterChainDefinitions">
            <value>
            /statics/**=anon
            /login.html=anon
            /sys/schedule.html=perms[sys:schedule:save]
            /sys/login=anon
            /captcha.jpg=anon
            /**=authc
            </value>
            </property>
            </bean>
            <!-- Shiro生命周期处理器 -->
            <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- AOP式方法级权限检查 -->
            <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
            <property name="proxyTargetClass" value="true" />
            </bean>
            <!-- 开启shiro注解 -->
            <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
            <property name="securityManager" ref="securityManager"/>
            </bean>
            </beans>

            转载地址:

            https://blog.csdn.net/u013132051/article/details/54949632

Spring项目集成ShiroFilter简单配置的更多相关文章

  1. Spring项目集成ShiroFilter简单实现权限管理

    Shiros是我们开发中常用的用来实现权限控制的一种工具包,它主要有认证.授权.加密.会话管理.与Web集成.缓存等功能.我是从事javaweb工作的,我就经常遇到需要实现权限控制的项目,之前我们都是 ...

  2. javaweb+spring 项目集成异常的处理

    在web项目开发中,一个系统应该要考虑到异常情况的处理,并且应该当异常发生时应该需要记录相应的异常日志,对于用户而言则不能直接抛出异常,需要考虑到用户的体验: 以下就介绍基于spring框架的基础上的 ...

  3. Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权

    Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...

  4. 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题

    如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowired 注入的bean都是null,无法注入mapper.搜了半天 ...

  5. springMVC+spring+MyBatis(SSM)的简单配置

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中: Spring是一 ...

  6. Spring dbcp连接池简单配置 示例

    一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...

  7. Spring项目使用Junit4测试配置

    我们经常要写junit测试,在不启动整个web项目的情况下,测试自己的service实现或者是dao实现,我们来充分利用下junit4的强大功能. 1.junit4的测试类 import java.u ...

  8. IntelliJ IDEA 导入新项目以后的简单配置

    首先,配置Maven. 然后,配置Git. 选择自己调试要用的默认浏览器. 进行Edit Configurations 配置: 转载请注明出处! http://www.cnblogs.com/libi ...

  9. quartz Web项目基础最简单配置

    web方面的quartz 配置资料,从网上搜索出来的很难找到完整可用的代码样例.自己上传一个. IDE:Intellij tomcat jdk1.7 quartz 2.1.5 这里下载: http:/ ...

随机推荐

  1. Go语言类型(布尔、整型、数组、切片、map等)

    1.基本类型 布尔类型:bool 注意:布尔类型不能接受其他类型的赋值,不支持自动或强制的类型转换. 整型:int8.byte(uint8).int16.int.uint.uintptr int.ui ...

  2. Android ProgressDialog 简单实用

    ProgressDialog progressDialog; @SuppressLint("HandlerLeak") Handler handler1 = new Handler ...

  3. 五一培训 清北学堂 DAY2

    今天还是冯哲老师的讲授~~ 今日内容:简单数据结构(没看出来简单qaq) 1.搜索二叉树 前置技能 一道入门题在初学OI的时候,总会遇到这么一道题.给出N次操作,每次加入一个数,或者询问当前所有数的最 ...

  4. centos6.8下安装dc2012

    前言 centos6.8系统中安装synopsys公司的design compiler 2012. 流程 1.请掌握必要的linux知识,否则你将获得成吨的困难. linux系统:centos 6.8 ...

  5. bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...

  6. Win10报错0x800f0906

    在安装适用于Linux的Windows子系统(Beta)的时候,有选中开发人员模式这一步设置->更新和安全->针对开发人员->选中开发人员模式 如果报错0x800f0906 那是因为 ...

  7. ueditor 编辑器常用方法

    官方文档http://fex.baidu.com/ueditor/ 简单实现工具栏调用: //实现方法 ue.getDialog("工具名称"); //工具名称可以参考如下: to ...

  8. windows 设置ipsec防火墙

    windows server 推荐使用ipsec修改防火墙设置,默认防火墙需要手动导入导出.wfw文件,需要手动添加单条规则,维护麻烦,推荐关闭,使用ipsec管理 以下是线上防火墙配置,可参照业务环 ...

  9. Linux iptables设置

    先举例子说明,若服务器网卡: eth0 10.10.0.100 eth0:0 10.10.0.200 eth0:1 10.10.0.201 eth0:2 10.10.0.202 只允许10.10.0. ...

  10. 【BZOJ4008】[HNOI2015]亚瑟王(动态规划)

    [BZOJ4008][HNOI2015]亚瑟王(动态规划) 题面 BZOJ 洛谷 题解 设\(f[i][j]\)表示前\(i\)张卡中有\(j\)张被触发的概率. 分两种情况转移,即当前这张是否被触发 ...