一、使用antMatchers放行静态资源:

package cn.zeal4j.configuration;

import cn.zeal4j.handler.FarsAuthenticationFailureHandler;
import cn.zeal4j.handler.FarsAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @file IntelliJ IDEA Spring-Security-Tutorial
 * @create 2020 09 27 21:55
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.formLogin(). // 设置登陆行为方式为表单登陆
                // 登陆请求参数设置
                usernameParameter("username").
                passwordParameter("password").
                loginPage("/login.html"). // 设置登陆页面URL路径
                loginProcessingUrl("/login.action"). // 设置表单提交URL路径
                // successForwardUrl("/main.page"). // 设置认证成功跳转URL路径 POST请求
                successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定义的重定向登陆
                // failureForwardUrl("/error.page");  // 设置认证失败跳转URL路径 POST请求
                failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域处理,不需要跳转了

        httpSecurity.authorizeRequests().
                antMatchers("/**/*.js", "/**/*.css", "/**/images/**/*.*").permitAll(). // 静态资源放行
                antMatchers("/login.html").permitAll(). // 登陆页面允许任意访问
                antMatchers("/error.html").permitAll(). // 失败跳转后重定向的页面也需要被允许访问
                anyRequest().authenticated(); // 其他请求均需要被授权访问

        // CSRF攻击拦截关闭
        httpSecurity.csrf().disable();
    }
}

一般JS和CSS这两个很好确定,因为后缀很简单就是js + css 所以对URL的匹配规则只要是后缀符合这两个条件的就放行

但是如果算图片在内的话就不行了,因为图片的种类非常多,无法根据后缀名拦截,所以我这里折中的办法是指定一个

目录存放图片。

二、使用regxMatchers放行资源:

如果这种常规的过滤设置不够简单,Secuirty还提供了正则表达式拦截处理:

package cn.zeal4j.configuration;

import cn.zeal4j.handler.FarsAuthenticationFailureHandler;
import cn.zeal4j.handler.FarsAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @file IntelliJ IDEA Spring-Security-Tutorial
 * @create 2020 09 27 21:55
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.formLogin(). // 设置登陆行为方式为表单登陆
                // 登陆请求参数设置
                usernameParameter("username").
                passwordParameter("password").
                loginPage("/login.html"). // 设置登陆页面URL路径
                loginProcessingUrl("/login.action"). // 设置表单提交URL路径
                // successForwardUrl("/main.page"). // 设置认证成功跳转URL路径 POST请求
                successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定义的重定向登陆
                // failureForwardUrl("/error.page");  // 设置认证失败跳转URL路径 POST请求
                failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域处理,不需要跳转了

        httpSecurity.authorizeRequests().
                regexMatchers(HttpMethod.POST, "正则表达式").permitAll(). // 还可以对符合正则表达式的请求方式进行要求,这个属性使用来制定请求的方式
                antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 静态资源放行
                antMatchers("/login.html").permitAll(). // 登陆页面允许任意访问
                antMatchers("/error.html").permitAll(). // 失败跳转后重定向的页面也需要被允许访问
                anyRequest().authenticated(); // 其他请求均需要被授权访问

        // CSRF攻击拦截关闭
        httpSecurity.csrf().disable();
    }
}

三、使用mvcMatchers访问资源

使用该方法,适用于配置MVC-Servlet-Path的一个情况

MSP是对整个项目的地址增加前缀,也就是

http://localhost:8080/MSP前缀/你的具体的控制器接口

MVC-Servlet-Path的配置:

application.yml配置

spring:
  # spring.mvc.servlet.path
  mvc:
    servlet:
      path: /xxx

application.properties配置

spring.mvc.servlet.path = /xxx

我的疑问:
除了MVC之外,其实SpringBoot还允许设置项目根路径:

server:
  servlet:
    context-path: /ctx-path

那么是不是应该连接起来的情况:

http://localhost:8080/ctx-path/MSP前缀/你的具体的控制器接口

mvcMatchers使用:

上述的项目URL就原封不动了,项目根路径 + MSP路径:

package cn.zeal4j.configuration;

import cn.zeal4j.handler.FarsAuthenticationFailureHandler;
import cn.zeal4j.handler.FarsAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @file IntelliJ IDEA Spring-Security-Tutorial
 * @create 2020 09 27 21:55
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.formLogin(). // 设置登陆行为方式为表单登陆
                // 登陆请求参数设置
                usernameParameter("username").
                passwordParameter("password").
                loginPage("/login.html"). // 设置登陆页面URL路径
                loginProcessingUrl("/login.action"). // 设置表单提交URL路径
                // successForwardUrl("/main.page"). // 设置认证成功跳转URL路径 POST请求
                successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定义的重定向登陆
                // failureForwardUrl("/error.page");  // 设置认证失败跳转URL路径 POST请求
                failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域处理,不需要跳转了

        httpSecurity.authorizeRequests().
                regexMatchers(HttpMethod.POST, "正则表达式").permitAll(). // 还可以对符合正则表达式的请求方式进行要求,这个属性使用来制定请求的方式
                antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 静态资源放行
                antMatchers("/login.html").permitAll(). // 登陆页面允许任意访问
                antMatchers("/error.html").permitAll().
                mvcMatchers("/main.page").servletPath("/xxx").permitAll().// 失败跳转后重定向的页面也需要被允许访问
                anyRequest().authenticated(); // 其他请求均需要被授权访问

        // CSRF攻击拦截关闭
        httpSecurity.csrf().disable();
    }
}

访问测试:

【Spring-Security】Re04 Matchers配置规则API_spring

或者自行补上MSP前缀

package cn.zeal4j.configuration;

import cn.zeal4j.handler.FarsAuthenticationFailureHandler;
import cn.zeal4j.handler.FarsAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @file IntelliJ IDEA Spring-Security-Tutorial
 * @create 2020 09 27 21:55
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.formLogin(). // 设置登陆行为方式为表单登陆
                // 登陆请求参数设置
                usernameParameter("username").
                passwordParameter("password").
                loginPage("/login.html"). // 设置登陆页面URL路径
                loginProcessingUrl("/login.action"). // 设置表单提交URL路径
                // successForwardUrl("/main.page"). // 设置认证成功跳转URL路径 POST请求
                successHandler(new FarsAuthenticationSuccessHandler("https://www.acfun.cn/")). // 使用自定义的重定向登陆
                // failureForwardUrl("/error.page");  // 设置认证失败跳转URL路径 POST请求
                failureHandler(new FarsAuthenticationFailureHandler("/error.html")); // 跨域处理,不需要跳转了

        httpSecurity.authorizeRequests().
                regexMatchers(HttpMethod.POST, "正则表达式").permitAll(). // 还可以对符合正则表达式的请求方式进行要求,这个属性使用来制定请求的方式
                antMatchers("/**/*.js", "/**/*.css", "/**/images/*.*").permitAll(). // 静态资源放行
                antMatchers("/login.html").permitAll(). // 登陆页面允许任意访问
                antMatchers("/error.html").permitAll(). // 失败跳转后重定向的页面也需要被允许访问
                // mvcMatchers("/main.page").servletPath("/xxx").permitAll(). // mvcMatchers资源放行匹配
                antMatchers("/xxx/main.page").permitAll(). // 或者多写MSP的前缀        
                anyRequest().authenticated(); // 其他请求均需要被授权访问

        // CSRF攻击拦截关闭
        httpSecurity.csrf().disable();
    }
}