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

  • 相关阅读:
    汇编10:CALL和RET指令
    汇编09:转移指令的原理
    汇编08:数据处理的两个基本问题
    汇编07:定位内存地址的方法
    汇编06:包含多个段的程序
    汇编05:[BX]和loop指令
    汇编04:第一个汇编程序
    汇编03:寄存器
    C#版的mongodb最新的官方驱动2.4.0版本
    如何教你看懂复杂的正则表达式
  • 原文地址:https://www.cnblogs.com/sharpest/p/14950966.html
Copyright © 2011-2022 走看看