我正在使用 Spring Boot v2.1.9.RELEASE 和 Thymeleaf 开发一个 Web 应用程序。该应用程序将服务器端渲染(Spring MVC 常规控制器)与通过 AJAX 调用的休息控制器混合在一起。只要是我使用的唯一 HTTP 方法,GET
该页面就像一个魅力。POST
一旦我添加了一个完全不相关但未使用的DELETE
REST 控制器,CSS 和 JS 资源就会停止加载,并且浏览器控制台中会显示如下错误:
The resource from “http://localhost:8080/css/demo.css” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff)
如果我删除新的控制器,页面将重新开始工作。
当我使用网络监视器检查请求时,我观察到:
X-Content-Type-Options: nosniff
响应中始终存在标头,这与Spring Security 文档一致。- 如果
DELETE
端点不存在,则请求会按预期GET /css/demo.css
返回带有代码。(request header) 和(response header)HTTP 200
都标记为.Accept
Content-Type
text/css
- 当我添加端点时,上述请求返回一个
HTTP 405
.Accept
标题text/css
只是Content-Type
变成application/json
. 此外,还添加了一个新的响应标头:Allow: DELETE
.
我的猜测是,当 Spring 扫描控制器并DELETE
找到方法时,会自动添加一些额外的配置,但我找不到任何参考来确认这一点。我想知道为什么会发生这种情况以及如何避免这种行为。
由于我在本地开发,我的 Spring Security 配置非常简单:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationManagerConfigurer authConfigurer() {
return new InMemoryAuthenticationManagerConfigurer();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authConfigurer().configure(auth);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
我添加的 REST 控制器几乎什么都不做:
@RestController
public class MotivosController {
private final String CONTROLLER_PATH = "/rest/motivos/{id}";
@RequestMapping(name=CONTROLLER_PATH, method=RequestMethod.DELETE)
public void deleteMotivo(@PathVariable(name="id") String id) {
logger.debug("Eliminando el motivo " + id);
}
// Other members I consider unrelated to the problem go here...
}
CSS 和 JS 文件正在加载到template.html
文件夹下的src/main/resources/templates
文件中,如下所示:
<link type="text/css" href="/css/demo.css" rel="stylesheet" />