java - 注销在 Spring Security 中不起作用

标签 java spring-security

我正在使用 Spring Security 4.0 编写一个安全应用程序。作为其中的一部分,我想调用注销电话。它只是给出了不支持请求方法“POST”。这是我的代码:

spring-security.xml

<security:http  auto-config="true">
    <security:access-denied-handler error-page="/denied"/>
        <security:form-login login-page="/login"
        username-parameter="j_username"
        password-parameter="j_password"
        login-processing-url="/j_spring_security_check"
        authentication-failure-url="/login?failed=true" 
        default-target-url="/home" always-use-default-target="true"/>
        <security:custom-filter ref="secfilter" before="FILTER_SECURITY_INTERCEPTOR" />

        <security:logout invalidate-session="true" logout-url="/j_spring_security_logout" logout-success-url="/login"/>
        <!-- <security:logout  logout-url="/j_spring_security_logout" logout-success-url="/login"/> -->

    <security:csrf /> 
</security:http>

jsp

<a href="j_spring_security_logout">  <button class="logoutbtn">logout</button></a>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

最佳答案

如果使用CSRF,则必须使用HTTP POST (在 JSP 中使用 <form>)而不是 HTTP GET (在 JSP 中使用 <a>),请参阅 Spring Security Reference :

18.5.3 Logging Out

Adding CSRF will update the LogoutFilter to only use HTTP POST. This ensures that log out requires a CSRF token and that a malicious user cannot forcibly log out your users.

One approach is to use a form for log out. If you really want a link, you can use JavaScript to have the link perform a POST (i.e. maybe on a hidden form). For browsers with JavaScript that is disabled, you can optionally have the link take the user to a log out confirmation page that will perform the POST.

例如,请参阅Spring Security Reference :

37.5.1 Automatic Token Inclusion

Spring Security will automatically include the CSRF Token within forms that use the Spring MVC form tag. For example, the following JSP:

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:form="http://www.springframework.org/tags/form" version="2.0">
    <jsp:directive.page language="java" contentType="text/html" />
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <!-- ... -->

    <c:url var="logoutUrl" value="/logout"/>
    <form:form action="${logoutUrl}"
            method="post">
    <input type="submit"
               value="Log out" />
    <input type="hidden"
                name="${_csrf.parameterName}"
                value="${_csrf.token}"/>
    </form:form>

    <!-- ... -->
</html>
</jsp:root>

关于java - 注销在 Spring Security 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41213013/

相关文章:

validation - 为什么我们需要对 google recaptcha 进行服务器端验证?

spring-security - Spring 3.0 安全性 - 带身份验证的授权

Java 并发 :Is ReentrantLock a wrong design?

用于以时间戳格式输入日期的 Java FX 控件

java - 在启动时传递 jboss 参数

java - 使用 "item1.item1A"等键访问 map 中的深层对象

java - 如何使用 Spring Boot Security 从 facebook 获取访问 token

java - 如何使用Spring Security同时保护2条路径?

java - Spring Security DelegatingFilterProxy无限递归

Java JLabel,为什么 "<html>/* test */</html>"导致JLabel为空?