手写Starter

创建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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值