当前位置 博文首页 > 文章内容

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

    作者:shunshunshun18 栏目:未分类 时间:2021-08-26 14:42:48

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    使用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 注释掉,最终问题解决~

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持IIS7站长之家博文。