当前位置: 首页 > 知识库问答 >
问题:

HTTP状态405-尝试提交spring security的自定义登录表单时,不支持请求方法“POST”

梁盛
2023-03-14

我用的是spring,Spring Security。我的应用程序有一个自定义的登录页面,一个jsp页面,我试图在其中发布用户名、密码和csrf令牌,在后端我有一个控制器来捕获和验证登录详细信息。我正在使用tomcat。我正在使用spring security进行登录验证。当我提交登录表单文件HTTP Status 405时出现以下错误-请求方法“POST”不支持任何想法?

登录页面:

<div id="login-box">

    <h3>Login with Username and Password</h3>

    <c:if test="${not empty error}">
        <div class="error">${error}</div>
    </c:if>
    <c:if test="${not empty msg}">
        <div class="msg">${msg}</div>
    </c:if>

    <form name='loginForm' action="<c:url value='/login' />" method='POST'>

        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='username' value=''></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>
        </table>

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

    </form>
</div>

控制器类:

@Controller
public class HelloController {

@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security Custom Login Form");
    model.addObject("message", "This is welcome page!");
    model.setViewName("hello");
    return model;

}

@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {

    ModelAndView model = new ModelAndView();
    model.addObject("title", "Spring Security Custom Login Form");
    model.addObject("message", "This is protected page!");
    model.setViewName("admin");

    return model;

}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout) {

    ModelAndView model = new ModelAndView();
    if (error != null) {
        model.addObject("error", "Invalid username and password!");
    }

    if (logout != null) {
        model.addObject("msg", "You've been logged out successfully.");
    }
    model.setViewName("login");

    return model;

}

Spring-安全配置:

<http auto-config="true">
  <intercept-url pattern="/admin**" access="ROLE_USER" />
  <form-login 
    login-page="/login" 
        default-target-url="/welcome" 
    authentication-failure-url="/login?error" 
    username-parameter="username"
    password-parameter="password" />
  <logout logout-success-url="/login?logout" />
  <!-- enable csrf protection -->
  <csrf/>
</http>

<authentication-manager>
  <authentication-provider>
    <user-service>
    <user name="mkyong" password="123456" authorities="ROLE_USER" />
    </user-service>
  </authentication-provider>
</authentication-manager>   

共有3个答案

艾阳羽
2023-03-14

您的控制器只接受GET请求,您的表单使用POST。首先,我将尝试更改此配置。

@RequestMapping(value = "/login", method = RequestMethod.POST)

您还可以避免指定选项方法,这应该意味着GET和POST。

束飞捷
2023-03-14

首先,使用重定向到登录页面

model.setViewName("login");

你使用spring security吗?如果是,我在你的代码中没有看到任何与Spring Security过滤器相关的内容。

我建议你去那边看看

mykong示例

或者显然是Spring参考

孟征
2023-03-14

好的,我在这里看到的问题是在jsp表单中。表单操作不正确,默认情况下,spring security尝试使用其他一些操作进行登录处理。i、 e./j_spring_security_check,甚至您邮件中的字段名都不正确。

Username field : j_username
Password field : j_password

所以你需要做三件事来让它工作。

  1. 将jsp中表单声明中的操作重命名为action=“

Spring security确实提供了重命名所有组件的灵活性,但让我们先让基本组件工作。预计不会有其他变化

编辑:

我错过了阅读用户名和密码自定义。

只需做一件事(添加了login-Procage-url属性):

<http auto-config="true">
  <intercept-url pattern="/admin**" access="ROLE_USER" />
  <form-login 
     login-page="/login" 
     default-target-url="/welcome" 
     authentication-failure-url="/login?error" 
     login-processing-url="/login"
     username-parameter="username"
     password-parameter="password" />
  <logout logout-success-url="/login?logout" />
  <!-- enable csrf protection -->
  <csrf/>
 类似资料:
  • 我有以下控制器: 13-feb-2016 16:55:09.442警告[http-apr-8080-exec-9]org.springframework.web.servlet.PageNotFound.HandleHttpRequestMethodNotSupported请求方法“Put”不受支持 我不明白这怎么可能。下面是我的依赖项: 编辑2: 2016-02-14 12:30:56 DEBU

  • 问题内容: 我收到此错误: 我正在尝试做的是创建一个带有下拉框的表单,该表单会根据在另一个下拉框中选择的其他值进行填充。例如,当我在框中选择一个名称时,应运行.jsp页面中的函数,然后提交提交的页面,然后在框中再次加载相应的值。 但是我收到此HTTP状态405错误。我在互联网上搜索了解决方案,但找不到任何有帮助的方法。这是我的代码的相关部分: jsp页面的一部分 控制器的一部分: 我怎么会得到这个

  • 我收到这个错误:< code>HTTP状态405 -不支持请求方法“POST ” 我想做的是创建一个带有下拉框的表单,该下拉框根据在另一个下拉框中选择的其他值进行填充。例如,当我在框中选择一个名称时,应该运行. jsp页面中的函数,然后提交的页面再次加载框中的相应值。 但是我收到此HTTP状态405错误。我已经在互联网上搜索了解决方案,但找不到任何有帮助的东西。以下是我的代码的相关部分: jsp页

  • 我有Spring MVC的Spring Security。当我尝试注册时,它给了我405个不支持的“帖子”。我已在安全配置中禁用csrf令牌。让我知道我哪里出错了? 我的登录页面: 授权由LoginController处理: 这是我的Spring Security配置类:

  • 当我尝试实现Spring Security性时,出现以下错误- 控制器: web.xml Spring-security.xml 登录名。jsp 错误:- http://localhost:8080/EmployeeManagement/j_spring_security_check

  • 该场景是用户选择一些产品,然后单击进行支付。在这里,我将他/她重定向到IPG(银行互联网支付网关),并在付款完成和定稿时传递我的返回url。在我添加spring security之前,一切正常。 但是如果在一些内部视图中发布这个url,一切都会恢复正常。 这是正常工作(spring security启用,一切正常) 在浏览器中查看银行IPG的来源(https://pna.shaparak.ir/C