1

我正在使用 Spring Boot v2.1.9.RELEASE 和 Thymeleaf 开发一个 Web 应用程序。该应用程序将服务器端渲染(Spring MVC 常规控制器)与通过 AJAX 调用的休息控制器混合在一起。只要是我使用的唯一 HTTP 方法,GET该页面就像一个魅力。POST一旦我添加了一个完全不相关但未使用的DELETEREST 控制器,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都标记为.AcceptContent-Typetext/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" />

4

1 回答 1

0

答案比我想象的要容易,而且我的假设完全错误。@RequestMapping我将的name属性与属性混淆了value,因此任何 URL 都与该方法匹配。更改namevalue使其工作。

于 2020-01-03T17:08:35.263 回答