jwt实现单点登录

1>>>maven 导入依赖 
<dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
 </dependency>
 2>>生成jwt工具类
		 public class jwtUtil{
		    //随便定义
		    public static final String SECRET="AABCCC";
		    //Token失效时间定义,这里定义2分钟过期
		    public static final int calendarField= Calendar.MINUTE;
		    private static final int calendarInterval=2;
		
		    /**
		     * 根据传入的用户名生成Token
		     * @param user_id
		     * @return
		     * @throws Exception
		     */
		    public static String createToken(Long user_id) throws Exception{
		        Date date = new Date();
		        Calendar nowTime = Calendar.getInstance();
		        nowTime.add(calendarField,calendarInterval);
		        Date expiresDate = nowTime.getTime();
		        Map<String, Object> map = new HashMap<>();
		        map.put("alg", "HS256");
		        map.put("typ", "JWT");
		        String sign = JWT.create().withHeader(map)
		                .withClaim("iss", "Service")
		                .withClaim("aud", "App")
		                .withClaim("user_id", null == user_id ? null : user_id.toString())
		                .withIssuedAt(date)
		                .withExpiresAt(expiresDate)
		                .sign(Algorithm.HMAC256(SECRET));
		        return sign;
		    }
		    /**
		     * 解密Token
		     *
		     * @param token
		     * @return
		     * @throws Exception
		     */
		    public static Map<String, Claim> verifyToken(String token) {
		        DecodedJWT jwt = null;
		        try {
		            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
		            jwt = verifier.verify(token);
		        } catch (Exception e) {
		            // e.printStackTrace();
		            // token 校验失败, 抛出Token验证非法异常
		        }
		        return jwt.getClaims();
		    }
		
		    /**
		     * 根据Token获取user_id
		     *
		     * @param token
		     * @return user_id
		     */
		    public static Long getAppUID(String token) {
		        Map<String, Claim> claims = verifyToken(token);
		        Claim user_id_claim = claims.get("user_id");
		        if (null == user_id_claim || StringUtils.isEmpty(user_id_claim.asString())) {
		            // token 校验失败, 抛出Token验证非法异常
		        }
		        return Long.valueOf(user_id_claim.asString());
		    }
		}
3>>>web层调用实现测试
@RestController
public class TestController {
    /**
     * 根据userId生成Token
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public String login(Long userId) throws Exception {
        String token = jwtUtil.createToken(userId);
        System.out.println(token);
        return token;
    }

    /**
     * 根据Token获取user_id
     * @param token
     */
    @RequestMapping("/index")
    public void index(String token) {
        Long appUID = jwtUtil.getAppUID(token);
        System.out.println(appUID);
    }
}		
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值