| | |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.core.model.LoginUserInfo; |
| | | import com.doumee.core.utils.Constants; |
| | | import io.jsonwebtoken.Jwts; |
| | | import io.jsonwebtoken.SignatureAlgorithm; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | if(payloads == null){ |
| | | return null; |
| | | } |
| | | payloads.setLoginDate(new Date()); |
| | | payloads.setLoginDate(new Date()); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("id",payloads.getId()); |
| | | // Map<String,Object> map = BeanUtil.beanToMap(payloads); |
| | |
| | | claims.setLoginDate(new Date()); |
| | | refreshedToken = generateTokenDo(claims); |
| | | if(refreshedToken!=null){ |
| | | redisTemplate.delete(token);//删除老的token |
| | | redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token |
| | | } |
| | | } catch (Exception e) { |
| | | refreshedToken = null; |
| | | } |
| | | return refreshedToken; |
| | | } |
| | | /** |
| | | * 退出登陆 |
| | | * |
| | | * @param token 原令牌 |
| | | * @return 新令牌 |
| | | */ |
| | | public void logout(String token) { |
| | | try { |
| | | redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | .setExpiration(expirationDate) |
| | | .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) |
| | | .compact(); |
| | | redisTemplate.opsForValue().set(token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); |
| | | redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); |
| | | return token; |
| | | } |
| | | |
| | |
| | | private LoginUserInfo getClaimsFromToken(String token) { |
| | | LoginUserInfo claims; |
| | | try { |
| | | String userInfo = (String) redisTemplate.opsForValue().get(token); |
| | | String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token); |
| | | claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),LoginUserInfo.class); |
| | | } catch (Exception e) { |
| | | claims = null; |