Controller类代码
@ResponseBody
@RequestMapping(value = "/makeAccessToken", params = {"deviceToken", "state", "model", "os", "appVersion"}, method = {RequestMethod.POST})
@ApiOperation(value = "用户兑换AccessToken", notes = "", httpMethod = "POST",
produces = MediaType.APPLICATION_JSON_VALUE)
public Object makeAccessToken(@ApiParam(value = "设备Token", required = true) String deviceToken,
@ApiParam(value = "状态") String state,
@ApiParam(value = "设备类型名称(如xiaomi)") String model,
@ApiParam(value = "操作系统及版本号 ios8.0") String os,
@ApiParam(value = "当前APP版本号 ") String appVersion) {
logger.info("用户兑换新的AccessToken(" + deviceToken + ":" + model + "),os:" + os + ",appVersion:" + appVersion);
UserAccesstoken accessToken = userAuthService.clientCredentials(deviceToken, state, model, os, appVersion);
//getSession().removeAttribute(BaseConstants.SESSION_ACCESS_TOKEN)
//假设当前session域对象中已经有属性名该SESSION_ACCESS_TOKEN常量为的属性
//详情见 https://www.cnblogs.com/qjm575632320/p/7009381.html
getSession().removeAttribute(BaseConstants.SESSION_ACCESS_TOKEN);
getSession().removeAttribute(BaseConstants.SESSION_USER);
//将数据put进Map里面获取前台传入的数据
Map<String, Object> result = Maps.newHashMap();
result.put("access_token", accessToken.getAccessToken());
result.put("expires_in", accessToken.getExpiresTime());
result.put("state", accessToken.getState());
result.put("scope", accessToken.getScope());
result.put("sessionId", getSession().getId());
return result;
}
Service类代码
/** * BUserbaseService接口 * Created by sddg on 2017/5/22. */ public interface UserAuthService { UserAccesstoken clientCredentials(String deviceToken, String state, String model, String osVersion, String appVersion); }
ServiceImpl实现类代码
@Transactional public UserAccesstoken clientCredentials(String deviceToken, String state, String model, String osVersion, String appVersion){ UserDeviceExample userDeviceExample = new UserDeviceExample(); userDeviceExample.createCriteria().andDeviceTokenEqualTo(deviceToken); //selectFirstByExample调用BaseService类封装好的方法 UserDevice device = deviceService.selectFirstByExample(userDeviceExample); _log.info("开始插入deviceToken" + deviceToken); //判断用户是否存在deviceToken if(device == null){ _log.info("第一次使用deviceToken" + deviceToken); try{ device = new UserDevice(); device.setDeviceToken(deviceToken); device.setModel(model); device.setOs(osVersion); device.setAppVersion(appVersion); device.setCreateTime(new Date()); device = deviceService.insertSelective(device); _log.info("第一次使用deviceToken,插入成功" + deviceToken); }catch(Exception e){ _log.info("deviceDuplicate: {}", device); } if(device.getAppVersion() == null || "".equals(device.getAppVersion())){ _log.info("设备:"+device.getId()+"版本号:"+device.getAppVersion()); } } else { if((!StringUtils.isEmpty(model) && !StringUtils.isEmpty(osVersion) && !StringUtils.isEmpty(appVersion)) &&(!StringUtils.defaultString(model).equals(StringUtils.defaultString(device.getModel())) || !StringUtils.defaultString(device.getOs()).contains(StringUtils.defaultString(osVersion)) || !StringUtils.defaultString(appVersion).equals(StringUtils.defaultString(device.getAppVersion())))) { _log.info("历史deviceToken" + deviceToken); device.setModel(model); device.setOs(osVersion); device.setAppVersion(appVersion); device.setUpdateTime(new Date()); deviceService.updateByPrimaryKey(device); _log.info("历史deviceToken,更新成功" + deviceToken); } } //清除原来用户用此设备登录过的accesstoken _log.info("开始删除accessToken" + deviceToken); UserAccesstokenExample userAccesstokenExample = new UserAccesstokenExample(); userAccesstokenExample.createCriteria().andDeviceIdEqualTo(device.getId()); accesstokenService.deleteByExample(userAccesstokenExample); _log.info("删除accessToken完毕" + deviceToken); UserAccesstoken accessToken = new UserAccesstoken(); accessToken.setAccessToken(generateToken()); accessToken.setDeviceId(device.getId()); accessToken.setTokenType("client_credentials"); accessToken.setScope("guest"); accessToken.setCreateTime(new Date()); Long nowTime = new Date().getTime(); Date newExpiresTime = new Date(nowTime + 3 * 30 * 24 * 60 * 60 * 1000L); accessToken.setExpiresTime(newExpiresTime); _log.info("开始插入新的accessToken" + deviceToken); accessToken.setState(state); accesstokenService.insertSelective(accessToken); _log.info("插入新的accessToken完毕" + deviceToken); return accessToken; }