Java 使用Spring Boot+;时映像上出现错误403;春季安全
我第一次试用SpringBoot,但我遇到了一个错误403,我不知道该如何处理 我已经使用thymeleaf创建了一个管理页面:Java 使用Spring Boot+;时映像上出现错误403;春季安全,java,spring,spring-security,spring-boot,Java,Spring,Spring Security,Spring Boot,我第一次试用SpringBoot,但我遇到了一个错误403,我不知道该如何处理 我已经使用thymeleaf创建了一个管理页面: <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>The Link Application</title> <link rel="stylesheet" href="css/bootstrap
<!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>
...
我有一个MVC配置类:
@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子文件夹,在执行
或
你知道为什么我在
/src/main/resources/static/img
中的图像给了我一个错误403,而/src/main/resources/static/CSS
中的CSS和/src/main/resources/static/JS
中的JS没有错误吗 我认为需要改进的只是您的安全配置
您不需要这一行,因为这正是提供静态资产的地方。这不是一条可以访问的路径
.antMatchers("/public/**", "/resources/**","/resources/public/**").permitAll()
对于这一行,尝试将.anonymous()
更改为.permitAll()
,您应该能够访问图像
.antMatchers("/css/**", "/js/**", "/img/**", "**/favicon.ico").anonymous()
我想补充一些内容,答案对我很有帮助,但我犯了另一个错误。当我加上
.antMatchers("/assets/css/**", "/assets/js/**", "/assets/img/**", "**/favicon.ico").permitAll();
错误代码403
更改为404
。因为我忘了加
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}
这是我从你的房间里找到的。
我希望其他人不要重复我的错误。太棒了,真管用!
anonymous()
的作用是什么?我可以看到允许所有的请求,匿名我不清楚。我从来没有在自己编写的任何应用程序中使用过它,但是你可以把匿名认证看作是具有一定限制的实际用户。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}