From ca749e43b18c7f28fd71c5ebf231b1c8dca69b31 Mon Sep 17 00:00:00 2001 From: "DESKTOP-G8BCEP0\\HP" <2037158277@qq.com> Date: Mon, 13 Sep 2021 09:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E7=BD=91=E7=AB=99=E8=B4=A6=E5=8F=B7=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=AF=B9?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xkrs/common/tool/JwtUtil.java | 94 ------------------- .../xkrs/controller/SysUserController.java | 4 + src/main/java/com/xkrs/dao/SysUserDao.java | 1 + .../service/impl/AppletsUserServiceImpl.java | 1 - 4 files changed, 5 insertions(+), 95 deletions(-) delete mode 100644 src/main/java/com/xkrs/common/tool/JwtUtil.java diff --git a/src/main/java/com/xkrs/common/tool/JwtUtil.java b/src/main/java/com/xkrs/common/tool/JwtUtil.java deleted file mode 100644 index 04f4cd2..0000000 --- a/src/main/java/com/xkrs/common/tool/JwtUtil.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.xkrs.common.tool; - -import com.auth0.jwt.JWT; -import com.auth0.jwt.JWTVerifier; -import com.auth0.jwt.algorithms.Algorithm; -import com.auth0.jwt.exceptions.JWTDecodeException; -import com.auth0.jwt.interfaces.DecodedJWT; - -import javax.servlet.http.HttpServletRequest; -import java.util.Date; - -/** - * @Author Scott - * @Date 2018-07-12 14:23 - * @Desc JWT工具类 - **/ -public class JwtUtil { - - /** - * Token过期时间30分钟(用户登录过期时间是此时间的两倍,以token在reids缓存时间为准) - */ - public static final long EXPIRE_TIME = 30 * 60 * 1000; - - /** - * 校验token是否正确 - * - * @param token 密钥 - * @param secret 用户的密码 - * @return 是否正确 - */ - public static boolean verify(String token, String username, String secret) { - try { - // 根据密码生成JWT效验器 - Algorithm algorithm = Algorithm.HMAC256(secret); - JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build(); - // 效验TOKEN - DecodedJWT jwt = verifier.verify(token); - return true; - } catch (Exception exception) { - return false; - } - } - - /** - * 获得token中的信息无需secret解密也能获得 - * - * @return token中包含的用户名 - */ - public static String getUsername(String token) { - try { - DecodedJWT jwt = JWT.decode(token); - return jwt.getClaim("username").asString(); - } catch (JWTDecodeException e) { - return null; - } - } - - /** - * 生成签名,5min后过期 - * - * @param username 用户名 - * @param secret 用户的密码 - * @return 加密的token - */ - public static String sign(String username, String secret) { - Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); - Algorithm algorithm = Algorithm.HMAC256(secret); - // 附带username信息 - return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm); - - } - - /** - * 根据request中的token获取用户账号 - * - * @param request - * @return - * @throws - */ - public static String getUserNameByToken(HttpServletRequest request){ - String accessToken = request.getHeader("Authorization"); - if(accessToken ==null){ - return null; - } - String username = getUsername(accessToken); - return username; - } - - public static void main(String[] args) { - String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUzMzY1MTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.xjhud_tWCNYBOg_aRlMgOdlZoWFFKB_givNElHNw3X0"; - System.out.println(JwtUtil.getUsername(token)); - - } -} diff --git a/src/main/java/com/xkrs/controller/SysUserController.java b/src/main/java/com/xkrs/controller/SysUserController.java index f563bee..a735a39 100644 --- a/src/main/java/com/xkrs/controller/SysUserController.java +++ b/src/main/java/com/xkrs/controller/SysUserController.java @@ -263,6 +263,10 @@ public class SysUserController { Locale locale = LocaleContextHolder.getLocale(); String openId = (String) map.get("openId"); String phone = (String) map.get("phone"); + SysUserEntity byUserName = sysUserDao.findByUserName(phone); + if(byUserName == null){ + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"输入错误或未注册,请检查注册!",locale); + } sysUserDao.updateOpenIdByPhone(phone,openId); return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"绑定成功!",locale); } diff --git a/src/main/java/com/xkrs/dao/SysUserDao.java b/src/main/java/com/xkrs/dao/SysUserDao.java index c13a953..43e83d3 100644 --- a/src/main/java/com/xkrs/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/dao/SysUserDao.java @@ -8,6 +8,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; +import java.util.Map; /** * SysUserDao diff --git a/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java b/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java index 332889e..dcc0c8d 100644 --- a/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java @@ -3,7 +3,6 @@ package com.xkrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.xkrs.common.encapsulation.PromptMessageEnum; -import com.xkrs.common.tool.JwtUtil; import com.xkrs.dao.AppletsUserDao; import com.xkrs.model.entity.AppletsUser; import com.xkrs.service.AppletsUserService;