[英]HTTP Status 405 - Request method 'POST' not supported Spring Security Java Config
Hello I am facing the 'HTTP Status 405 - Request method 'POST' not supported' exception when trying to do login using spring security. 您好,当我尝试使用Spring Security进行登录时,遇到“ HTTP状态405-请求方法'不支持POST'”异常。
Following is my code : 以下是我的代码:
pgLogin.jsp pgLogin.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<script type="text/javascript">
function focusOnLoad() {
document.getElementById("username").focus();
}
</script>
</head>
<body onload="focusOnLoad()">
<div align="center">
<c:if test="${not empty error}">
<div>
<p style="color: red;">${error}</p>
</div>
</c:if>
<c:if test="${not empty message}">
<div>
<p style="color: red;">${message}</p>
</div>
</c:if>
<c:url var="loginUrl" value="/login" />
<form action="${loginUrl}" method="post">
<div>
<table>
<tr>
<td><label for="username">Email</label></td>
<td><input type="text" id="username" name="email" placeholder="Enter Email" required></td>
</tr>
<tr>
<td><label for="password">Password</label></td>
<td><input type="password" id="password" name="password" placeholder="Enter Password" required></td>
</tr>
</table>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div>
<input type="submit" value="Log In">
</div>
</form>
</div>
</body>
</html>
DesertLampSecurityConfiguration.java DesertLampSecurityConfiguration.java
package co.in.desertlamp.configuration;
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 DesertLampSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationMgr) throws Exception {
authenticationMgr.inMemoryAuthentication()
.withUser("subodh.ranadive@desertlamp.com")
.password("Dlpl123#")
.authorities("ROLE_SUPER_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/home").access("hasRole('ROLE_SUPER_USER')")
.and()
.formLogin().loginPage("/loginPage")
.defaultSuccessUrl("/homePage")
.failureUrl("/loginPage?error")
.usernameParameter("email").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/loginPage?logout");
}
}
DefaultController.java DefaultController.java
package co.in.desertlamp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DefaultController {
@RequestMapping(value = { "/"}, method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
model.setViewName("common/pgDefault");
return model;
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView loginPage(@RequestParam(value = "error",required = false) String error) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid Email OR Password");
}
model.setViewName("common/pgLogin");
return model;
}
@RequestMapping(value = { "/home"}, method = RequestMethod.GET)
public ModelAndView homePage() {
ModelAndView model = new ModelAndView();
model.setViewName("common/pgWelcome");
return model;
}
}
Change your config as following to match your method handlers: 如下更改您的配置以匹配您的方法处理程序:
...
.formLogin().loginPage("/login")
.defaultSuccessUrl("/home")
.failureUrl("/login?error")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.