【问题标题】:Why does the Spring csrf give me a 403 network error?为什么 Spring csrf 给我一个 403 网络错误?
【发布时间】:2017-01-25 02:09:57
【问题描述】:

对于下面混乱的 HTML 语法,我深表歉意。当我启用 http.authorizeRequest() csrf 时,我不断收到 403 错误。当然,如果我有csrf.disable(),一切正常。

据我了解,<form:form> 标签自动使用了 CSRF 令牌,一定是这样,因为下面的 URL 停止工作。

谁能告诉我什么是我不明白的?

NetworkError: 403 Forbidden - http://localhost:8080/AssetCore/createGuideline/

我的配置类:

@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    private final Logger log = Logger.getLogger (this.getClass());
    /**
     * This class gets called during startup.
     */

    /**
     * Configure http security.
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
         * This method gets called when the app starts up.
         * I believe that all the patterns for the MVC and rest calls will need to go here.
         */
        log.info("configure(): called to set up authorizedRequest pattern matching.");
        http
            .logout().logoutSuccessUrl("/login?loggedout=true").invalidateHttpSession(true).deleteCookies("JSESSIONID")
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
        http.authorizeRequests()
            .antMatchers(HttpMethod.PUT, "/assessment/**").permitAll()
            .antMatchers("/createGuideline/**",).permitAll()    
    }

JSP:

   <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="ark" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type"
            content="text/html; charset=windows-1252">
        <meta charset="utf-8">
        <title>ASSET Core - PCC</title>
        <jsp:include page="ourCSSandJS.jsp" />
        <link rel="stylesheet" href="/AssetCore/resources/css/pccStyle.css">
        <link rel="stylesheet" href="/AssetCore/resources/css/scrollbar.css">
        <link rel="stylesheet" href="/AssetCore/resources/css/mcp_style.css">
        <script>var storedFormatExtraArgs = [];</script>
        <script src="/AssetCore/resources/js/valid/pcc.js"></script>
        <script src="/AssetCore/resources/js/controlFormatting.js"></script>
        <script src="/AssetCore/resources/js/valid/controlCard.js"></script>
        <script src="/AssetCore/resources/js/thirdParty/jquery.scrollbar.min.js"></script>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>     
    </head>
    <body>`                     <form:form id="customGuide" method="POST"
                            action="/AssetCore/createGuideline">
                            <fieldset>
                                <legend id="createGuideline">Create Guideline</legend>
                                <p>
                                    <label for="gName">Name</label><br /> <input type="text"
                                        id="gName" name="gName" />
                                </p>`

【问题讨论】:

  • 乍一看,它应该可以工作。在 Chrome 中尝试,并在网络检查器中查看 csrf 令牌是否在表单中。
  • 我不这么认为。这是我得到的:请求 URL:localhost:8080/AssetCore/createGuideline 请求方法:POST 状态代码:403 禁止远程地址:[::1]:8080 响应标头查看源缓存控制:无缓存,无存储,最大年龄=0,必须重新验证 Content-Language:en-US Content-Length:2106 Content-Type:text/html;charset=ISO-8859-1 Date:Fri, 16 Sep 2016 19:14:44 GMT Expires:0 Pragma :no-cache 服务器:Apache-Coyote/1.1 X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block 请求标头
  • 这是响应标头(我认为),除非我误读了请求正文中的文档。
  • 我添加了以下编码
    和 javascript 函数警报返回“未定义”的值。所以它 没有注入它。

标签: spring-security csrf


【解决方案1】:

好的,所以 Spring 文档不正确,至少不是我所看到的。我实际上在&lt;form:form&gt; 标签中添加了以下内容

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

现在它可以工作了。

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2011-04-10
    • 2019-07-02
    • 1970-01-01
    • 2021-10-14
    • 2018-01-14
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多