SpringMVC与Shiro快速整合:
好久没有重新整合Shiro框架了,为了方便以后参考查阅,特此将步骤分享出来,共同学习。
一、配置XML文件:
1、到相应的pom.xml中添加shiro相关依赖,注意这里没有版本号,因为在父模块中已经集中定义依赖版本号。
<!-- 添加Apache Shiro 依赖关系 -->
<!-- shiro核心包 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<!-- 添加shiro web支持 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</dependency>
<!--shiro cas单点登录-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
</dependency>
<!-- 支持shiro spring集成 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
</dependency>
2、添加shiro拦截器到web.xml:这里要注意filter-name的命名,下面配置shiro.xml需要用到。
<!-- 配置 Shiro 的 Filter-->
<filter>
<description>shiro 权限拦截</description>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<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>
3、配置shiro.xml文件
注:这里我们需要在service层的com.mz.shiro目前下新建AuthRealm、AuthFilter文件,下面会细讲。
<!-- 自定义Realm -->
<bean id="authRealm" class="com.mz.shiro.AuthRealm"></bean>
<!-- 自定义 filter -->
<bean id="authFilter" class="com.mz.shiro.AuthFilter" />
注:如果需要在方法上使用注解,声明调用该方法需要什么权限,那就要在spring配置文件中声明,开启shiro注解。
<!-- 开启shiro框架注解支持 -->
<bean id="lifecycleBeanPostProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<!-- 必须使用cglib方式为Action对象创建代理对象 -->
<property name="proxyTargetClass" value="true" />
</bean>
<!--权限注解的advisor -->
<bean class="org.apache.shiro.spring.security.interceptor
.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
注:设置session的失效时长,可有可无。
<!-- sessionManager -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- 设置session的失效时长 -->
<property name="globalSessionTimeout" value="600000"/>
<!-- 删除失效的session -->
<property name="deleteInvalidSessions" value="true"/>
</bean>
注:安全管理器用于管理所有的Subject,如上面配置的authRealm、sessionManager
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt
.DefaultWebSecurityManager">
<property name="realm" ref="authRealm"/>
<property name="sessionManager" ref="sessionManager"/>
</bean>
注:最后就是WEB过滤器了,这里的shiroFilter要与上面web.xml中的filter-name保持一致。
<!-- Shiro 的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 装配 securityManager -->
<property name="securityManager" ref="securityManager" />
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->
<property name="unauthorizedUrl" value="/login.jsp"/>
<!-- 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,
不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面 -->
<property name = "loginUrl" value="/login.jsp" />
<!-- 登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,
则在登录自动跳转到那个需要登录的页面。不跳转到此 -->
<property name="successUrl" value="/" />
<property name="filters">
<map>
<!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中-->
<entry key="authc" value-ref="authFilter" />
</map>
</property>
<!-- 就是需要验证的地址的列表,常用的包含anon、authc、perms、roles、user、logout。 -->
<property name="filterChainDefinitions">
<!-- 对静态资源设置匿名访问 -->
<value>
<!-- 必须通过身份认证方可访问,身份认证的url必须和过虑器中指定的loginUrl一致 -->
/login.jsp=anon
/error/** = anon
<!-- 无权访问页面 -->
/user/** = anon
<!-- 除了上面的那些,其他的所有都要经过拦截器,所有url都可以匿名访问 -->
/** = authc
</value>
</property>
</bean>