Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring安全性:注销Isn';t工作-”的;“请求的资源不可用”;_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Spring安全性:注销Isn';t工作-”的;“请求的资源不可用”;

Spring安全性:注销Isn';t工作-”的;“请求的资源不可用”;,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,所以,我正试图让我的第一个实践应用程序与SpringSecurity一起工作。这只是一个简单的测试,在继续进行更复杂的实现之前,试着看看我是否能让基础工作起来 顺便说一句,我使用的是SpringSecurity4BTW和Spring4.2.2 现在,我有一个欢迎页面和一个管理页面。试图访问管理页面被截获并重定向到默认的Spring安全登录表单。从那里,我可以登录并通过身份验证访问管理页面 在管理页面上,我有一个注销链接。这就是问题所在。我现在有两个注销链接,我尝试了两种不同的方法来实现这一点 第

所以,我正试图让我的第一个实践应用程序与SpringSecurity一起工作。这只是一个简单的测试,在继续进行更复杂的实现之前,试着看看我是否能让基础工作起来

顺便说一句,我使用的是SpringSecurity4BTW和Spring4.2.2

现在,我有一个欢迎页面和一个管理页面。试图访问管理页面被截获并重定向到默认的Spring安全登录表单。从那里,我可以登录并通过身份验证访问管理页面

在管理页面上,我有一个注销链接。这就是问题所在。我现在有两个注销链接,我尝试了两种不同的方法来实现这一点

第一个是一个简单的URL。它使用标记,其中包含JSTL标记。我正在尝试调用那里的注销URL。当我这样做时,我会得到一个HTTP状态404页面,页面上有一个说明,说明请求的资源不可用

第二种方法是使用表单。我尝试在表单中使用submit按钮,表单的action属性设置为注销URL,方法设置为post

这一个给了我一个更复杂的错误,我将复制并粘贴到这里:

标题:HTTP状态403-在请求参数“\u CSRF”或标头“X-CSRF-Token”上发现无效的CSRF令牌“null”

类型:状态报告

消息:在请求参数“\u CSRF”或标头“X-CSRF-Token”上发现无效的CSRF令牌“null”

描述:已禁止访问指定的资源

在这两种情况下,错误页面的url都是localhost:8080/spring-security/logout。spring security是此测试应用程序的名称

所以,我有点迷路了。这是我第一次使用Spring Security,我真的不知道我做错了什么。任何帮助都将不胜感激

下面我将粘贴admin.jsp页面和spring-security.xml页面

提前谢谢

admin.jsp页面:

<%@ page session="true" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="jstl-stub.jsp" %> <!-- Include links to JSTL Libraries -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin Page</title>
</head>
<body>

<h1>Title: ${title}</h1>

<h1>Message: ${message}</h1>

<c:if test="${pageContext.request.userPrincipal.name != null}">
    <h2>Welcome: ${pageContext.request.userPrincipal.name}
    | <a href="<c:url value="/logout"/>">Logout</a></h2>

    <br><br>

    <form action="logout" method="post">
        <input type="submit" value="logout"/>
    </form>
</c:if>

</body>
</html>

管理页面
标题:${Title}
消息:${Message}
欢迎:${pageContext.request.userPrincipal.name}
| 


spring-security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

<security:http auto-config="true">
    <security:intercept-url pattern="/admin" access="hasRole('ROLE_USER')"/>
    <security:logout logout-url="/logout" logout-success-url="/welcome"/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider >
        <security:user-service>
            <security:user name="user" password="password" authorities="ROLE_USER"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

</beans:beans>

我认为第二个问题是您的post方法。我看到您使用的是安全4.0,默认情况下,4.0启用了csrf保护。如果你不想有这个,你必须禁用它

从Spring Security 4.0开始,CSRF保护默认使用 XML配置。如果要禁用CSRF保护,请 下面可以看到相应的XML配置

<http>
  <!-- ... -->
  <csrf disabled="true"/>
</http>

请参阅下面的链接

但是,如果要添加csrf保护,则需要向post方法添加头

本教程可能对您有所帮助

禁用csrf将对您有效。并允许您通过get方法调用
/logout

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    // other code ...
}