广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决使用security和静态资源被拦截的问题
  • 402
分享到

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

2024-04-02 19:04:59 402人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录使用security和静态资源被拦截解决方法spring Security踩坑记录(静态资源放行异常)问题描述解决1.首先尝试使用网上的方法继承 WEBSecurityConfi

使用security和静态资源被拦截

之前的博客中我给过如何在SpringBoot中整合security,当时写的界面很简单,没有CSS样式,更谈不上静态资源,而现在在实际开发过程中经理让我们用security来开发,界面肯定不可能就是两个输入框,一个按钮就完事啊,当加上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)
                .maxAge(3600);
        super.addCorsMappings(registry);
    }
}

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

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

本文链接: https://www.lsjlt.com/news/133668.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作