#使用若依框架实现的微信小程序后端,小程序可在多设备登录,且登录后永不过期,问题是在一个设备修改用户信息后比如用户头像,其它设备信息不能自动更新,请问该如何解决
/**
* 登录验证
*
* @param code 微信小程序登录code
* @return 结果
*/
public String openidLogin(String code)
{
//获取token和sessiceKey
String res = HttpUtils.sendGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code");
JSONObject resJSON = JSONObject.parseObject(res);
AjaxResult ajax = AjaxResult.success();
ajax.put("openId", resJSON.get("openid"));
ajax.put("sessionKey", resJSON.get("session_key"));
//查询oppid是否存在,不存在则插入到数据库中
if (resJSON.get("openid")!=null){
SysUser openid = userMapper.selectByOpenid((String) resJSON.get("openid"));
if (openid==null){
SysUser sysUser = new SysUser();
sysUser.setOpenid((String) resJSON.get("openid"));
sysUser.setUnionid((String) resJSON.get("unionid"));
sysUser.setCreateTime(new Date());
sysUser.setUserName("露营人生"+((String) resJSON.get("openid")).substring(((String) resJSON.get("openid")).length()-6));
sysUser.setNickName("露营人生"+((String) resJSON.get("openid")).substring(((String) resJSON.get("openid")).length()-6));
sysUser.setDelFlag("0");
userMapper.insertUser(sysUser);
}
//根据openid更新用户IP地址
//获取请求头中的token信息
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
if(Objects.isNull(ra)){
return null;
}
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
//更新用户IP地址
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
userMapper.updateUserIpAddressTOOpenid((String) resJSON.get("openid"),ip);
}
//用户验证
Authentication authentication = null;
LoginUser loginUser=null;
String username = null;
try
{
authentication = authenticationManager.authenticate(new WxAuthenticationToken((String)resJSON.get("openid")));
loginUser = (LoginUser)authentication.getPrincipal();
username = loginUser.getUsername();
}
catch (Exception e)
{
String errMsg = e.getMessage();
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,Constants.LOGIN_FAIL,errMsg));
throw new RuntimeException(errMsg);
}
// 生成token
String token = tokenService.createToken(loginUser);
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username,Constants.LOGIN_SUCCESS,MessageUtils.message("user.login.success")));
recordLoginInfo(loginUser.getUser().getUserId());
return token;
}