StrutsWeb应用安全实现方案
立即解锁
发布时间: 2025-08-18 02:19:23 阅读量: 1 订阅数: 4 

# Struts Web应用安全实现方案
## 1. 保护JSP页面安全
### 1.1 问题描述
在Web应用中,我们常常需要确保只有已登录的用户才能访问特定的JSP页面。
### 1.2 解决方案
可以使用自定义的JSP标签来实现这一需求。以Struts邮件阅读器示例应用中的`checkLogon`标签为例,将其应用于需要用户登录才能访问的页面。以下是`CheckLogonTag`类的代码:
```java
package org.apache.struts.webapp.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.config.ModuleConfig;
/**
* Check for a valid User logged on in the current session. If there is no
* such user, forward control to the logon page.
*
* @author Craig R. McClanahan
* @author Marius Barduta
* @version $Revision: 1.5 $ $Date: 2005/03/21 18:08:09 $
*/
public final class CheckLogonTag extends TagSupport {
// --------------------------------------------------- Instance Variables
/**
* The key of the session-scope bean we look for.
*/
private String name = Constants.USER_KEY;
/**
* The page to which we should forward for the user to log on.
*/
private String page = "/logon.jsp";
// ----------------------------------------------------------- Properties
/**
* Return the bean name.
*/
public String getName( ) {
return (this.name);
}
/**
* Set the bean name.
*
* @param name The new bean name
*/
public void setName(String name) {
this.name = name;
}
/**
* Return the forward page.
*/
public String getPage( ) {
return (this.page);
}
/**
* Set the forward page.
*
* @param page The new forward page
*/
public void setPage(String page) {
this.page = page;
}
// ----------- Public Methods -----------------
/**
* Defer our checking until the end of this tag is encountered.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag( ) throws JspException {
return (SKIP_BODY);
}
/**
* Perform our logged-in user check by looking for the existence of
* a session scope bean under the specified name. If this bean is not
* present, control is forwarded to the specified logon page.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag( ) throws JspException {
// Is there a valid user logged on?
boolean valid = false;
HttpSession session = pageContext.getSession( );
if ((session != null) && (session.getAttribute(name) != null)) {
valid = true;
}
// Forward control based on the results
if (valid) {
return (EVAL_PAGE);
} else {
ModuleConfig config =
(ModuleConfig) pageContext.getServletContext( ).getAttribute(
org.apache.struts.Globals.MODULE_KEY);
try {
pageContext.forward(config.getPrefix( ) + page);
} catch (ServletException e) {
throw new JspException(e.toString( ));
} catch (IOException e) {
throw new JspException(e.toString( ));
}
return (SKIP_PAGE);
}
}
/**
* Release any acquired resources.
*/
public void release( ) {
super.release( );
this.name = Constants.USER_KEY;
this.page = "/logon.jsp";
}
}
```
在需要用户登录的页面开头引入该标签,示例如下:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/tags/app" prefix="app" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%-- Check if the user is logged in and redirect to logon if not --%>
<app:checkLogon/>
<html>
<head>
<title><bean:message key="mainMenu.title"/></title>
<link rel="stylesheet" type="text/css" href="base.css" />
</head>
<h3><bean:message key="mainMenu.heading"/> <bean:write name="user"
property="fullName" /></h3>
<ul>
<li><html:link action="/EditRegistration?action=Edit"><bean:message
key="mainMenu.registration"/></html:link></li>
<li><html:link forward="logoff"><bean:message key="mainMenu.logoff"/>
</html:link></li>
</ul>
</body>
</html>
```
### 1.3 详细讨论
自定义JSP标签的好处在于可以将登录验证逻辑集中在一处,便于在整个应用中复用。`checkLogon`标签会尝试从HTTP会话中获取指定名称的对象,默认名称由`Constants.USER_KEY`定义。如果未找到该对象,标签会将请求转发到`page`属性指定的页面,默认是`/logon.jsp`。
你可以根据实际需求修改标签的属性,例如:
```jsp
<%-- Check if there's a user and redirect to registration if not --%>
<app:checkLogon name="user" page="/Register.do"/>
```
需要注意的是,这种方式仅能保护包含该标签的JSP页面,对于动作、静态HTML页面或其他Web资源则无法提供安全保护。如果将静态HTML页面转换为JSP页面,也可以使用`checkLogon`标签来保护。
## 2. 按角色限制动作访问
### 2.1 问题描述
在Web应用中,我们可能需要根据用户的角色来限制其对某些动作的访问权限。
### 2.2 解决方案
可以使用`action`元素的`roles`属性来指定允许访问该动作的角色。示例如下:
```xml
<!-- Display all users -->
<action path="/ViewUsers"
forward="/view_users.jsp"
roles="manager,sysadmin"
/>
```
### 2.3 详细讨论
在`struts-config.xml`文件中配置Struts动作时,通过`roles`属性可以将动作限制给特定角色的用户。该属性接受一个以逗号分隔的角色名称列表。当接收到对该动作的请求时,`RequestProcessor.processRoles()`方法会检查用户是否至少拥有其中一个指定的角色。如果用户不具备任何一个角色,将返回HTTP 403错误(禁止访问);否则,正常处理请求。以下是`RequestProcessor`中的`processRoles()`方法:
```java
protected boolean processRoles( HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping )
```
0
0
复制全文
相关推荐










