转自:https://blog.csdn.net/qq_34204490/article/details/110005404
Springboot + Spring-Security的踩坑记录
问题描述:今天使用springboot整合springsecurity,出现静态资源404的状态
解决:
1.首先尝试使用网上的方法继承 WebSecurityConfigurerAdapter,然后重写public void configure(WebSecurity web)
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(loadExcludePath());
}
private String[] loadExcludePath() {
return new String[]{
"/",
"/static/**",
"/templates/**",
"/img/**",
"/js/**",
"/css/**",
"/lib/**"
};
}
照道理说。这应该就可以了,然而我这里就是不能成功放行
2.于是我又重写了方法 protected void configure(HttpSecurity http)
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//关闭跨域限制
.csrf().disable()
.authorizeRequests()
//在此处放行
.antMatchers(loadExcludePath()).permitAll()
.anyRequest().authenticated()//其他的路径都是登录后即可访问
.and()
.formLogin()
// .loginPage("/login")
// .loginProcessingUrl("/doLogin")
.successHandler(getAuthenticationSuccessHandler())
.failureHandler(getAuthenticationFailureHandler())
// .permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling().accessDeniedHandler(getAccessDeniedHandler());
}
这里的重点是下面几句(其他的配置可以忽略)
http
//关闭跨域限制
.csrf().disable()
.authorizeRequests()
//在此处放行
.antMatchers(loadExcludePath()).permitAll()
.anyRequest().authenticated()//其他的路径都是登录后即可访问
然而尽管标红的地方也进行了放行,可是依然失败。
到目前为止,应该是已经没问题了,毕竟两个方法中都进行了放行,可是静态资源依旧404
3.最终发现是跨域配置和springsecurity产生了冲突
也就是我项目中在其他位置配置了跨域的内容,如下
@Configuration
public class CORSConfiguration extends WebMvcConfigurationSupport {
@Override
protected void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders(
"access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-max-age",
"X-Frame-Options")
.allowCredentials(true)
.maxAge(3600);
super.addCorsMappings(registry);
}
}
把 CORSConfiguration 注释掉,最终问题解决
————————————————
版权声明:本文为CSDN博主「@小小白!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34204490/article/details/110005404
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2019-06-29 Oracle中select 1和select *的区别
2016-06-29 mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置
2016-06-29 Spring Test 整合 JUnit 4 使用总结
2016-06-29 论坛中常有的提问,评论,点赞设计