shiro 集成spring 配置 学习记录(一)

时间:2021-05-12 21:34:56

首先当然是项目中需要增加shiro的架包依赖:

 <!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

1、shiro 总体来说就是过滤器的集合,首先在项目的web.xml里增加shiro的filter配置:如下

 <!--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>

2、在项目的resource目录下 新增shiro的配置xml文件如:applicationContext-shiro.xml:

1)、增加 securityManager  的初始化;

2)、增加  shiroFilter  的配置, 注意 此配置文件中的bean  过滤器的 id  必须是 web.xml中的 filter-name值

3)、增加自定义的realm实现

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <description>Shiro安全配置</description> <!--此Bean 只是增加登录时的验证码处理,不是shiro必须的配置-->
<bean class="com.itzixi.web.utils.ItzixiCaptcha">
<property name="cacheManager" ref="shiroEhcacheManager"/>
<!-- 复用半小时缓存 -->
<property name="cacheName" value="itzixiCaptcha"/>
</bean> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm"></property>
</bean> <!-- 用户授权信息Cache, 采用EhCache -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:shiro/ehcache-shiro.xml"/>
</bean> <!-- 项目自定义的Realm -->
<bean id="shiroDbRealm" class="com.itzixi.web.shiro.ShiroDBRealm">
</bean> <!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器 -->
<property name="securityManager" ref="securityManager"/>
<!-- 默认的登陆访问url -->
<property name="loginUrl" value="/login.action"/>
<!-- 登陆成功后跳转的url -->
<property name="successUrl" value="/index.action"/>
<!-- 登录成功以后,没有权限访问的页面,会跳转到该url -->
<property name="unauthorizedUrl" value="/unauth.action"/> <property name="filterChainDefinitions">
<value>
<!--
anon 不需要认证
authc 需要认证
user 验证通过或RememberMe登录的都可以
-->
/** = authc
</value>
</property>
</bean>
</beans>

经过如上的配置 基本完成了shiro初始化集成。