| | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.http.useragent.UserAgent; |
| | | import cn.hutool.http.useragent.UserAgentUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.jwt.JWTUtil; |
| | | import cn.hutool.jwt.signers.JWTSigner; |
| | | import cn.hutool.jwt.signers.JWTSignerUtil; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.core.service.TokenService; |
| | |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.ip.AddressUtils; |
| | | import io.jsonwebtoken.Claims; |
| | | import io.jsonwebtoken.Jwts; |
| | | import io.jsonwebtoken.SignatureAlgorithm; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | |
| | | String token = getToken(request); |
| | | if (StringUtils.isNotEmpty(token)) { |
| | | try { |
| | | Claims claims = parseToken(token); |
| | | JSONObject claims = parseToken(token); |
| | | // 解析对应的权限以及用户信息 |
| | | String uuid = (String) claims.get(Constants.LOGIN_USER_KEY); |
| | | String uuid = claims.getStr(Constants.LOGIN_USER_KEY); |
| | | String userKey = getTokenKey(uuid); |
| | | LoginUser user = RedisUtils.getCacheObject(userKey); |
| | | return user; |
| | |
| | | * @return 令牌 |
| | | */ |
| | | private String createToken(Map<String, Object> claims) { |
| | | String token = Jwts.builder() |
| | | .setClaims(claims) |
| | | .signWith(SignatureAlgorithm.HS512, tokenProperties.getSecret()).compact(); |
| | | JWTSigner signer = JWTSignerUtil.hs512(tokenProperties.getSecret().getBytes()); |
| | | String token = JWTUtil.createToken(claims, signer); |
| | | return token; |
| | | } |
| | | |
| | |
| | | * @param token 令牌 |
| | | * @return 数据声明 |
| | | */ |
| | | private Claims parseToken(String token) { |
| | | return Jwts.parser() |
| | | .setSigningKey(tokenProperties.getSecret()) |
| | | .parseClaimsJws(token) |
| | | .getBody(); |
| | | private JSONObject parseToken(String token) { |
| | | JWTSigner signer = JWTSignerUtil.hs512(tokenProperties.getSecret().getBytes()); |
| | | return JWTUtil.parseToken(token).setSigner(signer).getPayload().getClaimsJson(); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public String getUsernameFromToken(String token) { |
| | | Claims claims = parseToken(token); |
| | | return claims.getSubject(); |
| | | JSONObject claims = parseToken(token); |
| | | return claims.getStr("sub"); |
| | | } |
| | | |
| | | /** |