思路:
调用登录接口时,使用jwt生成token,前端调用接口时在请求头中传入token
调用请求时通过拦截去拦截,获取请求头里的token进行校验并将用户信息保存到threadlocal中
线程执行完后清除threadloal数据 (threadlocal中数据线程执行完毕后不会自动清空,需手动清除,否则可能会出现数据异常-----web中线程是从线程池中获取,创建一个线程后若此前线程的threadlocal数据未被清除,则可能会使用之前threadlocal中的数据,引起数据异常)
登录:
//根据用户名密码校验用户
@GetMapping(“login”)
public AjaxResult login(User user){
try {
//根据用户名密码校验用户
//获取token
return AjaxResult.success(getToken(user));
} catch (WxErrorException e) {
e.printStackTrace();
return AjaxResult.error(e.getError().getErrorMsg());
}
}
//生成token
public String getToken(User user) {
String token="";
Date now=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.DATE,1);
token= JWT.create(). //token创建
withA