【问题标题】:Spring Security Logout Back ButtonSpring Security 注销后退按钮
【发布时间】:2011-08-05 23:08:53
【问题描述】:

spring security 有没有办法防止下面的最后一点?我正在使用 3.0.5

-用户登录我的网站 -用户转到网站中的任何页面并单击注销 -注销链接使用户会话无效并将它们发送到我网站的登录页面 - 在同一个浏览器中,用户导航到新网站(比如 cnn.com) -用户点击后退按钮,他们登陆我的登录页面 - 用户再次点击返回按钮,他们最终进入应用程序中可能包含我们不希望出现的数据的页面。如果他们点击页面上的任何链接,他们会立即被发送到登录页面,但他们可以从浏览器缓存中查看缓存的页面……有什么办法不让他们查看?

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
    xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="dc" />
    <global-method-security />
    <http access-denied-page="/auth/denied.html">
         <intercept-url filters="none" pattern="/javax.faces.resource/**" />
         <intercept-url filters="none" pattern="/services/rest-api/1.0/**" />
         <intercept-url filters="none" pattern="/preregistered/*"/>
         <intercept-url
            pattern="/**/*.xhtml"
            access="ROLE_NONE_GETS_ACCESS" />
         <intercept-url
            pattern="/auth/*"
            access="ROLE_ANONYMOUS,ROLE_USER"/>
         <intercept-url
            pattern="/preregistered/*"
            access="ROLE_ANONYMOUS,ROLE_USER"/>
         <intercept-url
            pattern="/registered/*"
            access="ROLE_USER"
            requires-channel="http"/>
        <form-login
            login-processing-url="/j_spring_security_check.html"
            login-page="/auth/login.html"
            default-target-url="/registered/home.html"
            authentication-failure-url="/auth/login.html" />
         <logout invalidate-session="true" 
              logout-url="/auth/logout.html" 
              success-handler-ref="DCLogoutSuccessHandler"/>
        <anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/>
        <custom-filter after="FORM_LOGIN_FILTER" ref="xmlAuthenticationFilter" />
        <session-management session-fixation-protection="none"/>
    </http>
    <!-- Configure the authentication provider -->
    <authentication-manager alias="am">
        <authentication-provider user-service-ref="userManager">
                <password-encoder ref="passwordEncoder" />
        </authentication-provider>
        <authentication-provider ref="xmlAuthenticationProvider" />
    </authentication-manager>
</beans:beans>

【问题讨论】:

标签: spring-security


【解决方案1】:

下面的过滤器处理了我的情况:

package com.dc.api.service.impl;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class CacheControlFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
        resp.setHeader("Last-Modified", new Date().toString());
        resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
        resp.setHeader("Pragma", "no-cache");

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}

    @Override
    public void init(FilterConfig arg0) throws ServletException {}

}

【讨论】:

  • " resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT"); " 是什么意思?为什么指定 2001 年?
  • hmm 使用相同的解决方案,但过滤器不适用于 /j_spring_security_logout。想法?
【解决方案2】:

要解决这个问题,您必须在您的安全 xml 配置文件中添加:

<security:http auto-config="true" use-expressions="true">

    <security:headers >
        <security:cache-control />
        <security:hsts/>
    </security:headers>

【讨论】:

  • 这很好用。但是,文档说“我们建议您避免使用它,而是明确配置您需要的服务。”对此有什么想法吗?
【解决方案3】:

在春季 3.0.x 中

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="cacheSeconds" value="0" />
</bean>

春季 2.5.x

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="cacheSeconds" value="0" />
</bean>

【讨论】:

    【解决方案4】:

    是的,我使用了 spring-security 3.2.9.RELEASE 并简单地将 &lt;security:headers /&gt; 与上面帖子中的 applicationContext.xml 文件之类的 spring 配置文件结合在一起

    <security:http 
       auto-config="true" use-expressions="true">
       <security:headers />      
    </security:http>
    

    这样用户将无法访问访问过的其他应用页面 注销后使用浏览器的后退和前进按钮。

    【讨论】:

      【解决方案5】:

      如果你和我一样,在使用 c12 的缓存过滤器后没有让它工作,并且你正在使用 &lt;security:http auto-config="true"&gt;,请确保你不再需要 auto-config="true" 部分。它(看起来像)添加了不处理协议注销的http基本身份验证!这导致您可以获取您的注销 URL,但点击后退按钮只会让您返回,因为您并没有真正注销。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 2015-07-11
        • 2013-09-21
        • 2023-03-29
        • 2014-08-14
        • 2013-12-11
        • 2011-03-26
        相关资源
        最近更新 更多