修改了绑定网站账号的功能模块,添加了对账号是否存在的判断
This commit is contained in:
parent
307626f8fe
commit
ca749e43b1
@ -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));
|
||||
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user