首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2EE

Struts 2 安全配置實戰:基於 web.xml 的角色訪問控制與 BASIC 認證

     編輯:J2EE

Web 應用安全的兩種路徑

Servlet 規範提供兩種保護資源方式:web.xml 宣告式配置(靈活、無需改碼)與 Java 硬編碼。Struts 2 建立於 Servlet 之上,自然繼承宣告式安全機制。

核心概念:用戶 vs 角色

  • 用戶:具體自然人或組織(如張三、李四);
  • 角色:抽象職務/權限集合(如職員、主管、經理)。一用戶可屬多角色。

Tomcat 實戰:定義用戶與角色

編輯 $CATALINA_HOME/conf/tomcat-users.xml:

<tomcat-users>
    <role rolename="tomcat"/>
    <role rolename="role1"/>
    <user username="tomcat" password="tomcat" roles="tomcat"/>
    <user username="both" password="tomcat" roles="tomcat,role1"/>
    <user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>

定義 2 角色、3 用戶,both 擁有雙重權限。

web.xml 安全約束完整配置

1. <security-constraint>:保護資源與授權角色

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin Area</web-resource-name>
        <url-pattern>*.action</url-pattern>  <!-- 所有 .action 請求受保護 -->
    </web-resource-collection>
    <auth-constraint>
        <role-name>myeclipseWeb</role-name>  <!-- 僅此角色可訪問 -->
    </auth-constraint>
    <!-- 可選:傳輸保障 NONE | INTEGRAL | CONFIDENTIAL -->
</security-constraint>
  • url-pattern 至少一個,支援多個;
  • http-method 可選,限制特定 HTTP 方法(如僅限 POST);
  • auth-constraint 缺失 = 所有角色可訪問;
  • user-data-constraint 可設 CONFIDENTIAL 強制 HTTPS。

2. <login-config>:指定認證方式

<login-config>
    <auth-method>BASIC</auth-method>  <!-- BASIC | DIGEST | FORM | CLIENT-CERT -->
    <realm-name>User Basic Authentication</realm-name>
</login-config>

若用 FORM 需額外配置 form-login-page / form-error-page。

3. <security-role>:聲明角色存在

<security-role><role-name>myeclipseWeb</role-name></security-role>
<security-role><role-name>myeclipseWebservices</role-name></security-role>

完整 web.xml 範例(整合 Struts 2 + BASIC 認證)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="https://java.sun.com/xml/ns/j2ee">
    <display-name>Struts Blank</display-name>

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

    <welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>

    <!-- 安全約束 -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Admin Area</web-resource-name>
            <url-pattern>*.action</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>myeclipseWeb</role-name>
        </auth-constraint>
    </security-constraint>

    <security-role><role-name>myeclipseWeb</role-name></security-role>
    <security-role><role-name>myeclipseWebservices</role-name></security-role>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>User Basic Authentication</realm-name>
    </login-config>
</web-app>

實戰驗證流程

  1. 建立 Action 類 SecureAction(含 username 屬性);
  2. struts.xml 映射 /secure.action → SecureAction → /index.jsp;
  3. 輸入頁 input.jsp 提交表單至 secure.action;
  4. 瀏覽器訪問 input.jsp → 提交 → 彈出 BASIC 認證框;
  5. 測試場景:
    • 用戶 webservices(無 myeclipseWeb 角色)→ 403 Forbidden;
    • 不存在用戶 → 401 Unauthorized;
    • 用戶 admin(擁有角色)→ 成功顯示 Welcome, Tom。

中文亂碼提示:若需傳遞中文參數,請將 Struts 2 預設編碼與頁面編碼統一修改為 GBK 或 UTF-8(現代專案建議全鏈路 UTF-8)。

原文出處:博客園 - 配置Struts 2應用程序的安全功能

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved