gpt4 book ai didi

java - POST 请求显示 "405 GET not supported"并启用了 Spring 安全性

转载 作者:行者123 更新时间:2023-11-30 02:50:14 25 4
gpt4 key购买 nike

当我使用扩展 WebSecurityConfigurerAdapter 的类启用 Spring 安全性时,在发送 POST 请求时收到此错误:

o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

当我发送 POST 时,我收到一条错误消息,指出不允许使用 GET...我几个小时都无法弄清楚这一点。

如果我在我的主类和 WebSecurityConfigrerAdapter 类中注释掉 @EnableWebSecurity ,它就可以正常工作,如下所示:

@Configuration
@EnableResourceServer
@Order(-20)
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

private final AuthenticationManager authenticationManager;

@Autowired
public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.parentAuthenticationManager(authenticationManager);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**", "/fonts/**", "/videos/**")
.permitAll()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.authorizeRequests()
.antMatchers("/", "/join", "/login", "/about", "/contact", "/index.html")
.permitAll()
.and().authorizeRequests()
.anyRequest().authenticated()
.and().exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and()
.logout().logoutSuccessUrl("/index.html").permitAll()
.and()
.csrf().disable();
}

}

顺便说一句,如果需要的话,这是我的 Spring Controller :

    @RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<BusinessUser> login(@RequestBody BusinessUser inputUser) {
BusinessUser requestedUser = businessUserService.findByUsername(inputUser.getUsername());
if(requestedUser != null)
if(BCrypt.checkpw(inputUser.getPassword(), requestedUser.getPassword()))
return new ResponseEntity<>(requestedUser, HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

同样,该 Controller 工作正常,能够接收 POST 请求并以 JSON 格式返回用户信息,除非我有 WebSecurityConfigurerAdapter 类(我需要启用 OAuth2.0 和CSRF 保护稍后)。

最佳答案

我将为后代添加更多文档。正如 jack 所说,这条线

.formLogin().loginPage("/login").permitAll()

这实际上是问题所在。根据documentation登录页面有几个要求:

  • 它必须是 HTTP POST
  • 必须提交给AbstractAuthenticationFilterConfigurer.loginProcessingUrl(String)
  • 它应包含用户名作为 HTTP 参数,名称为用户名参数(字符串)
  • 它应该包含 HTTP 格式的密码参数名称为passwordParameter(String)

该方法定义缺少 loginPage() 方法所需的 http 参数。

关于java - POST 请求显示 "405 GET not supported"并启用了 Spring 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925821/

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