我是靠谱客的博主 动听自行车,这篇文章主要介绍解决使用security和静态资源被拦截的问题,现在分享给大家,希望可以做个参考。

使用security和静态资源被拦截

之前的博客中我给过如何在springboot中整合security,当时写的界面很简单,没有CSS样式,更谈不上静态资源,而现在在实际开发过程中经理让我们用security来开发,界面肯定不可能就是两个输入框,一个按钮就完事啊,当加上CSS样式的时候问题就来了。

首先是CSS样式没办法被加载,其次登录之后跳转的路径出错,随机跳转到一个CSS文件中,这让我很烦恼,查了很多资料,也问了很多前辈之后终于解决了这个问题。

解决方法

下面我给出具体的代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private UserDetailsServiceImpl uds; protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login","/css/**","/image/*").permitAll() .anyRequest().authenticated().and().formLogin() .loginPage("/login").defaultSuccessUrl("/index").permitAll().and().logout().permitAll(); } public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(uds); } }

上面给出了不被拦截的一些静态资源的路径 **表示可以跨文件夹

Spring Security踩坑记录(静态资源放行异常)

问题描述

今天使用springboot整合springsecurity,出现静态资源404的状态

解决

1.首先尝试使用网上的方法继承 WebSecurityConfigurerAdapter

然后重写public void configure(WebSecurity web)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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产生了冲突

也就是我项目中在其他位置配置了跨域的内容,如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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 注释掉,最终问题解决~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是动听自行车最近收集整理的关于解决使用security和静态资源被拦截的问题的全部内容,更多相关解决使用security和静态资源被拦截内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(370)

评论列表共有 0 条评论

立即
投稿
返回
顶部