集成步骤: 创建动态web,也可直接创建maven项目(本章采用动态web举例说明,都一样)
1.创建web,添加jar包
这里是把用到的jar包都添加上了
2. web.xml中的配置
<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>
<servlet>
<servlet-name>spring
</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring
</servlet-name>
<url-pattern>/
</url-pattern>
</servlet-mapping>
3.src 下创建applicationContext.xml,web-inf下创建spring-servlet.xml并写如下内容
<context:component-scan
base-package="com.dw.shiro"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
4. 创建user.jsp,并访问user.jsp能访问则说明spring mvc搭建好了
5.web.xml中配置shiroFilter
<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>
6.spring中配置shiroFilter bean,在applicationContent.xml中配置,并遭src下创建com.dw.shiro.realms包,创建ShiroRealm文件,实现Realm
<bean id="securityManager"
class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<property name="realm" ref="jdbcRealm" />
</bean>
<bean id="cacheManager"
class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile"
value="classpath:ehcache.xml" />
</bean>
<bean id="jdbcRealm" class="com.dw.shiro.realms.ShiroRealm">
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<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>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/list.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
# everything else requires authentication:
/** = authc
</value>
</property>
</bean>
7. 注意添加ehcache jar包和ehcache.xml ,xml中内容如下
<ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="authorizationCache"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<cache name="authenticationCache"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<cache name="shiro-activeSessionCache"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true">
</cache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
</ehcache>
8.运行login.jsp 发现可以被访问,其他页面访问不了,且其他页面跳转到login.jsp页面。 (这里看spring中最后的配置即可明白)
这里我创了两个页面举例,一个login.jsp 一个user.jsp 且访问login.jsp时候,访问user.jsp页面时候,始终跳转到login.jsp页面 就是因为配置中login.jsp可以被匿名访问 anon 其他页面必须认证才可以访问,要认证必须先登录
补充:
url匹配模式: url匹配顺序: