From a010ec4a0b9b7c0639224e52b1b37950e33b9f46 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Thu, 2 Mar 2023 11:34:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../straw/controller/FirePointController.java | 6 +-- .../straw/controller/SysUserController.java | 16 ++++---- src/main/java/com/xkrs/utils/TokenUtil.java | 37 ++++++------------- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/xkrs/straw/controller/FirePointController.java b/src/main/java/com/xkrs/straw/controller/FirePointController.java index da7ac92..a012c71 100644 --- a/src/main/java/com/xkrs/straw/controller/FirePointController.java +++ b/src/main/java/com/xkrs/straw/controller/FirePointController.java @@ -121,7 +121,7 @@ public class FirePointController { @GetMapping("/selectUserName") public String selectUserName(@RequestHeader(value = "Authorization") String token) { // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); @@ -156,7 +156,7 @@ public class FirePointController { // 植被类型 String landType = (String) map.get("landType"); // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); // 用于查询的区划编码,可能是省市区县街道任意级别 String queryCode = TextUtils.isEmpty(cityCode) ? sysUserEntity.getCountyCode() : cityCode; @@ -185,7 +185,7 @@ public class FirePointController { @GetMapping("/selectFirePointByMonth") public String selectFirePointByMonth(@RequestHeader(value = "Authorization") String token) { // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); List firePointEntities = firePointService.selectFirePointByMonth(sysUserEntity.getCountyCode()); if (firePointEntities == null || firePointEntities.size() == 0) { diff --git a/src/main/java/com/xkrs/straw/controller/SysUserController.java b/src/main/java/com/xkrs/straw/controller/SysUserController.java index d401ed9..aed48d4 100644 --- a/src/main/java/com/xkrs/straw/controller/SysUserController.java +++ b/src/main/java/com/xkrs/straw/controller/SysUserController.java @@ -2,16 +2,16 @@ package com.xkrs.straw.controller; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.xkrs.common.encapsulation.PromptMessageEnum; -import com.xkrs.straw.model.qo.SysUserQo; -import com.xkrs.straw.model.qo.SysUserReceiveSmsQo; -import com.xkrs.straw.model.qo.SysUserRemarkQo; -import com.xkrs.straw.model.qo.SysUserVipLevelQo; import com.xkrs.model.validation.SysUserQoInsert; import com.xkrs.model.validation.SysUserQoUpdate; import com.xkrs.service.RedisService; import com.xkrs.straw.dao.SysUserDao; import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.model.helper.SMSHelper; +import com.xkrs.straw.model.qo.SysUserQo; +import com.xkrs.straw.model.qo.SysUserReceiveSmsQo; +import com.xkrs.straw.model.qo.SysUserRemarkQo; +import com.xkrs.straw.model.qo.SysUserVipLevelQo; import com.xkrs.straw.service.SysUserService; import com.xkrs.straw.utils.VipTimeRangeUtils; import com.xkrs.utils.AliYunSmsUtils; @@ -260,7 +260,7 @@ public class SysUserController { @GetMapping("/selectAllUser") public String selectAllUser(@RequestHeader(value = "Authorization") String token) { // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); @@ -280,7 +280,7 @@ public class SysUserController { Long userId = (Long) map.get("userId"); String keepType = (String) map.get("keepType"); // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); @@ -304,7 +304,7 @@ public class SysUserController { String newPassword = (String) map.get("newPassword"); String confirmPassword = (String) map.get("confirmPassword"); // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); @@ -322,7 +322,7 @@ public class SysUserController { String newPassword = (String) map.get("newPassword"); String confirmPassword = (String) map.get("confirmPassword"); // 验证token - String tokenUserName = TokenUtil.getTokenUserName(token); + String tokenUserName = TokenUtil.obtainUserNameByToken(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); diff --git a/src/main/java/com/xkrs/utils/TokenUtil.java b/src/main/java/com/xkrs/utils/TokenUtil.java index b34f023..b778b6b 100644 --- a/src/main/java/com/xkrs/utils/TokenUtil.java +++ b/src/main/java/com/xkrs/utils/TokenUtil.java @@ -3,44 +3,31 @@ package com.xkrs.utils; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.security.Keys; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.authority.AuthorityUtils; import javax.crypto.SecretKey; -import java.util.List; /** * token工具 */ public class TokenUtil { + /** * Token前缀 */ - static public final String TOKEN_PREFIX = "Bearer"; + private static final String TOKEN_PREFIX = "Bearer"; - static public final String SECRETKEY = "0Y9H364Q9Y908262F25LMXGIKIN5N858XM3674GWL2DD8X1DS4W6I722IRY8PS4XPNB6U30345" + "HBVCUL94STG8C3Z53T7A09JJ100I56YE9894CI11PX9J71HIZ8L5Y2O504C4E2K8276425UA8734833F45K36878FXAG799QV9LXUJ" + "OI3XA2046UPG8TB2OT84R5T6ZB127N9ZPJ7AJMC41JVHB2WK2B6H8NL45LZNAZ666KHZH3QUT65UX6F8"; + private static final String SECRET_KEY = "0Y9H364Q9Y908262F25LMXGIKIN5N858XM3674GWL2DD8X1DS4W6I722IRY8PS4XPNB6U30345" + "HBVCUL94STG8C3Z53T7A09JJ100I56YE9894CI11PX9J71HIZ8L5Y2O504C4E2K8276425UA8734833F45K36878FXAG799QV9LXUJ" + "OI3XA2046UPG8TB2OT84R5T6ZB127N9ZPJ7AJMC41JVHB2WK2B6H8NL45LZNAZ666KHZH3QUT65UX6F8"; - static SecretKey key = Keys.hmacShaKeyFor(SECRETKEY.getBytes()); + private static final SecretKey KEY = Keys.hmacShaKeyFor(SECRET_KEY.getBytes()); - public static String getTokenUserName(String token) { - String userName = ""; - if (token != null) { - try { - // 解析 Token - Claims claims = Jwts.parserBuilder().setSigningKey(key).build() - // 去掉 Bearer - .parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody(); - // 获取用户名 - userName = claims.getSubject(); - // 获取权限 - List authorities = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("auths")); - } catch (Exception e) { - // the sub field was missing or did not have a 'jsmith' value - return null; - } - } else { - return null; + public static String obtainUserNameByToken(String token) { + try { + Claims claims = Jwts.parserBuilder().setSigningKey(KEY).build().parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody();//解析Token +// List authorityList = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("auths"));//获取权限 + return claims.getSubject(); //获取用户名 + } catch (Exception e) { + e.printStackTrace(); } - return userName; + return null; } }