SpringBoot通过token实现用户互踢功能

认识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) {//签发token
        try {
            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);//移除之前的token
            String token=Token.sign(account);//签发新的token
            Token.map.put(account,token);//将新的token移入有效token
            user.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 {

    @Override
    public 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);//获取该用户最新的有效token
        if(!Objects.equals(token, usingToken)){//该token已经失效
            response.setStatus(401);
            return false;
        }
        //检查token
        if(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 {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建用户拦截器对象并指定其拦截的路径和排除的路径
        registry.addInterceptor(new Interceptor()).
            addPathPatterns("/**").excludePathPatterns("/user/login","/admin/login","/service/getAllServices",
                        "/shop/getShopById","/img/**");
    }
}

上述代码为放行了登入等接口。

总结

当然,在此次我的白名单使用的是Map存储的。网络上也有使用redis的好像,因为我目前并没有学习redis,大家感兴趣的可以试试使用redis。

相关推荐

  1. SpringBoot通过token实现用户功能

    2024-04-09 12:36:03       37 阅读
  2. Linux掉远程用户

    2024-04-09 12:36:03       51 阅读
  3. Vue3 实现基于token 用户登录

    2024-04-09 12:36:03       40 阅读
  4. linux掉远程登录用户*

    2024-04-09 12:36:03       54 阅读
  5. 通过Slf4j中的MDC实现在日志中添加用户IP功能

    2024-04-09 12:36:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 12:36:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 12:36:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 12:36:03       87 阅读
  4. Python语言-面向对象

    2024-04-09 12:36:03       96 阅读

热门阅读

  1. C++:万能进制转换

    2024-04-09 12:36:03       41 阅读
  2. iOS MT19937随机数生成,结合AES-CBC加密算法实现。

    2024-04-09 12:36:03       28 阅读
  3. 头歌:共享单车之数据可视化

    2024-04-09 12:36:03       39 阅读
  4. 计算机网络-ICMP和ARP协议——沐雨先生

    2024-04-09 12:36:03       39 阅读
  5. Ubuntu22.04 安装 Supabase

    2024-04-09 12:36:03       35 阅读
  6. 【力扣】238. 除自身以外数组的乘积

    2024-04-09 12:36:03       42 阅读
  7. npm的一些经常使用的命令

    2024-04-09 12:36:03       38 阅读
  8. CSS变换

    CSS变换

    2024-04-09 12:36:03      33 阅读