這是applicationContext.xml裏的配置,可是我是將有關shiro的配置單獨放在一個xml文件中(application_shiro.xml),我我的以爲這樣更容易區分:java
<?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">
<!--第一步:直接配置一個 securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--剛配置時,先把這條註釋掉,等後面寫了MyRealm.java時,再把它的註釋去掉,由於若是沒有去掉就會在
tomcat開啓時報一個錯誤 -->
<!--<property name="realm" ref="myRealm" /> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- 第三步:把請求路徑攔截以後的處理 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--shiro整個的處理,都由securityManger指定和決定 -->
<property name="securityManager" ref="securityManager" />
<!-- loginUrl==>若是登陸成功,跳轉到哪一個頁面,或者執行哪一個請求 -->
<property name="loginUrl" value="/login.jsp" />
<!-- 驗證經過執行的請求或者跳轉 -->
<property name="successUrl" value="/home.jsp" />
<!-- 驗證不經過執行的請求或者跳轉 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp" />
<!-- 具體的攔截路徑和攔截的方式和角色和權限的範圍 -->
<property name="filterChainDefinitions">
<value>
</value>
</property>
</bean>
</beans>
接着配置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>
</filter-mapping>
<!-- 還有一個地方要注意,由於我是單獨把shiro寫在一個xml文件中,新加了一個文件,因此要添加路徑 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:application_shiro.xml
</param-value>
</context-param>
而後再是spring_mvc.xml(添加註解功能):web
<!-- 配置啓用Shiro的註解功能 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>spring
而後開啓
配置文件完成,開啓tomcat,成功開啓(出現如圖所示的紅線部分,說明配置shiro成功)apache