13 changed files with 385 additions and 41 deletions
@ -0,0 +1,60 @@ |
|||||
|
package com.bnyer.img.api.model; |
||||
|
|
||||
|
import com.bnyer.common.core.vo.WxUserLoginVo; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
/** |
||||
|
* @author chengkun |
||||
|
* @date 2022/5/30 16:25 |
||||
|
*/ |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序用户信息 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class LoginWechatUser implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序用户唯一标识 |
||||
|
*/ |
||||
|
private String token; |
||||
|
|
||||
|
/** |
||||
|
* 主键id |
||||
|
*/ |
||||
|
private Long wxUserId; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序用户开放Id |
||||
|
*/ |
||||
|
private String wxUserOpenId; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序用户昵称 |
||||
|
*/ |
||||
|
private String wxUserName; |
||||
|
|
||||
|
/** |
||||
|
* 登录时间 |
||||
|
*/ |
||||
|
private Long loginTime; |
||||
|
|
||||
|
/** |
||||
|
* 过期时间 |
||||
|
*/ |
||||
|
private Long expireTime; |
||||
|
|
||||
|
/** |
||||
|
* 登录IP地址 |
||||
|
*/ |
||||
|
private String ipaddr; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序用户信息 |
||||
|
*/ |
||||
|
private WxUserLoginVo wxUser; |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
package com.bnyer.auth.controller; |
||||
|
|
||||
|
import com.bnyer.auth.service.WxUserLoginService; |
||||
|
import com.bnyer.common.core.domain.R; |
||||
|
import com.bnyer.common.core.dto.WxLoginDto; |
||||
|
import com.bnyer.common.security.service.WxUserTokenService; |
||||
|
import com.bnyer.common.security.utils.SecurityUtils; |
||||
|
import com.bnyer.img.api.model.LoginWechatUser; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序token 控制 |
||||
|
* |
||||
|
* @author penny |
||||
|
*/ |
||||
|
@RestController |
||||
|
public class WxUserController |
||||
|
{ |
||||
|
@Autowired |
||||
|
private WxUserLoginService wxUserLoginService; |
||||
|
|
||||
|
@Autowired |
||||
|
private WxUserTokenService WxUserTokenService; |
||||
|
|
||||
|
@ApiOperation(value="微信小程序用户登录") |
||||
|
@PostMapping(value = "/wxUserLogin") |
||||
|
public R<?> loginWx(@Validated @RequestBody @ApiParam("登录对象") WxLoginDto dto){ |
||||
|
LoginWechatUser loginWxUser = wxUserLoginService.login(dto); |
||||
|
return R.ok(WxUserTokenService.createToken(loginWxUser)); |
||||
|
} |
||||
|
|
||||
|
@PostMapping("/wxUserLogout") |
||||
|
@ApiOperation("微信小程序用户注销") |
||||
|
public R<?> logout(HttpServletRequest request) |
||||
|
{ |
||||
|
String token = SecurityUtils.getToken(request); |
||||
|
//删除微信用户缓存
|
||||
|
WxUserTokenService.delLoginFhUser(token); |
||||
|
return R.ok(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.bnyer.auth.service; |
||||
|
|
||||
|
import com.bnyer.common.core.dto.FhLoginDto; |
||||
|
import com.bnyer.common.core.dto.WxLoginDto; |
||||
|
import com.bnyer.img.api.RemoteImgService; |
||||
|
import com.bnyer.img.api.model.LoginFhUser; |
||||
|
import com.bnyer.img.api.model.LoginWechatUser; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 微信小程序端用户登录校验方法 |
||||
|
* |
||||
|
* @author penny |
||||
|
*/ |
||||
|
@Component |
||||
|
public class WxUserLoginService { |
||||
|
|
||||
|
@Autowired |
||||
|
private RemoteImgService remoteImgService; |
||||
|
|
||||
|
public LoginWechatUser login(WxLoginDto dto) { |
||||
|
return remoteImgService.getWxLoginUserByLoginParam(dto).getData(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
package com.bnyer.common.core.vo; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.Date; |
||||
|
|
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
@ApiModel("微信小程序用户登录响应类") |
||||
|
public class WxUserLoginVo implements Serializable { |
||||
|
|
||||
|
@ApiModelProperty(value="id") |
||||
|
private Long id; |
||||
|
|
||||
|
@ApiModelProperty(value="用户昵称") |
||||
|
private String username; |
||||
|
|
||||
|
@ApiModelProperty(value="微信id") |
||||
|
private String wxCode; |
||||
|
|
||||
|
@ApiModelProperty(value="是否为vip(0->否;1->是)") |
||||
|
private String isVip; |
||||
|
|
||||
|
@ApiModelProperty(value="头像img地址") |
||||
|
private String img; |
||||
|
|
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
@ApiModelProperty(value="创建时间") |
||||
|
private Date createTime; |
||||
|
|
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
@ApiModelProperty(value="更新时间") |
||||
|
private Date updateTime; |
||||
|
|
||||
|
@ApiModelProperty(value="排序") |
||||
|
private Integer sort; |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
} |
||||
@ -0,0 +1,101 @@ |
|||||
|
package com.bnyer.common.security.service; |
||||
|
|
||||
|
import com.bnyer.common.core.constant.CacheConstants; |
||||
|
import com.bnyer.common.core.constant.RedisKeyConstant; |
||||
|
import com.bnyer.common.core.constant.SecurityConstants; |
||||
|
import com.bnyer.common.core.utils.JwtUtils; |
||||
|
import com.bnyer.common.core.utils.ServletUtils; |
||||
|
import com.bnyer.common.core.utils.StringUtils; |
||||
|
import com.bnyer.common.core.utils.ip.IpUtils; |
||||
|
import com.bnyer.common.core.utils.uuid.IdUtils; |
||||
|
import com.bnyer.common.redis.service.RedisService; |
||||
|
import com.bnyer.img.api.model.LoginFhUser; |
||||
|
import com.bnyer.img.api.model.LoginWechatUser; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
/** |
||||
|
* 微信端小程序token验证处理 |
||||
|
* |
||||
|
* @author penny |
||||
|
*/ |
||||
|
@Component |
||||
|
public class WxUserTokenService |
||||
|
{ |
||||
|
@Autowired |
||||
|
private RedisService redisService; |
||||
|
|
||||
|
protected static final long MILLIS_SECOND = 1000; |
||||
|
|
||||
|
protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND; |
||||
|
|
||||
|
private final static long expireTime = CacheConstants.EXPIRATION; |
||||
|
|
||||
|
private final static String ACCESS_TOKEN = RedisKeyConstant.WECHAT_USER_LOGIN_KEY; |
||||
|
|
||||
|
/** |
||||
|
* 创建令牌 |
||||
|
*/ |
||||
|
public Map<String, Object> createToken(LoginWechatUser loginWechatUser) |
||||
|
{ |
||||
|
String token = IdUtils.fastUUID(); |
||||
|
Long wxUserId = loginWechatUser.getWxUser().getId(); |
||||
|
String wxUserName = loginWechatUser.getWxUser().getUsername(); |
||||
|
String openId = loginWechatUser.getWxUser().getWxCode(); |
||||
|
loginWechatUser.setToken(token); |
||||
|
loginWechatUser.setWxUserId(wxUserId); |
||||
|
loginWechatUser.setWxUserName(wxUserName); |
||||
|
loginWechatUser.setWxUserOpenId(openId); |
||||
|
loginWechatUser.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest())); |
||||
|
refreshToken(loginWechatUser); |
||||
|
|
||||
|
// Jwt存储信息
|
||||
|
Map<String, Object> claimsMap = new HashMap<String, Object>(); |
||||
|
claimsMap.put(SecurityConstants.WECHAT_USER_KEY, token); |
||||
|
claimsMap.put(SecurityConstants.DETAILS_WECHAT_USER_ID, wxUserId); |
||||
|
claimsMap.put(SecurityConstants.DETAILS_WECHAT_USERNAME, wxUserName); |
||||
|
claimsMap.put(SecurityConstants.DETAILS_WECHAT_OPENID,openId); |
||||
|
|
||||
|
// 接口返回信息
|
||||
|
Map<String, Object> rspMap = new HashMap<String, Object>(); |
||||
|
rspMap.put("access_token", JwtUtils.createToken(claimsMap)); |
||||
|
rspMap.put("userInfo",loginWechatUser.getWxUser()); |
||||
|
rspMap.put("expires_in", expireTime); |
||||
|
return rspMap; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 删除微信小程序用户缓存信息 |
||||
|
*/ |
||||
|
public void delLoginFhUser(String token) |
||||
|
{ |
||||
|
if (StringUtils.isNotEmpty(token)) |
||||
|
{ |
||||
|
String wxUserKey = JwtUtils.getWechatUserKey(token); |
||||
|
redisService.deleteObject(getTokenKey(wxUserKey)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 刷新令牌有效期 |
||||
|
* |
||||
|
* @param loginWechatUser 微信小程序用户登录信息 |
||||
|
*/ |
||||
|
public void refreshToken(LoginWechatUser loginWechatUser) |
||||
|
{ |
||||
|
loginWechatUser.setLoginTime(System.currentTimeMillis()); |
||||
|
loginWechatUser.setExpireTime(loginWechatUser.getLoginTime() + expireTime * MILLIS_MINUTE); |
||||
|
// 根据uuid将loginWechatUser缓存
|
||||
|
String fhUserKey = getTokenKey(loginWechatUser.getToken()); |
||||
|
redisService.setCacheObject(fhUserKey, loginWechatUser, expireTime, TimeUnit.MINUTES); |
||||
|
} |
||||
|
|
||||
|
private String getTokenKey(String token) |
||||
|
{ |
||||
|
return ACCESS_TOKEN + token; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue