新增多平台token
This commit is contained in:
15
src/main/java/com/peanut/common/utils/HttpContextUtil.java
Normal file
15
src/main/java/com/peanut/common/utils/HttpContextUtil.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.peanut.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class HttpContextUtil {
|
||||||
|
|
||||||
|
private HttpContextUtil(){}
|
||||||
|
public static HttpServletRequest getHttpServletRequest() {
|
||||||
|
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,10 @@ public class SysUserTokenEntity implements Serializable {
|
|||||||
private Long userId;
|
private Long userId;
|
||||||
//token
|
//token
|
||||||
private String token;
|
private String token;
|
||||||
|
//token
|
||||||
|
private String tokenMedical;
|
||||||
|
//token
|
||||||
|
private String tokenSociology;
|
||||||
//过期时间
|
//过期时间
|
||||||
private Date expireTime;
|
private Date expireTime;
|
||||||
//更新时间
|
//更新时间
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
package com.peanut.modules.sys.service.impl;
|
package com.peanut.modules.sys.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.peanut.common.utils.HttpContextUtil;
|
||||||
import com.peanut.common.utils.R;
|
import com.peanut.common.utils.R;
|
||||||
import com.peanut.modules.sys.dao.SysUserTokenDao;
|
import com.peanut.modules.sys.dao.SysUserTokenDao;
|
||||||
import com.peanut.modules.sys.entity.SysUserTokenEntity;
|
import com.peanut.modules.sys.entity.SysUserTokenEntity;
|
||||||
@@ -17,6 +18,7 @@ import com.peanut.modules.sys.service.SysUserTokenService;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -30,36 +32,30 @@ public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUse
|
|||||||
public R createToken(long userId) {
|
public R createToken(long userId) {
|
||||||
//生成一个token
|
//生成一个token
|
||||||
String token = TokenGenerator.generateValue();
|
String token = TokenGenerator.generateValue();
|
||||||
|
|
||||||
//当前时间
|
//当前时间
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
//过期时间
|
//过期时间
|
||||||
Date expireTime = new Date(now.getTime() + EXPIRE * 1000);
|
Date expireTime = new Date(now.getTime() + EXPIRE * 1000);
|
||||||
|
|
||||||
//判断是否生成过token
|
//判断是否生成过token
|
||||||
SysUserTokenEntity tokenEntity = this.getById(userId);
|
SysUserTokenEntity tokenEntity = this.getById(userId);
|
||||||
if (tokenEntity == null) {
|
if (tokenEntity == null) {
|
||||||
tokenEntity = new SysUserTokenEntity();
|
tokenEntity = new SysUserTokenEntity();
|
||||||
|
}
|
||||||
|
//根部不同登录平台生成不同token
|
||||||
|
HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
|
||||||
|
String appType = request.getHeader("appType")==null?"":request.getHeader("appType");
|
||||||
|
if ("".equals(appType)){
|
||||||
|
tokenEntity.setToken(token);
|
||||||
|
}else if ("medical".equals(appType)){
|
||||||
|
tokenEntity.setTokenMedical(token);
|
||||||
|
}else if ("sociology".equals(appType)){
|
||||||
|
tokenEntity.setTokenSociology(token);
|
||||||
|
}
|
||||||
tokenEntity.setUserId(userId);
|
tokenEntity.setUserId(userId);
|
||||||
tokenEntity.setToken(token);
|
|
||||||
tokenEntity.setUpdateTime(now);
|
tokenEntity.setUpdateTime(now);
|
||||||
tokenEntity.setExpireTime(expireTime);
|
tokenEntity.setExpireTime(expireTime);
|
||||||
|
this.saveOrUpdate(tokenEntity);
|
||||||
//保存token
|
|
||||||
this.save(tokenEntity);
|
|
||||||
}else{
|
|
||||||
if (tokenEntity.getExpireTime().getTime() > now.getTime()){
|
|
||||||
token = tokenEntity.getToken();
|
|
||||||
}
|
|
||||||
tokenEntity.setToken(token);
|
|
||||||
tokenEntity.setUpdateTime(now);
|
|
||||||
tokenEntity.setExpireTime(expireTime);
|
|
||||||
//更新token
|
|
||||||
this.updateById(tokenEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
R r = R.ok().put("token", token).put("expire", EXPIRE);
|
R r = R.ok().put("token", token).put("expire", EXPIRE);
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,6 @@
|
|||||||
<mapper namespace="com.peanut.modules.sys.dao.SysUserTokenDao">
|
<mapper namespace="com.peanut.modules.sys.dao.SysUserTokenDao">
|
||||||
|
|
||||||
<select id="queryByToken" resultType="com.peanut.modules.sys.entity.SysUserTokenEntity">
|
<select id="queryByToken" resultType="com.peanut.modules.sys.entity.SysUserTokenEntity">
|
||||||
select * from sys_user_token where token = #{value}
|
select * from sys_user_token where (token = #{value} or token_medical = #{value} or token_sociology = #{value})
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user