gpt4 book ai didi

java - HTTP 状态 405 - 请求方法 'POST' 不支持 Spring Security Java Config

转载 作者:行者123 更新时间:2023-11-30 07:38:52 33 4
gpt4 key购买 nike

您好,我遇到“HTTP 状态 405 - 不支持请求方法“POST””尝试使用 Spring Security 登录时出现异常。

以下是我的代码:

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

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

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;
}
}

最佳答案

按以下方式更改您的配置以匹配您的方法处理程序:

...
.formLogin().loginPage("/login")
.defaultSuccessUrl("/home")
.failureUrl("/login?error")

关于java - HTTP 状态 405 - 请求方法 'POST' 不支持 Spring Security Java Config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967097/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com