Spring安全性-POST方法不工作
尊敬的论坛成员们,大家好 请告诉我为什么POST方法不起作用?我无法捕获通过POST接收的参数。它不提供任何错误,只返回null。我已经为这个问题挣扎了好几天了 证券配置类Spring安全性-POST方法不工作,spring,post,spring-security,http-post,Spring,Post,Spring Security,Http Post,尊敬的论坛成员们,大家好 请告诉我为什么POST方法不起作用?我无法捕获通过POST接收的参数。它不提供任何错误,只返回null。我已经为这个问题挣扎了好几天了 证券配置类 package com.myfilter.security; import com.myfilter.filter.CustomFilter; import org.springframework.beans.factory.annotation.Autowired; import org.
package com.myfilter.security;
import com.myfilter.filter.CustomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
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;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// I connect my CustomFilter where I will catch POST parameters
@Autowired
CustomFilter customFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
// I add a filter where I will catch POST parameters
addFilterBefore(customFilter, AnonymousAuthenticationFilter.class)
.authorizeRequests()
.mvcMatchers("/login", "/").permitAll()
// I indicate that the POST method will be used
.mvcMatchers(HttpMethod.POST,"/login").permitAll()
.and()
.csrf().disable()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/");
}
}
CustomFilter.class(这是一个我将捕获POST参数的过滤器)
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign in</title>
</head>
<body>
<form name="user" method="post" action="/login">
<input type="text" name="email">
<input type="password" name="password">
<button type="submit">Sign in</button>
</form>
</body>
</html>
在github查看此项目您可以发布控制器类吗?另外,您可以提供一些有关为什么使用filterbean的详细信息。我需要一个筛选器,所以我使用的是GenericFilterBean
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign in</title>
</head>
<body>
<form name="user" method="post" action="/login">
<input type="text" name="email">
<input type="password" name="password">
<button type="submit">Sign in</button>
</form>
</body>
</html>
package com.myfilter.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class HomeController {
@GetMapping(value = "/")
public String home () {
return "/home";
}
@GetMapping(value = "/login")
public String login () {
return "/login";
}
@PostMapping(value = "/login")
public String loginPost () {
return "redirect:/";
}
}