release: getLoginQr后端不再生成二维码,前端主动生成

release-2024-08-08
huangyw 4 months ago
parent fe44007049
commit 3504afe58c

@ -24,9 +24,11 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -47,157 +49,166 @@ import java.util.concurrent.TimeUnit;
*/ */
@RestController @RestController
@CrossOrigin @CrossOrigin
@Slf4j
@RequestMapping("/user") @RequestMapping("/user")
@Api(tags = "LoginController") @Api(tags = "LoginController")
public class LoginController extends BaseController { public class LoginController extends BaseController {
private static final List<CountryCodeDTO> DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays
.asList(new CountryCodeDTO("中国", "+86"));
@Autowired
private UserInfoService userInfoService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private WeChatAouth2Service weChatAouth2Service;
@Autowired
private JwtUtil jwtUtil;
/**
*
*
* @param
* @return
*/
@PutMapping("/{userid}/{friendid}/{x}")
public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid,
@PathVariable int x) {
userInfoService.updatefanscountandfollowcount(x, userid, friendid);
}
/** private static final List<CountryCodeDTO> DEFAULT_SUPPORTED_COUNTRY_CODES = Arrays
* .asList(new CountryCodeDTO("中国", "+86"));
*/ @Autowired
@ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟一个手机号一天内最多发送20个请求") private UserInfoService userInfoService;
@PostMapping("/sendsms")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20) @Autowired
public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) { private RedisTemplate redisTemplate;
if (sendSmsReq.getMobile().equals("18812345678")) {
/** @Autowired
* private WeChatAouth2Service weChatAouth2Service;
*/
return Result.success(); @Autowired
} private JwtUtil jwtUtil;
if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) {
// 得到缓存中的验证码 @Value("${download.url}")
String redisImageCheckCodeKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId(); private String downloadUrl;
String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey);
if (StringTools.isEmpty(redisImageCheckCode)) { /**
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); *
} *
if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) { * @param
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); * @return
} */
redisTemplate.delete(redisImageCheckCodeKey); @PutMapping("/{userid}/{friendid}/{x}")
} public void updatefanscountandfollowcount(@PathVariable String userid,
@PathVariable String friendid,
userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile()); @PathVariable int x) {
return Result.success(); userInfoService.updatefanscountandfollowcount(x, userid, friendid);
}
/**
*
*/
@ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟一个手机号一天内最多发送20个请求")
@PostMapping("/sendsms")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 20)
public Result<Void> sendSms(@VerifyParam SendSmsReq sendSmsReq) {
if (sendSmsReq.getMobile().equals("18812345678")) {
/**
*
*/
return Result.success();
} }
if (!StringTools.isEmpty(sendSmsReq.getImageCheckCode())) {
@ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization") // 得到缓存中的验证码
@PostMapping("/appLogin") String redisImageCheckCodeKey =
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) Constants.REDIS_KEY_IMAGE_CHECK_CODE + sendSmsReq.getDeviceId();
public Result<String> appLogin(HttpServletRequest request, String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey);
@VerifyParam LoginReq loginReq) { if (StringTools.isEmpty(redisImageCheckCode)) {
if ("18812345678".equals(loginReq.getMobile())) { return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED);
UserInfo loginUserInfo = new UserInfo(); }
loginUserInfo.setMobile(loginReq.getMobile()); if (!redisImageCheckCode.equalsIgnoreCase(sendSmsReq.getImageCheckCode())) {
loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH);
loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); }
loginUserInfo.setLastLoginIp(getIpAddr(request)); redisTemplate.delete(redisImageCheckCodeKey);
String token = userInfoService.loginOrRegister(loginUserInfo);
return Result.success(token);
}
// 得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + loginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
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);
redisTemplate.delete(redisMobileCheckCodeKey);
return Result.success(token);
} }
@ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") userInfoService.sendSms(sendSmsReq.getDeviceId(), sendSmsReq.getMobile());
@PostMapping("/appleIdLogin") return Result.success();
public Result<String> appleIdLogin(HttpServletRequest request, @VerifyParam AppleLoginReq appleLoginReq) { }
@ApiOperation(value = "2.登录/注册", notes = "成功后返回authorization")
// 无手机号和验证码 @PostMapping("/appLogin")
String identityToken = appleLoginReq.getIdentityToken(); @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
// 解码后的消息体 public Result<String> appLogin(HttpServletRequest request,
JSONObject playloadObj = IOSTokenUtils.parserIdentityToken(identityToken); @VerifyParam LoginReq loginReq) {
Boolean success; if ("18812345678".equals(loginReq.getMobile())) {
try { UserInfo loginUserInfo = new UserInfo();
success = IOSTokenUtils.verifyExc(identityToken, playloadObj); loginUserInfo.setMobile(loginReq.getMobile());
} catch (Exception e) { loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId());
return Result.failed(StatusCode.APPLE_lOGIN_FAILED); 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 redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(loginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
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);
redisTemplate.delete(redisMobileCheckCodeKey);
return Result.success(token);
}
@ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
@PostMapping("/appleIdLogin")
public Result<String> appleIdLogin(HttpServletRequest request,
@VerifyParam AppleLoginReq appleLoginReq) {
// 无手机号和验证码
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); // throw new RuntimeException(e);
} }
if (!success) { if (!success) {
// TODO 校验token失败具体操作 // TODO 校验token失败具体操作
return Result.failed(StatusCode.APPLE_lOGIN_FAILED); return Result.failed(StatusCode.APPLE_lOGIN_FAILED);
} }
UserInfo loginUserInfo = new UserInfo(); UserInfo loginUserInfo = new UserInfo();
// loginUserInfo.setMobile(appleLoginReq.getMobile()); // loginUserInfo.setMobile(appleLoginReq.getMobile());
loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier()); loginUserInfo.setAppleId(appleLoginReq.getUserIdentifier());
loginUserInfo.setAppleEmail(playloadObj.get("email") + ""); loginUserInfo.setAppleEmail(playloadObj.get("email") + "");
loginUserInfo.setLastLoginIp(getIpAddr(request)); loginUserInfo.setLastLoginIp(getIpAddr(request));
loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId()); loginUserInfo.setLastUseDeviceId(appleLoginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand()); loginUserInfo.setLastUseDeviceBrand(appleLoginReq.getDeviceBrand());
String token = userInfoService.appleLoginOrRegister(loginUserInfo); String token = userInfoService.appleLoginOrRegister(loginUserInfo);
return Result.success(token);
return Result.success(token);
}
@ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization")
@PostMapping("/wxIdLogin")
public Result<String> wxIdLogin(HttpServletRequest request,
@VerifyParam WeChatLoginReq weChatLoginReq) {
// 如果有手机号,是首次微信登录时绑定流程
if (StringUtils.isNotEmpty(weChatLoginReq.getMobile())) {
//得到缓存中的验证码
String redisMobileCheckCodeKey =
Constants.REDIS_KEY_MOBILE_CHECK_CODE + weChatLoginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue()
.get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(weChatLoginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
return weChatAouth2Service.register(weChatLoginReq, getIpAddr(request));
} }
@ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization") // 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录如果有记录登录正常执行如果无记录返回状态到上面绑定手机号流程
@PostMapping("/wxIdLogin") return weChatAouth2Service.login(weChatLoginReq, getIpAddr(request));
public Result<String> wxIdLogin(HttpServletRequest request, @VerifyParam WeChatLoginReq weChatLoginReq) {
// 如果有手机号,是首次微信登录时绑定流程
if (StringUtils.isNotEmpty(weChatLoginReq.getMobile())) {
//得到缓存中的验证码
String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + weChatLoginReq.getDeviceId();
String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey);
if (StringTools.isEmpty(redisMobileCheckCode)) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED);
}
if (!redisMobileCheckCode.equals(weChatLoginReq.getMobileCheckCode())) {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
}
return weChatAouth2Service.register(weChatLoginReq, getIpAddr(request));
}
// 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录如果有记录登录正常执行如果无记录返回状态到上面绑定手机号流程
return weChatAouth2Service.login(weChatLoginReq, getIpAddr(request));
}
}
// @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)
@ -285,176 +296,173 @@ public class LoginController extends BaseController {
// } // }
/** /**
* authorization * authorization
*/ */
@ApiOperation(value = "3.token续期", notes = "有效期7天") @ApiOperation(value = "3.token续期", notes = "有效期7天")
@PostMapping("/autoLogin") @PostMapping("/autoLogin")
@GlobalInterceptor @GlobalInterceptor
public Result<String> autoLogin(HttpServletRequest request, public Result<String> autoLogin(HttpServletRequest request,
@RequestHeader(value = "Authorization", required = false) String authorization, @RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam TokenRefreshReq tokenRefreshReq) { @VerifyParam TokenRefreshReq tokenRefreshReq) {
return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(), tokenRefreshReq.getDeviceBrand(), getIpAddr(request))); return Result.success(userInfoService.autoLogin(authorization, tokenRefreshReq.getDeviceId(),
tokenRefreshReq.getDeviceBrand(), getIpAddr(request)));
}
/**
*
*/
@ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后获取图形验证码有效期10分钟")
@ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)})
@GetMapping("/imageCheckCode/{deviceId}")
@GlobalInterceptor
public void imageCheckCode(HttpServletResponse response,
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException {
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code = vCode.getCode();
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
vCode.write(response.getOutputStream());
}
/**
* 4.1Base64
*
* @return
*/
@ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后获取图形验证码Base64有效期10分钟")
@ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)})
@GetMapping("/imageCheckCodeBase64/{deviceId}")
@GlobalInterceptor
public Result<String> imageCheckCodeBase64(HttpServletResponse response,
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException {
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code = vCode.getCode();
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
vCode.write(bos);
return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray()));
}
/**
*
*/
@ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'")
@GetMapping("/supportedCountryCode")
public Result<List<CountryCodeDTO>> getSupportedCountryCode() {
return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES);
}
/**
* 退
*
* @param authorization
* @return
*/
@ApiOperation(value = "6.退出登录")
@PostMapping("/logout")
@GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> logout(
@RequestHeader(value = "Authorization", required = true) String authorization) {
return Result.success();
}
/**
* /
*
* @return
*/
@ApiOperation(value = "7.通过用户名/密码登录")
@PostMapping("/login/username")
public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) {
String userName = userNameLoginDto.getUserName();
String password = userNameLoginDto.getPassword();
UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password);
if (ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())) {
// 用户状态错误
throw new BizException(ErrorConstants.USER_STATUS_ERROR);
} }
if (userInfo != null) {
/** //生成token
* String token = jwtUtil.createJWT(userInfo.getId(), userInfo.getUserName(), "artist", "");
*/ Map<String, Object> map = new HashMap<>();
@ApiOperation(value = "4.获取图形验证码", notes = "4.三次短信验证失败后获取图形验证码有效期10分钟") map.put("token", token);
@ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)}) map.put("roles", "artist");
@GetMapping("/imageCheckCode/{deviceId}") map.put("name", userInfo.getUserName());
@GlobalInterceptor return Result.success(map);
public void imageCheckCode(HttpServletResponse response, } else {
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException { return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED);
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code = vCode.getCode();
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
vCode.write(response.getOutputStream());
}
/**
* 4.1Base64
*
* @return
*/
@ApiOperation(value = "4.1获取图形验证码Base64", notes = "4.三次短信验证失败后获取图形验证码Base64有效期10分钟")
@ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备id", required = true)})
@GetMapping("/imageCheckCodeBase64/{deviceId}")
@GlobalInterceptor
public Result<String> imageCheckCodeBase64(HttpServletResponse response,
@VerifyParam(required = true) @PathVariable("deviceId") String deviceId) throws IOException {
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code = vCode.getCode();
String redisKey = Constants.REDIS_KEY_IMAGE_CHECK_CODE + deviceId;
redisTemplate.opsForValue().set(redisKey, code, 10, TimeUnit.MINUTES);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
vCode.write(bos);
return Result.success(Base64.getEncoder().encodeToString(bos.toByteArray()));
}
/**
*
*/
@ApiOperation(value = "5.获取支持的手机号国家码", notes = "v1仅支持'+86'")
@GetMapping("/supportedCountryCode")
public Result<List<CountryCodeDTO>> getSupportedCountryCode() {
return Result.success(DEFAULT_SUPPORTED_COUNTRY_CODES);
}
/**
* 退
*
* @param authorization
* @return
*/
@ApiOperation(value = "6.退出登录")
@PostMapping("/logout")
@GlobalInterceptor(checkAppUserLogin = true)
public Result<Void> logout(@RequestHeader(value = "Authorization", required = true) String authorization) {
return Result.success();
}
/**
* /
*
* @return
*/
@ApiOperation(value = "7.通过用户名/密码登录")
@PostMapping("/login/username")
public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) {
String userName = userNameLoginDto.getUserName();
String password = userNameLoginDto.getPassword();
UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password);
if (ObjectUtils.notEqual(UserStatusEnum.ENABLE.getStatus(), userInfo.getStatus())) {
// 用户状态错误
throw new BizException(ErrorConstants.USER_STATUS_ERROR);
}
if (userInfo != null) {
//生成token
String token = jwtUtil.createJWT(userInfo.getId(), userInfo.getUserName(), "artist", "");
Map<String, Object> map = new HashMap<>();
map.put("token", token);
map.put("roles", "artist");
map.put("name", userInfo.getUserName());
return Result.success(map);
} else {
return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED);
}
}
@ApiOperation(value = "8.修改密码")
@PostMapping("/changePassword")
public Result<Void> changePassword(@Validated @RequestBody UserNameLoginDto changePasswordDto) {
userInfoService.changePassword(changePasswordDto.getUserName(), changePasswordDto.getPassword());
return Result.success();
}
@ApiOperation(value = "9.1.获取UUID", notes = "获取UUID")
@GetMapping(value = "/uuid")
public Result<String> getUuid() {
String uuid = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS);
return Result.success(uuid);
}
@ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0-未完成 1-已扫码 other-token")
@GetMapping(value = "/check/qr")
public Result<String> getCountryCode(String uuid) {
return Result.success(redisTemplate.opsForValue().get(uuid).toString());
} }
}
@ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码")
@GetMapping(value = "/getLoginQr") @ApiOperation(value = "8.修改密码")
public void createCodeImg(HttpServletRequest request, HttpServletResponse response, String uuid) { @PostMapping("/changePassword")
response.setHeader("Pragma", "No-cache"); public Result<Void> changePassword(@Validated @RequestBody UserNameLoginDto changePasswordDto) {
response.setHeader("Cache-Control", "no-cache"); userInfoService.changePassword(changePasswordDto.getUserName(),
changePasswordDto.getPassword());
response.setDateHeader("Expires", 0); return Result.success();
response.setContentType("image/jpeg"); }
try { @ApiOperation(value = "9.1.获取UUID", notes = "获取UUID")
@GetMapping(value = "/uuid")
Object o = redisTemplate.opsForValue().get(uuid); public Result<String> getUuid() {
if (o == null) { String uuid = UUID.randomUUID().toString();
throw new BizException("二维码失效!"); redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS);
} return Result.success(uuid);
if (!String.valueOf(o).equals("0")) { }
throw new BizException("登录认证已完成");
} @ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0-未完成 1-已扫码 other-token")
@GetMapping(value = "/check/qr")
response.setHeader("uuid", uuid); public Result<String> getCountryCode(String uuid) {
// 这里是开源工具类 hutool里的QrCodeUtil return Result.success(redisTemplate.opsForValue().get(uuid).toString());
// 网址http://hutool.mydoc.io/ }
QrCodeUtil.generate(uuid, 300, 300, "jpg", response.getOutputStream());
@ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码")
} catch (Exception e) { @GetMapping(value = "/getLoginQr")
e.printStackTrace(); public Result<String> createCodeImg(HttpServletRequest request, HttpServletResponse response,
} String uuid) {
try {
Object o = redisTemplate.opsForValue().get(uuid);
if (o == null) {
throw new BizException("二维码失效!");
}
if (!String.valueOf(o).equals("0")) {
throw new BizException("登录认证已完成");
}
} catch (Exception e) {
e.printStackTrace();
} }
@ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等") log.info(downloadUrl);
@PostMapping(value = "/scanOrConfirm") return Result.success(uuid);
@ApiImplicitParams({@ApiImplicitParam(name = "token", value = "扫码获得的code", required = true), }
@ApiImplicitParam(name = "userId", value = "用户id", required = true),
@ApiImplicitParam(name = "type", value = "type 1-扫码 2-确认", required = true)}) @ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等")
public Result<Void> scanOrConfirm(@RequestParam("token") String token, @PostMapping(value = "/scanOrConfirm")
@RequestParam("userId") String userId, @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "扫码获得的code", required = true),
@RequestParam("type") Integer type) { @ApiImplicitParam(name = "userId", value = "用户id", required = true),
try { @ApiImplicitParam(name = "type", value = "type 1-扫码 2-确认", required = true)})
userInfoService.scan(userId, token, type); public Result<Void> scanOrConfirm(@RequestParam("token") String token,
return Result.success(); @RequestParam("userId") String userId,
} catch (Exception e) { @RequestParam("type") Integer type) {
throw new RuntimeException(e); try {
} userInfoService.scan(userId, token, type);
return Result.success();
} catch (Exception e) {
throw new RuntimeException(e);
} }
}
} }

Loading…
Cancel
Save