【问题标题】:Error 403 on image when using Spring Boot + Spring Security使用 Spring Boot + Spring Security 时图像上出现错误 403
【发布时间】:2023-03-26 09:20:01
【问题描述】:

我是第一次尝试 Spring Boot,但遇到了一个错误 403,我不知道如何解决

我使用 thymeleaf 创建了一个管理页面:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>The Link Application</title>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
</head>
<body>

<nav class="navbar navbar-default">
    <div class="container-fluid">

        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle Navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">
                <img src="img/Company-logo-sm-header.png" />
            </a>
        </div>
    </div>
</nav>

...

CSS 加载完美,位于 src/main/resources/static/css,给我错误 403 的图像位于 src/main/resources/static/img

这是我的Application 班级:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

}

我有一个 MVC Config 类:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/home").setViewName("home");
    registry.addViewController("/").setViewName("home");
    registry.addViewController("/hello").setViewName("hello");
    registry.addViewController("/login").setViewName("login");
  }

}

还有一个安全配置,我不确定我是否正确使用它,antMatchers(...).permitAll() 对我来说似乎应该允许图像:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    http
        .authorizeRequests()
          .antMatchers("/public/**", "/resources/**","/resources/public/**").permitAll()
          .antMatchers("/", "/home", "/link").permitAll()
          .antMatchers("/css/**", "/js/**", "/img/**", "**/favicon.ico").anonymous()
          .antMatchers("/admin").hasRole("ADMIN")
        .anyRequest().authenticated().and()
        .formLogin().loginPage("/login").permitAll().and()
        .logout().permitAll();
  }

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
        .withUser("admin").password("admin").roles("USER", "ADMIN").and()
        .withUser("user").password("user").roles("USER");
  }

}

我正在使用 Spring Boot 1.3.3 我在/src/main/resources 中没有公共目录,我所有的静态内容都进入/src/main/resources/static,css 进入css 子文件夹,js 进入js 子文件夹,它们在&lt;link rel="stylesheet" href="css/bootstrap.min.css"/&gt;&lt;script src="js/jquery-2.2.1.min.js"&gt;&lt;/script&gt;

知道为什么我在/src/main/resouces/static/img 中的图像给我一个错误 403 而/src/main/resouces/static/css 中的 CSS 和/src/main/resouces/static/js 中的 JS 不是吗?

【问题讨论】:

    标签: java spring spring-security spring-boot


    【解决方案1】:

    我认为只是您的安全配置需要改进。

    您不需要此行,因为这正是提供静态资产的地方。这不是一条可访问的路径。

    .antMatchers("/public/**", "/resources/**","/resources/public/**").permitAll()
    

    至于这一行,尝试将.anonymous()更改为.permitAll(),您应该可以访问图像。

    .antMatchers("/css/**", "/js/**", "/img/**", "**/favicon.ico").anonymous()
    

    【讨论】:

    • 好东西,很管用! anonymous() 有什么作用? permitAll() 我可以看到是允许所有请求,匿名我不清楚。
    • 我自己从未在我编写的任何应用程序中使用过它,但您可以将匿名身份验证视为具有某些限制的实际用户。
    【解决方案2】:

    我想在above Patrick answer 中添加一些内容,答案对我有所帮助,但我又犯了一个错误。当我添加

    .antMatchers("/assets/css/**", "/assets/js/**", "/assets/img/**", "**/favicon.ico").permitAll();
    

    403 的错误代码更改为 404。因为忘记加了

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

    我是从another source 找到的。 我希望其他人不要重蹈我的覆辙。

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2021-03-11
      • 2017-10-03
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      相关资源
      最近更新 更多