认识token
所谓token,既用户能够在一定时间内证明自己身份的一长串字符串。正常的使用流程为:用户第一次登入——》服务器为该用户签发一份token——》进行其他服务请求时携带上token——》服务器判断此token在有效期内——》放行此次请求。
在上述过程中,用户只有在请求特定的接口时可以不用携带token,例如登入、请求一些基本的公共信息等。
通过token实现用户互踢
通过上述我们知道,用户在请求一些接口时需要用到token进行校验。那么要想通过token实现用户互踢的功能,其实就变得简单了。具体思路为:
①:设立一份token白名单
②:同一个账号多次登入时,新登入的用户将之前登入的用户token挤出白名单
这里需要注意的是:token无法主动设置某个token为无效状态。这也就意味着,我们需要设置一份白名单或者黑名单。
白名单:只有在白名单内的token才算是有效的。
黑名单:在黑名单内的token都是无效的。
具体实现
这里我使用的是白名单的方法,之所以使用白名单,是因为使用白名单所占用的空间小,因为一个用户正在有效的token只会有一个,而其无效的token可能会有多个。具体步骤如下:
1、token的实现
package org.example.untils;import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;import java.util.Date;
import java.util.HashMap;
import java.util.Map;public class Token {/*** 过期30分钟* */private static final long EXPIRE_TIME = 30 * 60 * 1000;/*** jwt密钥* */private static final String SECRET = "jwt_secret";public static Map<String,String> map=new HashMap<>();//存放token的map集合public static Map<String, String> getMap() {return map;}public static void setMap(Map<String, String> map) {Token.map = map;}/*** 生成jwt字符串,30分钟后过期 JWT(json web token)* @param account* @return* */public static String sign(String account) {//签发tokentry {Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);Algorithm algorithm = Algorithm.HMAC256(SECRET);return JWT.create()//将account保存到token里面.withAudience(account)//五分钟后token过期.withExpiresAt(date)//token的密钥.sign(algorithm);} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据token获取account* @param token* @return* */public static String getAccount(String token) {try {String account = JWT.decode(token).getAudience().get(0);return account;}catch (JWTDecodeException e) {return null;}}/*** 校验token* @param token* @return* */public static boolean checkSign(String token) {try {Algorithm algorithm = Algorithm.HMAC256(SECRET);JWTVerifier verifier = JWT.require(algorithm)//.withClaim("username, username).build();verifier.verify(token);return true;} catch (JWTVerificationException e) {System.out.println("token无效");String account=Token.getAccount(token);//将该token从白名单内移除if(account!=null){Token.map.remove(account);//移出白名单}return false;//token无效}}
}
上述为token的实现,其中包括了token的签发,验证以及根据token获取账号。
2、用户登入时的方法实现
@GetMapping("/login")//用户登入public Result login(@Param("account") String account,@Param("password") String password){User user = userServiceImpl.login(account);if (user==null){return new Result(201,"账号不存在",null);} else if(user.getPassword().equals(password)){//密码正确Token.map.remove(account);//移除之前的tokenString token=Token.sign(account);//签发新的tokenToken.map.put(account,token);//将新的token移入有效tokenuser.setPassword("");return new Result(200,token,user);} else {return new Result(201,"账号/密码错误",null);}}
从上述代码可见,当登入成功时,会将上一次登入时留下的token移除白名单,并将最近登入生成的token放入白名单。
3、通过拦截器进行校验
package org.example.untils;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;public class Interceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {String token=request.getHeader("token");String account=Token.getAccount(token);//通过token获取用户账号String usingToken=Token.map.get(account);//获取该用户最新的有效tokenif(!Objects.equals(token, usingToken)){//该token已经失效response.setStatus(401);return false;}//检查tokenif(Token.checkSign(token)){return true;} else {response.setStatus(401);return false;}}
}
在这里,我们会判断用户携带的token是否存在于白名单内,不存在则说明这次携带的token是无效的。
4、对拦截器进行注册并放行登入等接口
package org.example.untils;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration//定义此类为配置类
public class InterceptorConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {//创建用户拦截器对象并指定其拦截的路径和排除的路径registry.addInterceptor(new Interceptor()).addPathPatterns("/**").excludePathPatterns("/user/login","/admin/login","/service/getAllServices","/shop/getShopById","/img/**");}
}
上述代码为放行了登入等接口。
总结
当然,在此次我的白名单使用的是Map存储的。网络上也有使用redis的好像,因为我目前并没有学习redis,大家感兴趣的可以试试使用redis。