【问题标题】:Spring Security 4 with Spring Boot: static resource (i.e. css, js, img) 404 issue带有 Spring Boot 的 Spring Security 4:静态资源(即 css、js、img)404 问题
【发布时间】:2017-05-02 07:44:40
【问题描述】:

我正在使用带有 Spring Security 的 Spring Boot 创建启用 SSL 的 Web 应用程序(手动创建的 SSL 文件)。一切正常,但是,每当我尝试访问任何静态资源(如 css 或 js 文件)时,它都无法找到这些文件并显示以下错误:

来自“https://localhost:8443/css/lib/index.css”的资源由于 MIME 类型不匹配(X-Content-Type-Options: nosniff)而被阻止。

不仅登录页面会发生这种情况,其他页面也会发生这种情况。成功登录后,我也收到此错误。

以下是我的安全配置:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/css/**"/*, "/js/**", "/img/**", "/error"*/).permitAll()
            .anyRequest().fullyAuthenticated()
        .and()
            .formLogin()
            .loginPage("/login").permitAll()
            .defaultSuccessUrl("/home", true)
            .successHandler(authSuccessHandler)
        .and()
            .headers()
            .frameOptions().sameOrigin()
            .httpStrictTransportSecurity().disable()
        .and()
            .logout().addLogoutHandler(new LogoutHandler() {
                @Override
                public void logout(HttpServletRequest request,HttpServletResponse response, Authentication authentication) {
                    request.getSession().invalidate();                  
                }
            })
            .logoutUrl("/logout").permitAll().logoutSuccessUrl("/login")
            .invalidateHttpSession(true).logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .deleteCookies("JSESSIONID")
        .and()
            .sessionManagement().maximumSessions(1);
    }

css文件放在以下路径:

src/main/resources/public/css/lib/index.csss

我正在使用 Freemarker,下面是我如何调用这个 css 文件:

<link rel="stylesheet" href="/css/lib/index.css" type="text/css" media="screen" charset="utf-8"/>

我在这里做错了什么?

【问题讨论】:

  • 您是否尝试禁用content-type-options
  • 我试过 .headers() .frameOptions().sameOrigin() .httpStrictTransportSecurity().disable()
  • 尝试禁用 contect_type_options.only 没有嗅探警告。但仍然得到 404
  • 你会关心发布请求并响应 index.css 的 http 标头吗?使用 Chrome 应该很容易。来自 web.debug(true) 的日志也会有所帮助。
  • 你能告诉我如何在 chrome 中做到这一点吗?

标签: java spring spring-mvc spring-boot spring-security


【解决方案1】:

如果您没有使用 Boot 进行 WebMvc 的 AutoConfiguration 并且有一个配置类,该配置类具有扩展 WebMvcConfigurerAdapter 的注释 @EnableWebMvc

然后重写下面的方法,它为/public/**添加所需的位置

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/public/**")
        .addResourceLocations("/public/");

并在安全配置文件中,添加以下内容:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/public/**");
}
}

这将确保 /public/** 从安全预览中排除

【讨论】:

    【解决方案2】:

    尝试完全忽略这些资源并将其排除在处理之外:

    @Override
            public void configure(WebSecurity web) throws Exception {
                web.ignoring().antMatchers("/css/**","/js/**","/img/**","/error/**");
            }
    

    【讨论】:

    • 为什么这么难?这些都不行!一个多星期以来一直在尝试让 css 通过!!!!。我允许它仍然被拒绝的一切!
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2017-11-02
    • 2016-05-04
    • 2017-05-09
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多