【发布时间】: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 子文件夹,它们在<link rel="stylesheet" href="css/bootstrap.min.css"/> 或<script src="js/jquery-2.2.1.min.js"></script>
知道为什么我在/src/main/resouces/static/img 中的图像给我一个错误 403 而/src/main/resouces/static/css 中的 CSS 和/src/main/resouces/static/js 中的 JS 不是吗?
【问题讨论】:
标签: java spring spring-security spring-boot