This commit is contained in:
liuchengqian 2023-03-02 11:34:37 +08:00
parent ef68afe4ed
commit a010ec4a0b
3 changed files with 23 additions and 36 deletions

View File

@ -121,7 +121,7 @@ public class FirePointController {
@GetMapping("/selectUserName") @GetMapping("/selectUserName")
public String selectUserName(@RequestHeader(value = "Authorization") String token) { public String selectUserName(@RequestHeader(value = "Authorization") String token) {
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) { if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
@ -156,7 +156,7 @@ public class FirePointController {
// 植被类型 // 植被类型
String landType = (String) map.get("landType"); String landType = (String) map.get("landType");
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
// 用于查询的区划编码可能是省市区县街道任意级别 // 用于查询的区划编码可能是省市区县街道任意级别
String queryCode = TextUtils.isEmpty(cityCode) ? sysUserEntity.getCountyCode() : cityCode; String queryCode = TextUtils.isEmpty(cityCode) ? sysUserEntity.getCountyCode() : cityCode;
@ -185,7 +185,7 @@ public class FirePointController {
@GetMapping("/selectFirePointByMonth") @GetMapping("/selectFirePointByMonth")
public String selectFirePointByMonth(@RequestHeader(value = "Authorization") String token) { public String selectFirePointByMonth(@RequestHeader(value = "Authorization") String token) {
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
List<FirePointEntity> firePointEntities = firePointService.selectFirePointByMonth(sysUserEntity.getCountyCode()); List<FirePointEntity> firePointEntities = firePointService.selectFirePointByMonth(sysUserEntity.getCountyCode());
if (firePointEntities == null || firePointEntities.size() == 0) { if (firePointEntities == null || firePointEntities.size() == 0) {

View File

@ -2,16 +2,16 @@ package com.xkrs.straw.controller;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.xkrs.common.encapsulation.PromptMessageEnum; 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.SysUserQoInsert;
import com.xkrs.model.validation.SysUserQoUpdate; import com.xkrs.model.validation.SysUserQoUpdate;
import com.xkrs.service.RedisService; import com.xkrs.service.RedisService;
import com.xkrs.straw.dao.SysUserDao; import com.xkrs.straw.dao.SysUserDao;
import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.model.entity.SysUserEntity;
import com.xkrs.straw.model.helper.SMSHelper; 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.service.SysUserService;
import com.xkrs.straw.utils.VipTimeRangeUtils; import com.xkrs.straw.utils.VipTimeRangeUtils;
import com.xkrs.utils.AliYunSmsUtils; import com.xkrs.utils.AliYunSmsUtils;
@ -260,7 +260,7 @@ public class SysUserController {
@GetMapping("/selectAllUser") @GetMapping("/selectAllUser")
public String selectAllUser(@RequestHeader(value = "Authorization") String token) { public String selectAllUser(@RequestHeader(value = "Authorization") String token) {
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) { if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
@ -280,7 +280,7 @@ public class SysUserController {
Long userId = (Long) map.get("userId"); Long userId = (Long) map.get("userId");
String keepType = (String) map.get("keepType"); String keepType = (String) map.get("keepType");
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) { if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
@ -304,7 +304,7 @@ public class SysUserController {
String newPassword = (String) map.get("newPassword"); String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword"); String confirmPassword = (String) map.get("confirmPassword");
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) { if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
@ -322,7 +322,7 @@ public class SysUserController {
String newPassword = (String) map.get("newPassword"); String newPassword = (String) map.get("newPassword");
String confirmPassword = (String) map.get("confirmPassword"); String confirmPassword = (String) map.get("confirmPassword");
// 验证token // 验证token
String tokenUserName = TokenUtil.getTokenUserName(token); String tokenUserName = TokenUtil.obtainUserNameByToken(token);
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
if (sysUserEntity == null) { if (sysUserEntity == null) {
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);

View File

@ -3,44 +3,31 @@ package com.xkrs.utils;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys; import io.jsonwebtoken.security.Keys;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.util.List;
/** /**
* token工具 * token工具
*/ */
public class TokenUtil { public class TokenUtil {
/** /**
* Token前缀 * 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) { public static String obtainUserNameByToken(String token) {
String userName = "";
if (token != null) {
try { try {
// 解析 Token Claims claims = Jwts.parserBuilder().setSigningKey(KEY).build().parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody();//解析Token
Claims claims = Jwts.parserBuilder().setSigningKey(key).build() // List<GrantedAuthority> authorityList = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("auths"));//获取权限
// 去掉 Bearer return claims.getSubject(); //获取用户名
.parseClaimsJws(token.replace(TOKEN_PREFIX, "")).getBody();
// 获取用户名
userName = claims.getSubject();
// 获取权限
List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("auths"));
} catch (Exception e) { } catch (Exception e) {
// the sub field was missing or did not have a 'jsmith' value e.printStackTrace();
}
return null; return null;
} }
} else {
return null;
}
return userName;
}
} }