有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring Boot应用程序即使在加载图像时也会返回401,以获取资源中的所有静态内容

我有一个Spring Boot webapp,其中包含一些针对不同路线的安全配置。该项目的一些资源如下:

  • 资源/
  • 静止的/
    • css/
    • img/
    • js/
    • libs/
  • 模板/
  • 配置文件/
  • 申请。属性

问题是,虽然我可以通过模板引擎(thymeleaf)加载视图(没有样式),但对静态内容的所有请求都会返回401代码

现在,我搜索并找到了很多关于这个问题的内容。这里有趣的是,既没有加载js、libs也没有加载css文件,而是显示图像。澄清一下:即使是图像在显示时也会返回401代码

我发现了一些已经被问到的问题:

我已经翻阅了很多spring文档


这是我的配置、安全和;th的登录文件,我已经篡改了很多,但仍然没有任何效果:

@EnableWebMvc
@Configuration
@ImportResource({"classpath:mongo-config.xml"})
public class ProjectConfiguration implements  WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler(
            "/img/**",
            "/css/**",
            "/js/**",
            "/libs/**")
            .addResourceLocations(
                    "classpath:/static/img/",
                    "classpath:/static/css/",
                    "classpath:/static/js/",
                    "classpath:/static/libs/");
}

@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    System.out.println("Security Config");
    http    .csrf().disable()
            .httpBasic().disable()
            .authorizeRequests()
                .antMatchers("/js/**","/css/**","/img/**","/libs/**").permitAll()
                .antMatchers("/userManagement", "/userManagement/**", "/rest/auth").permitAll()
                .antMatchers("/admin", "/admin/**").hasRole("ADMIN")
                .antMatchers("/managementSettings", "/managementSettings/**").hasRole("MANAGEMENT")
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/**").hasAnyRole("USER", "DEMO", "MANAGEMENT")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .successHandler( CustomSimpleURLAuthenticationSuccesHandler())
                .failureUrl("/login?error")
                .and()
            .logout()
                .permitAll()
                .logoutRequestMatcher( new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login")
                .and()
            .addFilterAfter(AjaxRedirectFilter(), ExceptionTranslationFilter.class)
            .headers()
                .httpPublicKeyPinning()
                .addSha256Pins("*********************","*************************")
                .includeSubDomains(true)
                .maxAgeInSeconds(10000);
}

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

<div class="content">
    <img style="margin:auto" class="img-responsive" th:src="@{/img/logo.png}"/>
</div>

我想指出几件事:

  1. @{/img/logo.png}似乎是从视图请求的正确端点,因为如果我在此处更改任何内容,图像都不会显示

  2. 同样,如果我更改了安全或配置文件中的任何路由,图像不会显示,因此路由也必须正确。

  3. 如果我打开browser developer tools并检查网络选项卡,它会显示:“加载响应数据失败”,但如果双击该文件,它会以明文形式打开js资源,从服务器加载dev tools capture&plain text jQuery display


这么说吧,我怎么能在视图中加载所有静态文件呢?我不介意使用自定义或默认路径,只要我能够访问我的文件


共 (2) 个答案

  1. # 1 楼答案

    经过大量的研究和数小时的尝试来解决这个问题,解决方案是删除一个过时的API REST代码,它会扰乱整个项目的安全性

    项目迁移的典型预期