【发布时间】:2019-10-20 22:32:04
【问题描述】:
我正在尝试获取 spring 安全性以允许提供静态文件,如 .css .js 等,而无需先登录。
我尝试使用资源处理程序创建 MVC 配置并更改 spring 安全配置中的规则,但似乎没有任何效果。
MvcConfig.java:
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**")
.addResourceLocations("/assets/");
}
}
SecurityConfig.java:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/assets/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/assets/**");
}
}
当我转到 http://localhost:8080/assets/js/particles.min.js 时,我希望它返回文件内容,但每次我尝试像 localhost:8080/assets/* 这样的链接时,它都会返回 login.html 的内容
【问题讨论】:
-
我使用的是 2.1.5,我在代码中添加了@Configuration,但问题仍然存在
-
link 这个项目是今天早上新创建的,我一直在努力解决这个问题,我只有两个文件,我包含了指向它们内容的链接,但我不认为他们会有所帮助。这是我第一个使用 Spring Security 的项目,所以可能在某个地方我犯了一些我看不到的简单错误。我还用我的文件截图更新了帖子。
-
它在子包配置中。
-
哦,对不起,我刚刚意识到发生了什么,我碰巧刷新页面,同时它在断点处停止,我认为这是每个请求。只需再次检查,你就对了,它是在开始时。
-
没有任何变化
标签: java spring spring-boot spring-mvc spring-security