JWT是一种鉴权机制,实现前后端分离登陆和权限的一种解决方式,用户在登陆之后后端生成token传到前端,以后每次的请求都携带着token到后端验证,如果过期或者失效就要求重新登陆。
具体详情请看:https://blog.csdn.net/weixin_53312997/article/details/126938201
后端如何实现token
<dependency>
<groupId>com.auth0groupId>
<artifactId>java-jwtartifactId>
<version>3.4.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.78version>
dependency>
import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Base64;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* create by fzg
* 2021/10/12 9:41
*/
public class JwtUtil {
private static String TOKEN = "token!Q@W3e4r";
/*** 生成token * @param map //传入payload * @return 返回token */
public static String getToken(Map<String,String> map){
JWTCreator.Builder builder = JWT.create();
map.forEach((k,v)->{
builder.withClaim(k,v); });
Calendar instance = Calendar.getInstance();
instance.add(Calendar.HOUR,24*3);
builder.withExpiresAt(instance.getTime());
return builder.sign(Algorithm.HMAC256(TOKEN)).toString();
}/**
7.整合springboot * 验证token * @param token