Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc HTTP状态405-不支持请求方法“POST”。春季安全_Spring Mvc_Login_Spring Security_Freemarker_Http Status Code 405 - Fatal编程技术网

Spring mvc HTTP状态405-不支持请求方法“POST”。春季安全

Spring mvc HTTP状态405-不支持请求方法“POST”。春季安全,spring-mvc,login,spring-security,freemarker,http-status-code-405,Spring Mvc,Login,Spring Security,Freemarker,Http Status Code 405,我有SpringMVC的SpringSecurity。当我尝试注册时,它给了我405个不支持的“帖子”。我已在安全配置中禁用csrf令牌。让我知道我哪里出错了 我的登录页面: <#-- @ftlvariable name="error" type="java.util.Optional<String>" --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8

我有SpringMVC的SpringSecurity。当我尝试注册时,它给了我405个不支持的“帖子”。我已在安全配置中禁用csrf令牌。让我知道我哪里出错了

我的登录页面:

<#-- @ftlvariable name="error" type="java.util.Optional<String>" -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Log in</title>
</head>
<body>
<nav role="navigation">
<ul>
    <li><a href="/">Home</a></li>
</ul>
</nav>

<h1>Log in</h1>

<p>You can use: demo@localhost / demo</p>

<form role="form" action="/login" method="post">
<div>
    <label for="email">Email address</label>
    <input type="email" name="email" id="email" required autofocus/>
</div>
<div>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" required/>
</div>
<div>
    <label for="remember-me">Remember me</label>
    <input type="checkbox" name="remember-me" id="remember-me"/>
</div>
<button type="submit">Sign in</button>
</form>
</body>
</html>

这个问题很容易解决,您将从wiki获得HTTP状态405,这意味着:

请求的资源不支持请求方法;例如,表单上的GET请求要求通过POST显示数据,或者只读资源上的PUT请求

您正在尝试通过HTTP将表单发布到处理GET请求的处理程序

只要改变这个:

为此:

@Controller
public class LoginController {

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView getLoginPage(@RequestParam Optional<String> error) {
    return new ModelAndView("login", "error", error);
}
}
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/", "/public/**").permitAll()
            .antMatchers("/users/**").hasAuthority("ADMIN")
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .usernameParameter("email")
            .passwordParameter("password")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .deleteCookies("remember-me")
            .logoutSuccessUrl("/")
            .permitAll()
            .and()
            .rememberMe().and().csrf().disable();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .userDetailsService(userDetailsService)
            .passwordEncoder(new BCryptPasswordEncoder());
}
}
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView getLoginPage(@RequestParam Optional<String> error) {
    return new ModelAndView("login", "error", error);
}
}
@Controller
public class LoginController {

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView getLoginPage(@RequestParam Optional<String> error) {
    return new ModelAndView("login", "error", error);
}
}