springmvc+shiro认证框架配置
1,在web.xml中配置fiter,如下所示
<!-- shiro的filter -->
<!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spring容器中的bean和filter关联起来 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 设置true由servlet容器控制filter的生命周期 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置spring容器filter的bean id,如果不设置则找与filter-name一致的bean-->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2,上面配置了shiroFilter,那么在spring-shiro.xml中要配置shirofilter bean
<!-- web.xml中shiro的filter对应的bean -->
<!-- Shiro 的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 -->
<property name="loginUrl" value="/login" />
<!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 -->
<!--<property name="successUrl" value="/index.jsp"/>-->
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->
<!--<property name="unauthorizedUrl" value="/sysPer/noperm" />-->
<!--自定义filter配置 -->
<!--<property name="filters">-->
<!--<map>-->
<!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中-->
<!--<entry key="authc" value-ref="formAuthenticationFilter" />-->
<!--</map>-->
<!--</property>-->
<!-- 过虑器链定义,从上向下顺序执行,一般将/** 放在最下边 -->
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/styles/easyui/** = anon
/scripts/easyui/** = anon
/styles/** = anon
/images/** = anon
<!-- 请求 logout地址,shiro去清除session-->
/logout = logout
<!--商品查询需要商品查询权限 ,取消url拦截配置,使用注解授权方式 -->
<!-- /items/queryItems.action = perms[item:query] -->
<!--/sysuser/deleteUser = perms[user:delete]-->
<!-- 配置记住我或认证通过可以访问的地址 -->
<!--/index.jsp = user-->
<!-- /** = authc 所有url都必须认证通过才可以访问-->
/login = anon
/** = authc
</value>
</property>
</bean>
3,在spring-shiro.xml中添加securityManager和自定义realm,ecacheManager的相关配置
<!-- securityManager安全管理器 -->
<!-- realm -->
<bean id="customRealm" class="com.unisits.zngkpt.framework.privilegeframe.CustomeRealm">
<!-- 将凭证匹配器设置到realm中,realm按照凭证匹配器的要求进行散列 -->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!-- 凭证匹配器 -->
<bean id="credentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="1" />
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<!-- 配额securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm" />
<!-- 注入缓存管理器 -->
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>
3,在spring-shiro.xml中,开启shiro的注解支持
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
4,在spring-shiro.xml中配置shiro认证异常的页面
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException">/errorpage/refuse</prop> </props> </property> </bean>

浙公网安备 33010602011771号