InMemoryAuthentication无法验证提供的CSRF令牌并返回Whitelabel错误页面

时间:2018-03-01 22:50:32

标签: jsp authentication spring-boot spring-security

在Spring Boot安全项目中,当我尝试从登录(在输入的用户名和密码之后)重定向到所选页面时,我得到一个Whitelabel错误页面。详细信息是我从jsp文件的视图开始。 start.jsp只有一个目的,即重定向到test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
            8859-1">
        <title>Startpage</title>
    </head>
    <body>
        <p>Click <a href="/test">here</a> Start.</p>
    </body>
</html>

start.jsp的安全设置为permitAll,test.jsp的设置已经过身份验证,因此在调用test.jsp login.jsp之前输入用户名和密码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
            8859-1">
       <title>Login</title>
    </head>
    <body>

        <form method="POST" action="/login">
            User Name : <input type="text" name="username"/>
            Password: <input type="password" name="password"/>
            <button type="submit">Submit</button>
        </form>
    </body>
</html>

和endpage test.jsp看起来像这样

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
            8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        Hi
    </body>
</html>

错误消息是

  

Whitelabel错误页面   此应用程序没有/ error的显式映射,因此您将此视为回退。   Thu Mar 01 21:43:40 CET 2018   出现意外错误(type = Forbidden,status = 403)。   无法验证提供的CSRF令牌,因为找不到您的会话。

它作为http在我的localhost上运行,没有选择任何ssl或任何其他安全设置,除了pom.xml文件中的Spring Boot Securitydependency。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Mvc由

处理
package com.proj.db_proj;

import org.springframework.context.annotation.Configuration;
import 
  org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import 
 org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/startpage").setViewName("startpage");
        registry.addViewController("/test").setViewName("test");
        registry.addViewController("/").setViewName("start");
    }
}

以及具有身份验证和配置的Websecurity

package com.proj.db_proj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.
    builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.
    web.builders.HttpSecurity;
import org.springframework.security.config.annotation.
    web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.¨
    web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

     @Override
         protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/start").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
        }

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) 
            throws Exception {
            auth
                .inMemoryAuthentication()
                .withUser("user").password("user").roles("USER");
        }

 }

任何人都可以看到任何错误或知道我为什么会收到Whitelabel错误页面?我已经关注了手册和检查过的教程以及有关stackoverflow的问题,没有任何答案。

2 个答案:

答案 0 :(得分:1)

在错误消息中,您没有追加csrf令牌。

Could not verify the provided CSRF token because your session was not found.

在spring boot security中,默认情况下启用csrf。
如果要禁用csrf,请将此代码附加到HttpSecurity配置中。 (不要错过.and())

.csrf().disable();

答案 1 :(得分:0)

您应该使用Thymeleaf命名空间实际使用CSRF(默认情况下已启用)。

更改:

<form method="POST" action="/login">

收件人:

<form method="POST" th:action="@{/login}">