0

我的数据存储和休眠有问题,我已经能够在 localhost 中集成 Gae + Struts2 + Spring3 + Hibernate4 + MySQL 并且一切似乎都正常,但是当我尝试在 HTTP 中创建会话时,会话未创建或会话.getLastAccessedTime() 始终为 1970-01-01(默认值)。当我转到 http:localhost:8080/_ah/admin 中的数据存储链接时,没有创建会话。

我将 ESAPI 用于安全性和此方法:

public boolean isSessionTimeout() {
    HttpSession session = ESAPI.httpUtilities().getCurrentRequest().getSession(false);
    if (session == null)
        return true;
    Date deadline = new Date(session.getLastAccessedTime() + IDLE_TIMEOUT_LENGTH);
    Date now = new Date();
    return now.after(deadline);
}

返回始终为真。

我在 appengine-web.xml 中启用了会话,只要这种配置在 tomcat 中有效,我就不明白这个问题,但在 appengine 版本 1.9.2 中存在某种问题。

我像这样在 Maven 中构建项目:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <version>2.5.1</version>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <webResources>
                    <!-- in order to interpolate version from pom into appengine-web.xml -->
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>${appengine.app.version}</version>
            <configuration>
                <jvmFlags>
                    <jvmFlag>-Dappengine.generated.dir=${project.basedir}/appengine</jvmFlag>
                    <jvmFlag>-Ddatastore.backing_store=${project.basedir}/local_db.bin</jvmFlag>
                </jvmFlags>
            </configuration>
        </plugin>

    </plugins>
</build>

我的 web.xml 中的第一个过滤器 ESAPIFilter 包含:

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    try {
        ESAPI.httpUtilities().setCurrentHTTP(request, response);
    } catch (Exception e) {
        logger.error(Logger.SECURITY_FAILURE, "Error in ESAPI security filter: " + e.getMessage(), e);
    }

    try {
        // figure out who the current user is
        try {
            ESAPI.authenticator().login();
        } catch (AuthenticationException e) {
            request.setAttribute("message", "Unauthorized");
        }
        // log this request, obfuscating any parameter named password
        ESAPI.httpUtilities().logHTTPRequest(request, logger, Arrays.asList(obfuscate));
        // check access to this URL
        if (!ESAPI.accessController().isAuthorizedForURL(request.getRequestURI())) {
            request.setAttribute("message", "Unauthorized");
        }

        // check for CSRF attacks
        //ESAPI.httpUtilities().verifyCSRFToken(request);
        // forward this request on to the web application
        chain.doFilter(request, response);
        // set up response with content type
        ESAPI.httpUtilities().setContentType(response);
        // set no-cache headers on every response
        // only do this if the entire site should not be cached
        // otherwise you should do this strategically in your controller or actions
        ESAPI.httpUtilities().setNoCacheHeaders(response);
    } catch (Exception e) {
        logger.error(Logger.SECURITY_FAILURE, "Error in ESAPI security filter: " + e.getMessage(), e);
        request.setAttribute("message", e.getMessage());

    } finally {
        // VERY IMPORTANT
        // clear out the ThreadLocal variables in the authenticator
        // some containers could possibly reuse this thread without clearing the User
        try {
            ESAPI.clearCurrent();
        } catch (Exception e) {
            logger.error(Logger.SECURITY_FAILURE, "Error in ESAPI security filter: " + e.getMessage(), e);
        }
    }
}
4

1 回答 1

0

最后我发现了问题,会话是使用默认 session.getLastAccessedTime()=0 创建的,我用当前时间设置了这个值,如下所示:

    public boolean isSessionTimeout() {
    HttpSession session = ESAPI.httpUtilities().getCurrentRequest().getSession(false);
    if (session == null)
        return true;
    long lastAccessedTime=System.currentTimeMillis();
    if(session.getLastAccessedTime()!=0){
        lastAccessedTime=session.getLastAccessedTime();
    } 
    Date deadline = new Date(lastAccessedTime + IDLE_TIMEOUT_LENGTH);
    Date now = new Date();      
    return now.after(deadline);
}

终于现在一切正常,我可以在本地创建一个会话,我还没有在生产中尝试过。

于 2014-04-13T14:21:23.507 回答