spring boot 配置拦截器验证使用 token 登录
2020-12-13 06:27
标签:ati uil 权限 sre text fse vcc lan poc 1、自定义登录注解 2、创建 token 实体类 3、创建处理 token 的接口方法
* 根据请求token查询token信息
* 4、创建拦截器 5、将拦截器配置给 spring boot
* 此配置类可配置拦截器、参数解析器、返回值解析器、跨域支持等等
* 6、配置已经好了,可以开始测试 spring boot 配置拦截器验证使用 token 登录 标签:ati uil 权限 sre text fse vcc lan poc 原文地址:https://www.cnblogs.com/wujiaxing/p/11180062.htmlpackage io.xiongdi.annotation;
import java.lang.annotation.*;
/**
* @author wujiaxing
* @date 2019-07-12
* 登录校验
*/
@Target(ElementType.METHOD)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Login {
}
package io.xiongdi.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 用户token
* @author wujiaxing
* @date ${2019-6-30}
*/
@Data
@TableName("tb_token")
@Builder
public class TokenEntity implements Serializable {
private static final long serialVersionUID = 5584132314624077161L;
public TokenEntity(){}
public TokenEntity(long userId, String token, LocalDateTime expireTime, LocalDateTime updateTime) {
this.userId = userId;
this.token = token;
this.expireTime = expireTime;
this.updateTime = updateTime;
}
/**
* 用户ID
*/
@TableId(type = IdType.INPUT)
private long userId;
/**
* token
*/
private String token;
/**
* 过期时间
*/
private LocalDateTime expireTime;
/**
* 修改时间
*/
private LocalDateTime updateTime;
}
package io.xiongdi.service;
import com.baomidou.mybatisplus.extension.service.IService;
import io.xiongdi.entity.TokenEntity;
/**
* token
* @author wujiaxing
* @date 2019-06-30
*/
public interface TokenService extends IService
package io.xiongdi.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xiongdi.dao.TokenDao;
import io.xiongdi.entity.TokenEntity;
import io.xiongdi.service.TokenService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;
/**
* @author wujiaxing
* @date 2019-07-08
*/
@Service("tokenService")
public class TokenServiceImpl extends ServiceImpl
package io.xiongdi.interceptor;
import io.xiongdi.annotation.Login;
import io.xiongdi.common.exception.XDException;
import io.xiongdi.common.utils.ResultType;
import io.xiongdi.entity.TokenEntity;
import io.xiongdi.service.TokenService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
/**
* 权限(token)验证
* @author wujiaxing
* @date 2019-06-30
*/
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {
@Autowired
private TokenService tokenService;
public final static String USER_KEY = "userId";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Login annotation;
// 如果处理对象是一个处理方法,则获取到方法上的注解
if (handler instanceof HandlerMethod) {
annotation = ((HandlerMethod)handler).getMethodAnnotation(Login.class);
// 否则直接放过拦截的请求
} else {
return true;
}
// 说明此方法没有Login注解
if (annotation == null) {
return true;
}
// 从请求头获取token
String token = request.getHeader("token");
// 如果请求头没有token,则从请求参数中取
if (StringUtils.isBlank(token)) {
token = request.getParameter("token");
}
// 如果还是没有token,则抛异常
if (StringUtils.isBlank(token)) {
throw new XDException(ResultType.TOKEN_NULL);
}
// 查询token信息
TokenEntity tokenEntity = tokenService.queryByToken(token);
// 如果token信息是否为null或是否过期,则抛异常
if (tokenEntity == null || tokenEntity.getExpireTime().isBefore(LocalDateTime.now())) {
throw new XDException(ResultType.TOKEN_EXPIRE);
}
// 否则,存入request作用域,后续根据userId,获取用户信息
request.setAttribute(USER_KEY, tokenEntity.getUserId());
return true;
}
}
package io.xiongdi.config;
import io.xiongdi.interceptor.AuthorizationInterceptor;
import io.xiongdi.resolver.LoginUserHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
/**
* @author wujiaxing
*
文章标题:spring boot 配置拦截器验证使用 token 登录
文章链接:http://soscw.com/essay/33077.html