本文延續「Struts2 + Spring 3 + MyBatis Maven 專案」,加入 Apache Shiro 1.2.1 實現認證授權,採用「先跑通、再深入」原則,記錄關鍵整合步驟與坑點。
<properties>
<shiro.version>1.2.1</shiro.version>
</properties>
<dependencies>
<!-- Shiro 核心 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- 既有 Struts2/Spring/MyBatis 依賴略 -->
</dependencies>
<!-- 1. Shiro 過濾器必須在 Struts2 之前 -->
<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. Spring ContextLoaderListener 載入所有 applicationContext-*.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 3. Struts2 過濾器 (次序在 Shiro 之後) -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
鐵律:
DelegatingFilterProxy(Shiro) 必須映射在StrutsPrepareAndExecuteFilter之前,否則 Shiro 無法攔截 Struts2 Action 請求,導致授權失效或無限重導向。
applicationContext-shiro.xml<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 1. 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="shiroDbRealm"/>
</bean>
<!-- 2. 自定義 Realm (需實作 AuthorizingRealm) -->
<bean id="shiroDbRealm" class="com.example.shiro.ShiroDbRealm"/>
<!-- 3. ShiroFilterFactoryBean 定義攔截鏈 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login_loginPage.do"/>
<property name="successUrl" value="/login_home.do"/>
<property name="unauthorizedUrl" value="/other_error.do"/>
<property name="filterChainDefinitions">
<value>
/login_loginPage.do = anon
/login_login.do = anon
/login_home.do = authc
/login_hello.do = authc
/t1/** = roles[aa], perms[aaa]
/t2/** = roles[bb], perms[baaa]
/t3/** = roles[dd]
</value>
</property>
</bean>
<!-- 4. 生命週期 BeanPostProcessor (啟用 Shiro 註解支援) -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 5. AOP 式方法級權限檢查 (需 @RequiresRoles/@RequiresPermissions) -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
</beans>
| 過濾器 | 含義 | 範例 |
|---|---|---|
anon | 匿名存取,無需登入 | /login/** = anon |
authc | 需認證 (登入) | /user/** = authc |
roles[role1,role2] | 需具備所有指定角色 | /admin/** = roles[admin,super] |
perms[perm1,perm2] | 需具備所有指定權限 | /data/** = perms[data:read,data:write] |
rest[user] | REST 風格 (自動對應 HTTP 方法) | /api/** = rest[user] |
port[8443] | 強制指定埠口 | /secure/** = port[8443] |