Shiro 集成Spring

1. 环境搭建

1.1. 依赖

1
2


1.2. 配置Spring

web.xml 配置监听器,初始化Spring容器

1
2
3
4
5
6
7
8
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

1.3. 配置SpringMVC

web.xml 配置DispatcherServlet,以及常用的Filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

springmvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mv="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="demo.shiro.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>
</beans>

1.4. 配置Shiro的Filter

先在web.xml中,配置DelegatingFilterProxy

1
2
3
4
5
6
7
8
9
10
11
12
13
<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>
</filter-mapping>

在applicationContext.xml中注册Bean

1
2


1.5. 注册Shrio相关的Bean

编辑 applicationContext.xml

1
2
3
4
<!-- 配置CacheManager -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>

再添加ehcache的jar和配置文件ehcache.xml

1.6. 配置Realm

1
2
3
<!-- 配置realm,实际是实现了Realm接口的bean -->
<bean id="jdbcRealm" class="demo.shiro.realm.ShiroRealm">
</bean>

1.7. 配置LifecycleBeanPostProcessor

可以自动调用在Shiro Bean的生命周期方法

1
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

panchaoxin wechat
关注我的公众号
支持一下