1.登录成功后加入jwt令牌
@RequestMapping("/login") public R login(String userName,String password){ Map map = memberService.login(userName, password); Object member = map.get("member"); if(null==member){ return R.error("登录失败").put("map",map); } //登录成功获取令牌 String jwt = JWTUtil.generateToken(userName); map.put("jwt",jwt); return R.ok().put("map",map); }
1.1。令牌工具类----先引入依赖
<dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.10.3</version> </dependency>
工具类
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; public class JWTUtil { public static final String SECRET_KEY = "u-member-offcn123"; //秘钥 public static final long TOKEN_EXPIRE_TIME = 5 * 60 * 1000; //token过期时间 public static final long REFRESH_TOKEN_EXPIRE_TIME = 10 * 60 * 1000; //refreshToken过期时间 private static final String ISSUER = "shijian"; //签发人 //生成签名 public static String generateToken(String username){ Date now=n