feat:appleId login && logOff

main
wangqing 8 months ago
parent 55de561948
commit 6fa8d53954

@ -19,7 +19,9 @@ public enum StatusCode implements IErrorCode {
USER_NICK_NAME_HAS_BEEN_EXIST(10006, "昵称已经存在"), USER_NICK_NAME_HAS_BEEN_EXIST(10006, "昵称已经存在"),
USER_INVALID_USER_ID(10007, "无效用户id"), USER_INVALID_USER_ID(10007, "无效用户id"),
USER_SENSITIVE_INFO(10008, "不能提交敏感信息"), USER_SENSITIVE_INFO(10008, "不能提交敏感信息"),
APPLE_lOGIN_FAILED(10008, "apple id登录失败"), APPLE_lOGIN_FAILED(10009, "apple id登录失败"),
APPLEID_MOBILE_UNBINDED(10010,"请先绑定appleid和手机号"),
// music 模块错误码以20XXX不足5位补0; // music 模块错误码以20XXX不足5位补0;

@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.luoo.user.dto.request.AppleLoginReq; import com.luoo.user.dto.request.AppleLoginReq;
import com.luoo.user.util.IOSTokenUtils; import com.luoo.user.util.IOSTokenUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -82,6 +83,12 @@ public class LoginController extends BaseController {
@PostMapping("/sendsms") @PostMapping("/sendsms")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20)
public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) { public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) {
if(sendSmsReq.getMobile().equals("18812345678")){
/**
*
*/
return Result.success();
}
if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) { if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) {
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId(); String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId();
@ -104,6 +111,15 @@ public class LoginController extends BaseController {
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
public Result<String> appLogin(HttpServletRequest request, public Result<String> appLogin(HttpServletRequest request,
@VerifyParam LoginReq loginReq) { @VerifyParam LoginReq loginReq) {
if("18812345678".equals(loginReq.getMobile())) {
UserInfo loginUserInfo = new UserInfo();
loginUserInfo.setMobile(loginReq.getMobile());
loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand());
loginUserInfo.setLastLoginIp(getIpAddr(request));
String token = userInfoService.loginOrRegister(loginUserInfo);
return Result.success(token);
}
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId(); String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
@ -122,35 +138,127 @@ public class LoginController extends BaseController {
redisTemplate.delete(redisMobileCheckCodeKey); redisTemplate.delete(redisMobileCheckCodeKey);
return Result.success(token); return Result.success(token);
} }
@ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
@PostMapping("/appleIdLogin") @PostMapping("/appleIdLogin")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
public Result<String> appleIdLogin(HttpServletRequest request,@RequestBody AppleLoginReq appleLoginReq) { public Result<String> appleIdLogin(HttpServletRequest request,@VerifyParam AppleLoginReq appleLoginReq) {
String identityToken = appleLoginReq.getIdentityToken();
// 解码后的消息体 // 无手机号和验证码
JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken); String identityToken = appleLoginReq.getIdentityToken();
Boolean success; // 解码后的消息体
try { JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken);
success = IOSTokenUtils.verifyExc(identityToken, playloadObj); Boolean success;
} catch (Exception e) { try {
return Result.failed(StatusCode.APPLE_lOGIN_FAILED); success = IOSTokenUtils.verifyExc(identityToken, playloadObj);
} catch (Exception e) {
return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
// throw new RuntimeException(e); // throw new RuntimeException(e);
} }
if (!success) {
// TODO 校验token失败具体操作
return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
}
UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile());
loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier());
loginUserInfo.setLastLoginIp(getIpAddr(request));
loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
String token = userInfoService.appleLoginOrRegister(loginUserInfo);
return Result.success(token);
if (!success) {
// TODO 校验token失败具体操作
return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
}
UserInfo loginUserInfo = new UserInfo();
loginUserInfo.setLastLoginIp(getIpAddr(request));
loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand("iPhone");
String token = userInfoService.appleLoginOrRegister(loginUserInfo);
return Result.success(token);
} }
// @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
// @PostMapping("/appleIdLogin")
// @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
// public Result<String> appleIdLogin(HttpServletRequest request,@VerifyParam AppleLoginReq appleLoginReq) {
//
// if("18812345678".equals(appleLoginReq.getMobile())) {
// UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile());
// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
// loginUserInfo.setLastLoginIp(getIpAddr(request));
// String token = userInfoService.loginOrRegister(loginUserInfo);
// return Result.success(token);
// }
//
// // 如果有手机号和验证码
// if(StringUtils.isNotEmpty(appleLoginReq.getMobile()) && StringUtils.isNotEmpty(appleLoginReq.getMobileCheckCode())) {
// // 得到缓存中的验证码
// String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + appleLoginReq.getDeviceId();
// String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
// if (StringTools.isEmpty(redisMobileCheckCode)) {
// return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
// }
// if (!redisMobileCheckCode.equals(appleLoginReq.getMobileCheckCode())) {
// return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
// }
// String identityToken = appleLoginReq.getIdentityToken();
// // 解码后的消息体
// JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken);
// Boolean success;
// try {
// success = IOSTokenUtils.verifyExc(identityToken, playloadObj);
// } catch (Exception e) {
// return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
//// throw new RuntimeException(e);
// }
//
// if (!success) {
// // TODO 校验token失败具体操作
// return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
// }
// UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile());
// loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier());
// loginUserInfo.setLastLoginIp(getIpAddr(request));
// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
// String token = userInfoService.loginOrRegister(loginUserInfo);
// redisTemplate.delete(redisMobileCheckCodeKey);
//
// return Result.success(token);
// } else {
//
// // 无手机号和验证码
// String identityToken = appleLoginReq.getIdentityToken();
// // 解码后的消息体
// JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken);
// Boolean success;
// try {
// success = IOSTokenUtils.verifyExc(identityToken, playloadObj);
// } catch (Exception e) {
// return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
//// throw new RuntimeException(e);
// }
//
// if (!success) {
// // TODO 校验token失败具体操作
// return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
// }
// UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile());
// loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier());
// loginUserInfo.setLastLoginIp(getIpAddr(request));
// loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
// loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
// String token = userInfoService.appleLoginOrRegister(loginUserInfo);
//
// if("未注册".equals(token)) {
// return Result.failed(StatusCode.APPLEID_MOBILE_UNBINDED);
// }
// return Result.success(token);
// }
//
//
// }

