第一步:在web.xml中配置shiroFilter,确保web项目中须要权限管理的URL均可以被shiro拦截过滤java
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>//这个是固定写法,也就是shiro包里的内容
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>//若是有其余filter-mapping都应放在该filter-mapping以后,已达到shiro是第一个对web请求进行拦截过滤的,
<filter-name>shiroFilter</filter-name>//这个filename应该和下一步配置中的java bean的id一致。
<url-pattern>/*</url-pattern>//这是shiro拦截请求的样式
<dispatcher>REQUEST</dispatcher>//请求
<dispatcher>FORWARD</dispatcher>//转发,都拦截
</filter-mapping>web
第二部:配置各类javabean,该文件通常为sping和shiro整合的文件,命名没有强求,可是该文件必定要配置到web.xml中的<context-param>标签中redis
如:<!-- 读取spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/spring.xml;
classpath*:config/spring-mybatis.xml;
classpath*:config/spring-shiro.xml;
</param-value>
</context-param>spring
而在spring-shiro.xml的配置才是shiro配置的重点apache
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">缓存
<!-- Shiro's main business-tier object for web-enabled applications -->安全
//配置shiro安全管理器
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- <property name="sessionManager" ref="sessionManager" /> -->
<property name="cacheManager" ref="cacheManager" />//此为安全管理器的缓存管理
<property name="realms">//配置realm
<list>
<ref bean="securityRealm" />//这个realm事ref的,所以该配置文件中能够找到其javabean
</list>
</property>
</bean>session
//自定义一个realm类,这个类通常继承AuthorizingRealm,而后写登陆认证和权限分配的方法,登陆时执行。mybatis
<bean id="securityRealm" class="com.credithc.workorder.manage.shiro.SecurityRealm" >
//这个finder是一个查询用户角色查询的接口,主要是给权限分配调用的app
<property name="securityUsersFinder" ref="securityUsersFinder" />
</bean>
<bean id="securityUsersFinder" class="com.credithc.workorder.manage.shiro.SecurityUsersFinder">
</bean>
<!-- Shiro主过滤器自己功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 Web应用中,Shiro可控制的Web请求必须通过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
//shiro的核心安全接口,这个属性是必须的。即前面配置的shiro安全管理器
<property name="securityManager" ref="securityManager" />
//要求登陆时的连接(登陆页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面
<property name="loginUrl" value="/main/loginIndex" />
//登陆成功后要跳转的链接
<property name="successUrl" value="/workorder/homePage" />
//用户访问未对其受权的资源时,所显示的链接
<property name="unauthorizedUrl" value="/main/noperms" />
<!--
<property name="filterChainDefinitions">
<value>
/** = anon
</value>
</property>
-->
<property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource" />
</bean>
<bean id="chainDefinitionSectionMetaSource"
class="com.credithc.framework.core.shiro.commons.ChainDefinitionSectionMetaSource">
<property name="securityUsersFinder" ref="securityUsersFinder" />
<property name="filterChainDefinitions">
<value>
/error.jsp=anon//不须要通过受权,登陆便可访问
/workorder/homePage=authc//须要登陆受权才能访问
<!--
/favicon.ico=anon
/**=sso,authc /WEB-INF/views/index.jsp=anon /static/**=anon /login=anon
/logout=anon /main=user /unauthorized=user /**=authc -->
</value>
</property>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
//将shiro和redis配置在一块儿,redis缓存
<bean id="redisManager"
class="com.credithc.framework.core.shiro.crazycake.RedisManager">
<property name="master" value="${redis.master}" />
<property name="host" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="expire" value="${redis.expire}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.databse}" />
</bean>
//缓存管理器
<bean id="cacheManager"
class="com.credithc.framework.core.shiro.crazycake.RedisCacheManager">
<property name="redisManager" ref="redisManager" />
</bean>
<!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类, 并在必要时进行安全逻辑验证 -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
</beans>
第三步:剩下的就是编写页面和后台代码
注:有关shiro的安全管理器,管理器里面要配置缓存管理器cacheManager。工单系统使用的缓存是redis,所以在缓存管理器中要配置redis管理器redisManager。redisManager里面要配置redis的参数,包括路径,ip,端口,超时时长等。
参考资料:http://www.oschina.net/p/shiro-redis
http://blog.csdn.net/chris_mao/article/details/49288251