Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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/2/spring/12.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
Java 如何通过自定义登录页面纠正spring安全异常?_Java_Spring_Jsp_Spring Mvc_Spring Security - Fatal编程技术网

Java 如何通过自定义登录页面纠正spring安全异常?

Java 如何通过自定义登录页面纠正spring安全异常?,java,spring,jsp,spring-mvc,spring-security,Java,Spring,Jsp,Spring Mvc,Spring Security,我有一个例外 HTTP状态403-在请求参数“\u CSRF”或标头“X-CSRF-Token”上发现无效的CSRF令牌“null” 我试图通过自定义登录页面实现spring安全性 spring-security.xml <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans"

我有一个例外

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

我试图通过自定义登录页面实现spring安全性

spring-security.xml

    <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">

<!-- <http>
<intercept-url pattern ="/welcome*" access="hasRole('ROLE_USER')"/>
<http-basic/>
</http> -->

<!-- <http>
<intercept-url pattern ="/welcome*" access="hasRole('ROLE_USER')"/>
<form-login/>
<logout logout-success-url="/home"/>
</http> -->
<http>
<intercept-url pattern ="/welcome*" access="hasRole('ROLE_USER')"/>
<form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed"/>
<logout logout-success-url="/logout"/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="rahul" password="123" authorities="ROLE_USER"/>
<user name="rohit" password="567" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
hello.jsp

home.jsp
我认为不需要

您需要在登录时提交crsf令牌(以及注销和每隔一段时间提交一次) 发布、放置、删除请求)

有几种方法可以将其添加到jsp中:

  • 使用spring的jsp
    标记(而不是standart表单标记)或
  • 您需要通过spring安全标签添加crsf令牌,或者:
  • 通过“标准jsp”:
“标准jsp”示例:


@请参阅:Spring安全参考章节


顺便说一句:我强烈建议您阅读完整的

您需要在登录时提交crsf令牌(以及注销和每隔一段时间提交一次) 发布、放置、删除请求)

有几种方法可以将其添加到jsp中:

  • 使用spring的jsp
    标记(而不是standart表单标记)或
  • 您需要通过spring安全标签添加crsf令牌,或者:
  • 通过“标准jsp”:
“标准jsp”示例:


@请参阅:Spring安全参考章节

顺便说一句:我强烈建议阅读完整的

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Login Page</title>
<!-- <style>
.errorblock
{
color : #f0000;
background-color : #ffEEEE;
border : 3px solid #ff0000;
padding : 8px;
margin : 16px;
}
</style> -->
</head>
<body onload='document.f.j_username.focus();' bgcolor="blue">
<h3>Login with Username andPassword (Custom page)</h3>

<%-- <c:if test="$SPRING_SECURITY_LAST_EXCEPTION !=null"}">
<div class="errorblock">
Your login atempt are not sucessfull,try again
 <br/>Caused : ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message }
 </div>
 </c:if> --%>
<%-- <form name='f' action="<c:url value='j_spring_security_logout'/>" method="POST">
 --%>
 <form name='f' action='/SpringSecurityApplication/login' method="POST">
<table>
<tr>
<td>User :</td><td><input type='text' name='username'></td>
</tr>

<tr>
<td>Password :</td><td><input type='password' name='password'></td>
</tr>
<tr><td colspan ='2'><input name="submit" type="submit" value="submit" ></td></tr>
<tr><td colspan ='2'><input name="reset" type="reset" ></td></tr>
</table>
</form>
</body>
</html>
package com.springtraining.security.controller;

import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {
    public LoginController() {
        System.out.println("LoginController constructor is called ");
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String printWelcome(ModelMap model, Principal principal) {
        System.out.println("**********Login Controller is Called********");

        String name = principal.getName();
        model.addAttribute("username", name);
        model.addAttribute("message", "Spring Security Custom Form Example");
        return "hello";
    }

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String home(ModelMap model) {

        return "home";

    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(ModelMap model) {
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return "login";
    }

    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
    public String loginError(ModelMap model) {
        model.addAttribute("error","true");
        return "login";
    }

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