Spring安全注销处理

cht*_*cht 9 spring spring-security

根据Spring Security 4.0.0文档:

4.2.4注销处理

logout元素通过导航到特定URL添加了对注销的支持.默认的注销URL是/ logout,但您可以使用logout-url属性将其设置为其他内容.有关其他可用属性的更多信息,请参见命名空间附录.

但是,在doc中遵循安全设置后,URL/logout不会显示注销页面.相反,它表明

在此输入图像描述

相反,URL /登录正常.

在此输入图像描述

以下是我的设置:

Spring Framework 4.1.6
Spring Security 4.0.0

在web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Test8</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/security-config.xml</param-value>
    </context-param>


</web-app>
Run Code Online (Sandbox Code Playgroud)

安全-config.xml中

<?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: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/security
           http://www.springframework.org/schema/security/spring-security.xsd">
    <http>
        <intercept-url pattern="/**" access="hasRole('USER')" />
        <form-login />
        <logout />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="aaa" password="111" authorities="ROLE_USER, ROLE_ADMIN" />
                <user name="bbb" password="222" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>
Run Code Online (Sandbox Code Playgroud)

f.k*_*sis 12

Spring安全性自动启用csrf,后者自动禁用GET注销.您可以通过<csrf disabled="true"/>在设置中禁用csrf保护<http>或仅使用POST 来解决此问题.

请参阅http://docs.spring.io/spring-security/site/docs/4.0.1.RELEASE/reference/htmlsingle/#csrf-logout


小智 5

只需将以下代码放在要注销的jsp中,

<c:url var="logoutUrl" value="/j_spring_security_logout" />
    <form action="${logoutUrl}" id="logout" method="post">
        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />
    </form>
    <a href="#" onclick="document.getElementById('logout').submit();">Logout</a>
Run Code Online (Sandbox Code Playgroud)

Bean配置文件中的相应条目-

<security:logout logout-url="/j_spring_security_logout" logout-success-url="/whateverPageYouWant" invalidate-session="true" />
Run Code Online (Sandbox Code Playgroud)

-这对我来说是spring-security-4。*


Oha*_*adR 0

请注意,没有“注销页面”。/logout 是 Spring 的端点,它让 Spring 知道应用程序要求注销用户,因此它调用特定的处理程序。

用户注销后,Spring 会重定向到另一个页面,您可以在 XML 中配置“默认目标”。