gpt4 book ai didi

java - Spring Security HTTP POSTMAN - 登录 - 状态 405 - 不允许方法

转载 作者:行者123 更新时间:2023-12-01 17:02:50 25 4
gpt4 key购买 nike

使用 Spring Security 构建了一个小型应用程序。我尝试了一下是否可以使用POSTMAN登录。

问题是我收到了 405 错误 method not allowed。

我用来发布登录请求的 URL 是/authenticateTheUser。

将表单发布到/authenticateTheUser,效果很好。

我想了解一下是否可以通过 POSTMAN 实现登录。

<form action="${pageContext.request.contextPath}/authenticateTheUser" 
method="POST">

我在 POSTMAN 上使用的 URL -> http://dev.security.com/authenticatetheUser?username=mary&password=12345

我知道我没有传递 csrf token 。我首先尝试解决 405 问题。

protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/manager").hasRole("manager")
.antMatchers("/user/**").hasRole("user")
.antMatchers("/admin/**").hasRole("admin")
.and()
.formLogin()
.loginPage("/showLoginPage")
.loginProcessingUrl("/authenticateTheUser")
.successHandler(customAuthenticationSuccessHandler)
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/access-denied");
}

最佳答案

首先,您的 spring 安全配置是正确的,登录表单可以正常工作。

现在您发送的 POSTMAN 请求不适合您的 Spring Security 配置...原因是 Spring Security 正在等待 HTTP POST 请求,该请求在正文中包含用户名和密码。并且您发送的 POST 请求并没有在 BODY 中发布用户名和密码,而是在 URL 中发布,就像 HTTP GET 请求一样。

要使其正常工作,只需使用 URL http://dev.security.com/authenticatetheUser 更改您的 POSTMAN 请求即可但是,将用户名和密码作为表单数据放在 BODY 中...

关于java - Spring Security HTTP POSTMAN - 登录 - 状态 405 - 不允许方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489337/

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