解决使用security和静态资源被拦截的问题

网友投稿 1191 2022-12-14

解决使用security和静态资源被拦截的问题

解决使用security和静态资源被拦截的问题

目录使用security和静态资源被拦截解决方法Spring Security踩坑记录(静态资源放行异常)问题描述解决1.首先尝试使用网上的方法继承 WebSecurityConfigurerAdapter2.于是我又重写了方法 protected void configure(HttpSecurity http)3.最终发现是跨域配置和springsecurity产生了冲突

使用security和静态资源被拦截

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

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

解决方法

下面我给出具体的代码

@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)

@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)

http:// .maxAge(3600);

super.addCorsMappings(registry);

}

}

把 CORSConfiguration 注释掉,最终问题解决~

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用springcloud+oauth2携带token去请求其他服务
下一篇:从实战角度详解Disruptor高性能队列
相关文章

 发表评论

暂时没有评论,来抢沙发吧~