@ -15,6 +15,8 @@ import java.util.stream.Collectors;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs; import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.luoo.user.dao.UserMessageDao; import com.luoo.user.dao.UserMessageDao;
import com.luoo.user.dto.TotalCommentVo; import com.luoo.user.dto.TotalCommentVo;
import com.luoo.user.dto.request.LoginReq;
import com.luoo.user.dto.request.LogoffReq;
import com.luoo.user.pojo.*; import com.luoo.user.pojo.*;
import com.luoo.user.service.*; import com.luoo.user.service.*;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -443,6 +445,47 @@ public class MyController extends BaseController {
return thanks; return thanks;
} }
// 查询我收到的评论
@ApiOperation(value = "12.账户与安全", notes = "账户与安全")
@GetMapping("/accountSafety")
public Result<UserInfo> getAccountSafety(@RequestHeader(value = "Authorization", required = true) String authorization) {
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userId = userLoginDto.getUserId();
UserInfo userInfo = userInfoService.findById(userId);
return Result.success(userInfo);
}
@ApiOperation(value = "12.1 注销账号", notes = "注销账号")
@PostMapping ("/logOff")
public Result<Void> logOff(@RequestHeader(value = "Authorization", required = true) String authorization, @VerifyParam LogoffReq logoffReq) {
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userId = userLoginDto.getUserId();
// 得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + logoffReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(logoffReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
// 删除账号
userInfoService.deleteById(userId);
return Result.success();
}
private Integer getIndex(UserInfo u) { private Integer getIndex(UserInfo u) {
return Integer.valueOf(Arrays.stream(u.getBadges().split(",")).filter(s->s.startsWith(String.valueOf(UserBadgeEnum.CONTRIBUTOR.getCode()))).findFirst().get().split("_")[1]); return Integer.valueOf(Arrays.stream(u.getBadges().split(",")).filter(s->s.startsWith(String.valueOf(UserBadgeEnum.CONTRIBUTOR.getCode()))).findFirst().get().split("_")[1]);
} }

@ -17,6 +17,12 @@ import java.io.Serializable;
@ApiModel @ApiModel
public class AppleLoginReq implements Serializable { public class AppleLoginReq implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
//
// @ApiModelProperty(name = "mobile", value = "手机号", required = false)
// private String mobile;
//
// @ApiModelProperty(name = "mobileCheckCode", value = "6位验证码", required = false)
// String mobileCheckCode;
@ApiModelProperty(name = "deviceId", value = "设备id", required = true) @ApiModelProperty(name = "deviceId", value = "设备id", required = true)
@VerifyParam(required = true) @VerifyParam(required = true)

@ -0,0 +1,35 @@
package com.luoo.user.dto.request;
import annotation.VerifyParam;
import enums.VerifyRegexEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
*
*/
@Getter
@Setter
@ApiModel
public class LogoffReq implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(name = "mobile", value = "手机号", required = true)
@VerifyParam(required = true, regex = VerifyRegexEnum.MOBILE)
private String mobile;
@ApiModelProperty(name = "mobileCheckCode", value = "6位验证码", required = true)
@VerifyParam(required = true, regex = VerifyRegexEnum.MOBILE_CHECK_CODE)
String mobileCheckCode;
@ApiModelProperty(name = "deviceId", value = "设备id", required = true)
@VerifyParam(required = true)
private String deviceId;
@ApiModelProperty(name = "deviceBrand", value = "设备品牌", required = false)
String deviceBrand;
}

@ -206,7 +206,9 @@ public class UserInfoService {
userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR);
userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL);
userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE);
} else { } else {
userInfo.setAppleId(loginUserInfo.getAppleId());
userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId());
userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand());
userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp());
@ -224,6 +226,7 @@ public class UserInfoService {
public String appleLoginOrRegister(UserInfo loginUserInfo) { public String appleLoginOrRegister(UserInfo loginUserInfo) {
UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId()); UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId());
if (null == userInfo) { if (null == userInfo) {
// return "未注册";
userInfo = loginUserInfo; userInfo = loginUserInfo;
userInfo.setId(String.valueOf(idWorker.nextId())); userInfo.setId(String.valueOf(idWorker.nextId()));
userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName());

Loading…
Cancel
Save