您的位置:首页 > 编程语言 > Java开发

Struts2.5学习9-HTTP会话接口,Preparable,排除请求参数

2020-01-15 08:54 686 查看

Struts2.5学习9-HTTP会话接口

  • 2.Preparable Interface
  • 3.排除请求参数
  • 1.HTTP会话接口

    Struts 2应用程序可能需要访问HTTP会话对象。Struts 2提供了一个接口SessionAware,您的Action类应实现该接口 来获取对HTTP会话对象的引用。

    1.1修改HelloWorldAction类

    1.实现SessionAware接口,并创建一个Map<String,Object> userSession代表Session,并重写setSession方法
    2.写一个increaseHelloCount()方法,用于每次访问+1
    3.在execute()中执行此方法

    package com.zhou.action;
    
    import java.util.Map;
    
    import org.apache.struts2.interceptor.SessionAware;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloWorldAction extends ActionSupport implements SessionAware{
    private String name;
    private Map<String, Object> userSession;
    private static final String HELLO_COUNT = "helloCount";public String execute() throws Exception{
    increaseHelloCount();
    return "success";
    }
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    @Override
    public void setSession(Map<String, Object> userSession) {
    this.userSession = userSession;
    }
    
    private void increaseHelloCount() {
    Integer helloCount = (Integer) userSession.get(HELLO_COUNT);
    
    if (helloCount == null ) {
    helloCount = 1;
    } else {
    helloCount++;
    }
    
    userSession.put(HELLO_COUNT, helloCount);
    }
    }

    1.2修改helloworld.jsp页面

    value="#session.helloCount"
    从session会话中读取helloCount属性,这里的helloCount在Action类已经定义过

    private static final String HELLO_COUNT = "helloCount";
    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <body>
    <h1><s:text name="greeting"/></h1>
    Hello World, <s:property value="name"/><br/>
    <p>I've said hello to you <s:property value="#session.helloCount" /> times!</p>
    </body>
    </body>
    </html>

    1.3运行测试结果


    1.4 安全漏洞

    为了避免被他人获取session
    使用SessionAware确实引入了一个潜在的安全漏洞
    Map<String, Object> getSession在Action类中没有公共方法。您只需要一个公共void setSession方法即可实现该SessionAware接口。
    还可以使Action类实现ParameterNameAware接口 并重写其acceptableParameterName方法:

    public boolean acceptableParameterName(String parameterName) {
    boolean allowedParameterName = true ;
    
    if ( parameterName.contains("session")  || parameterName.contains("request") ) {
    allowedParameterName = false ;
    }
    
    return allowedParameterName;
    }

    除了让每个实现SessionAware的操作也实现ParameterNameAware接口,您可以告诉参数拦截器为包中的所有操作排除特定的请求属性。在struts.xml中,配置struts-default拦截器集,如下所示:

    <package name="basicstruts2" extends="struts-default">
    <interceptors>
    <interceptor-stack name="appDefault">
    <interceptor-ref name="defaultStack">
    <param name="exception.logEnabled">true</param>
    <param name="exception.logLevel">ERROR</param>
    <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*</param>
    </interceptor-ref>
    </interceptor-stack>
    </interceptors>
    
    <default-interceptor-ref name="appDefault" />
    ...
    </package>

    上面的代码将确保实现“ SessionAware”接口的“ basicstruts2”包中的每个操作都将从params.excludeParams节点中提供的字符串开头的处理参数中排除。

    2.Preparable Interface

    如果某些参数验证失败了却还要跳转到画面,那么使用Preparable接口
    实现此接口
    让Action完成一些初始化的工作,这些初始化工作是放在Preparable接口的prepare()方法中的,该方法在execute()方法之前得到调用。
    所以当参数没有,也要跳转的时候不用丢失参数

    3.排除请求参数

    <interceptors>
    <interceptor-stack name="appDefault">
    <interceptor-ref name="defaultStack">
    <param name="exception.logEnabled">true</param>
    <param name="exception.logLevel">ERROR</param>
    <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*,submit</param>
    </interceptor-ref>
    </interceptor-stack>
    </interceptors>
    
    <default-interceptor-ref name="appDefault" />

    node的值是用逗号分隔的正则表达式或简单字符串列表,这些列表标识不应由Parameters拦截器处理的请求参数。为了排除submit参数(这是上面表单代码中的提交按钮的名称),我刚刚添加submit到列表中。

    请参阅此处描述的“ 基本拦截器堆栈”以查看要排除的参数名称/正则表达式的初始集合。确保复制已排除的参数列表,然后将自己的参数添加到末尾,以逗号分隔。

    • 点赞
    • 收藏
    • 分享
    • 文章举报
    z12131998 发布了7 篇原创文章 · 获赞 0 · 访问量 353 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