由于添加了DELETE端点时,由于Spring MVC中的MIME类型,资源被阻止

问题描述 投票:0回答:1

我正在使用Spring Boot v2.1.9.RELEASE和Thymeleaf开发一个Web应用程序。该应用程序将服务器端渲染(Spring MVC常规控制器)与通过AJAX调用的其余控制器混合在一起。只要GETPOST是我使用的唯一HTTP方法,该页面就可以像超级按钮一样工作。一旦添加了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 documentation一致。
  • 如果DELETE端点不存在,则按预期返回GET /css/demo.css请求并带有HTTP 200代码。 Accept(请求标头)和Content-Type(响应标头)都标记为text/css
  • 当我添加端点时,上述请求返回一个HTTP 405Accept标头为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" />

java spring-boot spring-security http-delete x-content-type-options
1个回答
0
投票

答案比我想象的要容易,我的假设完全错误。我将@RequestMappingname属性与value属性混淆了,因此,任何URL都与该方法匹配。将name更改为value即可使用。

© www.soinside.com 2019 - 2024. All rights reserved.