简体   繁体   English

找不到CSRF令牌

[英]CSRF token not found

I am following a tutorial on spring security and I have the following so far: I get following error when I try to login from my login page: 我正在关注有关spring安全性的教程,到目前为止,我有以下内容:尝试从登录页面登录时出现以下错误:

HTTP Status 403 - Expected CSRF token not found. HTTP状态403-找不到预期的CSRF令牌。 Has your session expired? 您的会话是否已过期?

The solution I found on stackoverflow indicated to add a hidden attribute to the form and I did that. 我在stackoverflow上找到的解决方案指示向表单添加隐藏属性,而我做到了。 this is the attribute: 这是属性:

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

I have rechecked the code multiple times to and I keep getting the same 403 error even though my code seems to exactly match the Udemy tutorial. 我已经多次检查代码,即使我的代码似乎与Udemy教程完全匹配,我仍然收到相同的403错误。

Thanks in advance. 提前致谢。

My login.jsp page: 我的login.jsp页面:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>

<html>

    <head>

        <title>Please login</title>
    </head>

    <body>

    <c:url value="/login" var="loginProcessingUrl"/>
 <form action="${loginProcessingUrl}" method="post">

    <fieldset>
        <legend>Please Login</legend>
        <!-- use param.error assuming FormLoginConfigurer#failureUrl contains the query parameter error -->
        <c:if test="${param.error != null}">
            <div>
                Failed to login.
                <c:if test="${SPRING_SECURITY_LAST_EXCEPTION != null}">
                  Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
                </c:if>
            </div>
        </c:if>
        <!-- the configured LogoutConfigurer#logoutSuccessUrl is /login?logout and contains the query param logout -->
        <c:if test="${param.logout != null}">
            <div>
                You have been logged out.
            </div>
        </c:if>
        <p>
        <label for="username">Username</label>
        <input type="text" id="username" name="username"/>
        </p>
        <p>
        <label for="password">Password</label>
        <input type="password" id="password" name="password"/>
        </p>
        <!-- if using RememberMeConfigurer make sure remember-me matches RememberMeConfigurer#rememberMeParameter -->
        <p>
        <label for="remember-me">Remember Me?</label>
        <input type="checkbox" id="remember-me" name="remember-me"/>

        </p>


        <input type="hidden" name="${_csrf.parameterName}" value="{_csrf.token}"/>
        <div>
            <button type="submit" class="btn">Log in</button>
        </div>


    </fieldset>

 </form>

    </body>


</html>

My index.jsp with a logout form: 我的index.jsp具有注销表单:

    <!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>
        <c:url value="/showMessage.html" var="messageUrl" />
        <a href="${messageUrl}">Click to enter</a>

        <form action="logout" method="post">
            <input type="submit" value="logout"/>
            <input type="hidden" name="${_csrf.parameterName}" value="{_csrf.token}"/>

        </form>
    </body>
</html>

My security config file: 我的安全配置文件:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void congigureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

    public void configure(HttpSecurity http) throws Exception{

        http.authorizeRequests()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
        .loginPage("/login")
            .and()
        .httpBasic();
    }
}

An Empty WebApplicationInitializer: 空的WebApplicationInitializer:

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

}

A login controller: 登录控制器:

    @Controller
public class LoginController {

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String loginPage(ModelMap map){

        //prefix: /WEB-INF/view
        //postfix .jsp
        //prefix + view + postfix
        // /WEB-INF/view/login.jsp
        return "login";

    }

This is the code in my application-config.xml 这是我的application-config.xml中的代码

<context:component-scan
        base-package="com.springsecurityexample"/>

This is the code in my mvc-config.xml file: 这是我的mvc-config.xml文件中的代码:

<context:component-scan
        base-package="com.springsecurityexample.web"/> 


<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
</bean>

and my web.xml file: 和我的web.xml文件:

<display-name>SpringSecurityTut1</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

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


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

maybe you need <form:form></form:form> because 也许您需要<form:form></form:form>因为

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

is spring'tag 是春天的标签

It was a simple mistake that cost me a whole day. 这是一个简单的错误,使我花了一整天的时间。 I forgot the "$" in my value assignment in both the login and index page. 我在登录页面和索引页面的值分配中都忘记了“ $”。

I had value="{_csrf.token} instead of value="${_csrf.token} 我有value =“ {_ csrf.token}而不是value =” $ {_ csrf.token}

That was it. 就是这样 A missing dollar sign!! 缺少美元符号!! Thanks for all the input people. 感谢所有输入者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 安全 csrf 已禁用,仍然找到无效的 CSRF 令牌 - Spring security csrf disabled, still get an Invalid CSRF token found 找不到AngularJS HTTP POST预期的CSRF令牌 - AngularJS HTTP POST Expected CSRF token not found 找不到预期的CSRF令牌Spring Security - Expected CSRF token not found Spring Security 发现无效的 CSRF 令牌 - Spring 引导和 Axios - Invalid CSRF token found - Spring Boot and Axios HTTP状态403-找不到预期的CSRF令牌 - HTTP Status 403 - Expected CSRF token not found 春季启动-在请求参数&#39;_csrf&#39;或标头&#39;X-CSRF-TOKEN&#39;上发现无效的CSRF令牌&#39;null&#39; - Spring boot - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN' 错误HTTP状态403-在请求参数&#39;_csrf&#39;或标头&#39;X-CSRF-TOKEN&#39;上发现无效的CSRF令牌&#39;null&#39; - Error HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN' 找不到预期的CSRF令牌。 您的会话是否已过期? - Expected CSRF token not found. Has your session expired? HTTP状态403 - 在请求参数上找到无效的CSRF令牌“null” - HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter 未找到预期的CSRF令牌。您的会话是否已过期403 - Expected CSRF token not found. Has your session expired 403
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM