您的位置 首页 java

java SpringBoot登录验证token拦截器

用户访问接口验证,如果用户没有登录,则不让他访问除登录外的任何接口。

实现思路:

1.前端登录,后端创建token(通过JWT这个依赖),返给前端

2.前端访问其他接口,传递token,后端判断token存在以或失效

3.失效或不存在,则返回失效提示,前端根据接口返回的失效提示,让其跳转到登录界面

目录

注解的作用说明@Target代表此注解,能@到哪些代码上

返回值-全局异常类定义

程序员 使用:方法不加注解,测试

程序员使用:加上,调用通过,注解

拓展:从请求中获取token

注解 定义

定义2个注解,1个用于 任何接口都能访问 ,另外一个用于 需要登录才能访问

调用都通过注解

 package com.example.etf.story.tools;

import  java .lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PassToken {
     boolean  required() default true;
}  

登录才能通过

 package com.example.etf.story.tools;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserLoginToken {
    boolean required() default true;
}  

注解的作用说明@Target代表此注解,能@到哪些代码上

@Target :注解的作用目标

@Target(ElementType.TYPE) ——接口、类、枚举、注解
@Target(ElementType.FIELD) ——字段、枚举的常量
@Target(ElementType.METHOD) ——方法
@Target(ElementType.PARAMETER) ——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE) ——局部变量
@Target(ElementType. annotation _TYPE) ——注解
@Target(ElementType.PACKAGE) ——包

@Retention :注解的保留位置

RetentionPolicy.SOURCE :这种类型的 Annotations 只在源代码级别保留,编译时就会被忽略,在 class 字节码 文件中不包含。

RetentionPolicy.CLASS :这种类型的 Annotations 编译时被保留,默认的保留策略,在 class 文件中存在,但 JVM 将会忽略,运行时无法获得。

@Document :说明该注解将被包含在 javadoc

@Inherited :说明子类可以继承父类中的该注解

token生成与验证

传送门

然后springBoot拦截器验证token

拦截器定义

拦截器配置定义

拦截器拦截,除了登录和发送短信,不拦截,其他都拦截

 package com.example.etf.story.tools;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.Interceptor Registry ;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import  javax .annotation.Resource;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Resource
     private  LoginInterceptor loginInterceptor;
    @Override
    public  void  addInterceptors(InterceptorRegistry registry) {
        //注册自己的拦截器,并设置拦截的请求路径
        //addPathPatterns为拦截此请求路径的请求
        //excludePathPatterns为不拦截此路径的请求
        registry.addInterceptor(loginInterceptor).addPathPatterns("/story/*").excludePathPatterns("/story/sendSMS")
                .excludePathPatterns("/story/signOrRegister");
    }
}  

拦截的时候,调用的方法,给谁通过

其中service查询数据库,有没有用户,的方法要自己写

拦截器的方法执行类

 package com.example.etf.story.tools;

import com.auth0.jwt.JWT;
import com.auth0.jwt. Exception s.JWT decode Exception;

import com.example.etf.story.dao.R;
import com.example.etf.story.paramer.UserInfoParam;
import com.example.etf.story.service.TestClientService;
import com.example.etf.story.service.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet Request ;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Slf4j
@Component
public class LoginInterceptor  extends  R implements HandlerInterceptor {
    /**
     * 目标方法执行前
     * 该方法在 控制器 处理请求方法前执行,其返回值表示是否中断后续操作
     * 返回 true 表示继续向下执行,返回 false 表示中断后续操作
     *
     * @return
     */    @Resource
    private TestClientService testClientService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.get Header ("token");// 从 http 请求头中取出 token
        // 如果不是映射到方法直接通过
         if  (!(handler  instanceof  HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        //检查方法是否有passtoken注解,有则跳过认证,直接通过
        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }
        //检查有没有需要用户权限的注解
        if (method.isAnnotationPresent(UserLoginToken.class)) {
            UserLoginToken userLoginToken = method.getAnnotation(UserLoginToken.class);
            if (userLoginToken.required()) {
                // 执行认证
                if (token == null) {
                    throw new RuntimeException("无token,请重新登录");
                }
                // 获取 token 中的 user id
                String phone;
                try {
                    phone = JWT.decode(token).getClaim("phone").asString();
                } catch (JWTDecodeException j) {
                    throw new RuntimeException("token不正确,请不要通过非法手段创建token");
                }
                //查询数据库,看看是否存在此用户,方法要自己写
                UserInfoParam userInfoParam = testClientService.selectUserByPhone(phone);
                if (userInfoParam == null) {
                    throw new RuntimeException("用户不存在,请重新登录");
                }

                // 验证 token
                if (TokenUtils.verify(token)) {
                    return true;
                } else {
                    throw new RuntimeException("token过期或不正确,请重新登录");
                }

            }
        }
          throw new RuntimeException("没有权限注解一律不通过");
    }




        /**
         * 目标方法执行后
         * 该方法在控制器处理请求方法调用之后、解析视图之前执行
         * 可以通过此方法对请求域中的模型和视图做进一步修改
         */        @Override
        public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
        modelAndView) throws Exception {
            System.out.println("postHandle执行{}");

        }
        /**
         * 页面渲染后
         * 该方法在视图渲染结束后执行
         * 可以通过此方法实现资源清理、记录日志信息等工作
         */        @Override
        public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception
        ex) throws Exception {
            System.out.println("afterCompletion执行异常");

        }

}  

注解使用

在controller层加入注解进行测试

返回值-全局异常类定义

加入全局,异常类,这样当异常,会返回你所指定的异常

 package com.example.etf.story.tools;

import com.alibaba.fast JSON . json Object;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class GloablExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
         String  msg = e.getMessage();
        if (msg == null || msg.equals("")) {
             msg  = "服务器出错";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", msg);
        jsonObject.put("status",500)
        return jsonObject;
    }
}  

各种测试

不传token

成功

制造可行的假token

我们测试一下加token后的

因为数据库里,我没有插入,所以不存在,我们在随便写个token

伪造token测试

我们在试试

程序员使用:方法不加注解,测试

程序员使用:加上,调用通过,注解

我们试试,加上通过注解

拓展:从请求中获取token

我们在试试从中获取token

结束。

文章来源:智云一二三科技

文章标题:java SpringBoot登录验证token拦截器

文章地址:https://www.zhihuclub.com/187446.shtml

关于作者: 智云科技

热门文章

网站地图