Spring Security如何设置才能避免拦截到静态资源

继承 FilterSecurityInterceptor 自定义Spring security 拦截器,
但是每次都拦截了css等静态资源,求教如何设置。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //.addFilterAfter(getMyFilterSecurityInterceptor(), FilterSecurityInterceptor.class)
                .authorizeRequests()
                //.mvcMatchers(HttpMethod.DELETE, "/delete/**").hasRole("ADMIN")
                //.mvcMatchers(HttpMethod.GET, "/model-**").hasRole("ADMIN")
                //任何尚未匹配的URL只需要对用户进行身份验证
                .anyRequest().authenticated()
                .and()
                //登录地址,成功跳转地址,登录失败地址
                .formLogin().loginPage("/login")
                .defaultSuccessUrl("/", true)
                .failureUrl("/login?error")
                //授予所有用户以上地址的访问权限
                .permitAll();
        //设置可以iframe访问
        http.headers().frameOptions().sameOrigin();
    }
阅读 21.8k
2 个回答

可以再重写下面的方法(不影响你之前写的代码)

  @Override
  public void configure(WebSecurity web) throws Exception {
     web.ignoring().antMatchers("/druid/**");
     //可以仿照上面一句忽略静态资源
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        // .addFilterAfter(getMyFilterSecurityInterceptor(), FilterSecurityInterceptor.class)
        .authorizeRequests()
        .antMatchers(new String[]{"/js/**","/css/**","/img/**","/images/**","/fonts/**","/**/favicon.ico"}).permitAll()//
        // 任何尚未匹配的URL只需要对用户进行身份验证
        .anyRequest().authenticated().and()
        // 登录地址,成功跳转地址,登录失败地址
        .formLogin().loginPage("/login").defaultSuccessUrl("/", true).failureUrl("/login?error")
        // 授予所有用户以上地址的访问权限
        .permitAll();
        // 设置可以iframe访问
        http.headers().frameOptions().sameOrigin();
    }