创建maven项目
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
创建AuthorityAutoConfigruation
@ConditionalOnProperty(
prefix = "spring.auth",
value = "enabled",
havingValue = "true", //spring.auth.enabled 值与 havingValue值对对比,相等则自动装载,不相等不装载。
matchIfMissing = false
)
@Import(WebConfig.class)
public class AuthorityAutoConfigruation {
}
创建WebConfig
@Import(AuthorityProperties.class)
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebConfig implements WebMvcConfigurer {
@Autowired
private AuthorityProperties authorityProperties;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorityInteceptor())
.addPathPatterns(authorityProperties.getPathPatterns())
.excludePathPatterns(authorityProperties.getExcludePathPatterns());
}
}
创建AuthorityProperties
@ConfigurationProperties(prefix = "spring.auth")
@Data
public class AuthorityProperties {
//是否开启验证
boolean enabled;
//拦截路径
String pathPatterns;
//不拦截的路径
String excludePathPatterns;
}
创建AuthorityInteceptor
public class AuthorityInteceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
token = request.getParameter("token"); // xx.com?token=we8827sd
System.out.println("----------------同志,你被拦截了----------------- ");
return true;
}
}
创建resources里面创建文件META-INF里面创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.beiyou.AuthorityAutoConfigruation
然后clean然后导包就可以了
测试类
@RestController
public class TestController {
@GetMapping("/api/test")
public String test1(){
return "1";
} @GetMapping("/login")
public String test2(){
return "2";
}
}
配置文件里面是
spring.auto.enabled=true
spring.auto.pathPatterns=/api/*
spring.auto.excludePathPatterns=/login