书接上回 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml
继续在原有框架下 融合shiro ,具体shiro是啥 这里就不解释了,恩 反正功能挺强大的
本着先会用再深入的原则,还是尝试着将shiro融入框架中
0 首先上下这个项目的整体结构图
1 在导入shiro的jar包 在pom.xml中添加shiro的配置
... <shiro.version>1.2.1</shiro.version> ... <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> </dependencies>
2 在web.xml中导入 shiro的过滤器
<filter> <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> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
这里要说明一下
shiro的过滤器是前置过滤器,需要添加在struts2的前面,如果放在struts2之后会报错
然后是在spring的过滤配置中添加spring-shiro的配置文件
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext-*.xml </param-value> </context-param>
配置文件为applicationContext-shiro.xml, 因为这里用了通配符 所以不用修改
3 然后 添加spring-shiro的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans...> <description>Shiro安全配置 来源于: http://shiro.apache.org/spring.html </description> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="shiroDbRealm" /> </bean> <bean id="shiroDbRealm" class="lqb.shiro.ShiroDbRealm" /> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login_loginPage.do" /> <!-- 没有权限或者失败后跳转的页面 --> <property name="successUrl" value="/login_home.do" /> <property name="unauthorizedUrl" value="/other_error.do"/> <property name="filterChainDefinitions"> <value> /login_loginPage.do = anon /login_login.do = anon /login_home.do=authc /login_hello.do=authc /t1/**=roles[aa],perms[aaa] /t2/**=roles[bb],perms[baaa] /t3/**=roles[dd] </value> </property> </bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <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> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> </beans>
这里需要就是下 filterChainDefinitions 的value
key 是 对应的跳转路径 这里都是指定的struts2的跳转 可以匹配通配符 *
value 是对应的过滤权限
anon 不需要验证
authc 需要登录验证
roles[aa] 角色验证 中括号内为指定的角色
perms[aaa] 权限验证 中括号内卫指定的权限
4 添加shiro的缓存配置文件
<ehcache> <diskStore path="java.io.tmpdir/shiro-spring-sample"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <cache name="shiro-activeSessionCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="600"/> <cache name="org.apache.shiro.realm.SimpleAccountRealm.authorization" maxElementsInMemory="100" eternal="false" timeToLiveSeconds="600" overflowToDisk="false"/> </ehcache>