• 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

  • 相关阅读:
    asp.net笔记第一章
    数据库复习笔记
    tp5博客项目实战2
    springboot调整MybatisPlus全局的验证策略
    SpringBoot整合MybatisPlus,并实现新增、修改、删除、查看、分页
    springboot整合Apollo
    创建apollo项目,并发布配置
    Apollo部门管理
    搭建Apollo环境(Ubuntu-18.04.4)
    启动apollo时出现的问题,../demo.sh: 行 84: curl: 未找到命令
  • 原文地址:https://www.cnblogs.com/sharpest/p/14951058.html
Copyright © 2020-2023  润新知