我已经在spring boot应用程序上开发了rest API。API仅接受GET和POST,但在使用OPTIONS方法请求时,API会响应200状态(而不是405)。我用谷歌搜索了这个问题,但是没有一个解决方案是基于springboot的。
响应:
Allow: OPTIONS, TRACE, GET, HEAD, POST
Public: OPTIONS, TRACE, GET, HEAD, POST
Run Code Online (Sandbox Code Playgroud)
需要禁用OPTIONS方法。
我使用Spring Security进行基本身份验证来保护我的REST API.
以下是配置代码:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("admin");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated();
}
}
Run Code Online (Sandbox Code Playgroud)
我在使用正确的用户名和密码验证自己时遇到了禁止(403)错误.
请建议修改以使其正常工作.