zoukankan      html  css  js  c++  java
  • Spring Security踩坑记录(静态资源放行异常)

    转自: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

  • 相关阅读:
    [HNOI2006]超级英雄
    [CTSC1999]家园
    火星探险问题
    [HNOI2008]GT考试
    [USACO14DEC]后卫马克Guard Mark
    [NOI2018]归程
    [USACO15DEC]最大流Max Flow
    [NOIPlus]斗地主
    [LUOGU] P3128 [USACO15DEC]最大流Max Flow
    [BZOJ] 1878: [SDOI2009]HH的项链
  • 原文地址:https://www.cnblogs.com/sharpest/p/14951058.html
Copyright © 2011-2022 走看看