Java 发帖请求说:“请注意;405“不受支持”;启用了Spring安全性
当我使用扩展了Java 发帖请求说:“请注意;405“不受支持”;启用了Spring安全性,java,spring,rest,spring-mvc,spring-security,Java,Spring,Rest,Spring Mvc,Spring Security,当我使用扩展了websecurityConfigureAdapter的类启用Spring安全性时,我在发送POST请求时收到此错误: o.s.web.servlet.PageNotFound : Request method 'GET' not supported 我收到一个错误,当我发送POST时,get是不允许的。。。我已经好几个小时没弄明白了 如果我在我的主类和websecurityconfigeredapter类中注释掉@EnableWebSecurity,效果
websecurityConfigureAdapter
的类启用Spring安全性时,我在发送POST
请求时收到此错误:
o.s.web.servlet.PageNotFound : Request method 'GET' not supported
我收到一个错误,当我发送POST
时,get
是不允许的。。。我已经好几个小时没弄明白了
如果我在我的主类和websecurityconfigeredapter
类中注释掉@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控制器,如果需要的话:
@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);
}
@RequestMapping(value=“/login”,method=RequestMethod.POST)
公共响应身份登录(@RequestBody BusinessUser inputUser){
BusinessUser requestedUser=businessUserService.findByUsername(inputUser.getUsername());
if(requestedUser!=null)
if(BCrypt.checkpw(inputUser.getPassword(),requestedUser.getPassword())
返回新的ResponseEntity(requestedUser,HttpStatus.OK);
返回新的响应属性(HttpStatus.BAD_请求);
}
同样,该控制器工作正常,能够接收POST请求并以JSON格式返回用户信息,但我有WebSecurityConfigureAdapter
类时除外(我以后需要启用OAuth2.0和CSRF保护)。我修复了它
这一行导致了一个错误
.formLogin().loginPage("/login").permitAll()
删除了它,现在一切正常。我将为后代添加更多文档。正如杰克所说,这条线
.formLogin().loginPage("/login").permitAll()
事实上,这就是问题所在。根据以下要求,登录页面有几个要求:
- 它必须是HTTP POST
- 它必须提交给 AbstractAuthenticationFilterConfigure.loginProcessingUrl(字符串)
- 它 应将用户名作为HTTP参数包含在 usernameParameter(字符串)
- 它应该包括作为HTTP的密码 passwordParameter(字符串)名称的参数
方法定义缺少loginPage()方法所需的http参数 您将POST请求发送到哪个URL?@11thdimension@JakeMiller您确定
/login
前面没有在类级别定义的另一个路由?无论如何,当然,如果您试图通过在浏览器栏中写入URL来访问URL,您将发出GET请求。您需要使用AJAX来完成一篇博文……我注意到了一件事。formLogin()。loginPage:根据本文档,您似乎没有满足所有要求:特别是关于http参数的部分。@JohnK这就是问题所在!正在随机移除一些东西,看看它是否能修复它,而这个修复了它。我很感谢你的解释!我通过反复试验找出了问题所在,但完全不知道这条线为什么会导致错误。非常感谢。