栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 手写自定义 登录拦截器Starter启动器,并使用自定义的启动类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 手写自定义 登录拦截器Starter启动器,并使用自定义的启动类

开发步骤
1.新建一个普通的maven项目

2.自定义Starter启动器,最终项目结构如下

  1. 引入以下依赖
 
            org.springframework.boot
            spring-boot-autoconfigure
            2.7.0
        
        
            org.springframework
            spring-webmvc
            5.3.20
        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            org.projectlombok
            lombok
            true
             1.18.24
        
        
            cn.hutool
            hutool-all
             5.8.2
        

4.创建属性绑定文件AuthorityProperties

package com.example;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
//引入配置前缀为spring.auth的配置文件
@ConfigurationProperties(prefix = "spring.auth")
@Data
public class AuthorityProperties {

    //是否开启验证
    boolean enabled;
    //拦截路径
    String pathPatterns;
    //不拦截的路径
    String excludePathPatterns;
}

5.创建自动配置类AuthorityAutoConfigruation

package com.example;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Import;

@ConditionalOnProperty(
        prefix = "spring.auth",
        value = "enabled",
        havingValue = "true", //spring.auth.enabled 值与 havingValue值对对比,相等则自动装载,不相等不装载。
        matchIfMissing = false
)
//注入拦截器的配置
@Import(WebConfig.class)
public class AuthorityAutoConfigruation {
}

6.创建登录拦截器AuthorityInteceptor

package com.example;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthorityInteceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //String token = request.getHeader("token");
        //String token = request.getParameter("token"); // xx.com?token=we8827sd
        System.out.println("----------------同志,你被拦截了----------------- ");
        return true;
    }
}

7.注册拦截器WebConfig

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@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());

    }
}

8.配置自动配置
在resourecs文件目录下创建META-INF,并创建我们自己的spring.factories,并把我们的 MemberStaterAutoConfiguration 添加进去

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.AuthorityAutoConfigruation

9.打包,上传到阿里云效的制品仓库的非生产库-snapshot里面
9.1上传的前提条件,下载setting.xml,覆盖原来的maven setting.xml文件

9.2上传点击depoy自动上传到实仓

10.导入打包上传好的依赖

11开始使用,新建spring boot项目,引入刚刚那个依赖

12.编写测试demoController

package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {

    @GetMapping("/api/name")
    public String getName(){
        return "李老师";
    }
    @GetMapping("/login")
    public String login(){
        return "请登录";
    }
}

13配置application.properties

#spring.auth.enabled 值与 havingValue值对对比,相等则自动装载,不相等不装载。
spring.auth.enabled=true
#拦截所有
spring.auth.pathPatterns=/api/*
#放行的路径
spring.auth.excludePathPatterns=/login 

14.测试结果
14.1测试接口/api/name,启动器使用成功

14.2测试接口/login,测试成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号