|
@@ -1,5 +1,7 @@
|
|
|
package com.yonge.cooleshow.auth.web.controller;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
import com.yonge.cooleshow.auth.api.dto.QRLoginDto;
|
|
|
import com.yonge.cooleshow.common.enums.CacheNameEnum;
|
|
|
import com.yonge.cooleshow.common.util.TokenUtil;
|
|
@@ -68,10 +70,14 @@ public class TokenController extends BaseController {
|
|
|
|
|
|
@PostMapping(value = "/smsLogin", consumes = MediaType.APPLICATION_JSON_VALUE)
|
|
|
@ApiOperation(value = "短信验证码的方式登录")
|
|
|
- @ApiImplicitParams({@ApiImplicitParam(name = "smsCode", value = "验证码", required = true, dataType = "String"),
|
|
|
- @ApiImplicitParam(name = "clientSecret", value = "固定传 app", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParams({
|
|
|
@ApiImplicitParam(name = "clientId", value = "固定传 app", required = true, dataType = "String"),
|
|
|
- @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")})
|
|
|
+ @ApiImplicitParam(name = "clientSecret", value = "固定传 app", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "smsCode", value = "验证码", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "isSurportRegister", value = "是否在登录时注册", dataType = "Boolean"),
|
|
|
+ @ApiImplicitParam(name = "loginUserType", value = "登录用户类型 STUDENT TEACHER(官网登录)", dataType = "String")
|
|
|
+ })
|
|
|
public Object smsLogin() {
|
|
|
return succeed();
|
|
|
}
|
|
@@ -97,10 +103,13 @@ public class TokenController extends BaseController {
|
|
|
|
|
|
@PostMapping(value = "/usernameLogin", consumes = MediaType.APPLICATION_JSON_VALUE)
|
|
|
@ApiOperation(value = "手机号密码方式登录")
|
|
|
- @ApiImplicitParams({@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
|
|
|
- @ApiImplicitParam(name = "clientSecret", value = "固定传 app", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParams({
|
|
|
@ApiImplicitParam(name = "clientId", value = "固定传 app", required = true, dataType = "String"),
|
|
|
- @ApiImplicitParam(name = "username", value = "手机号", required = true, dataType = "String")})
|
|
|
+ @ApiImplicitParam(name = "clientSecret", value = "固定传 app", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "username", value = "手机号", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "loginUserType", value = "登录用户类型 STUDENT TEACHER(官网登录)", dataType = "String")
|
|
|
+ })
|
|
|
public Object usernameLogin() {
|
|
|
return succeed();
|
|
|
}
|
|
@@ -175,13 +184,8 @@ public class TokenController extends BaseController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 获取二维码登录code(不需要鉴权)
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
@GetMapping("/getQRLoginCode")
|
|
|
- @ApiOperation(value = "获取二维码登录code")
|
|
|
+ @ApiOperation(value = "获取二维码登录code(不需要鉴权)")
|
|
|
public HttpResponseResult<QRLoginDto> getQRLoginCode(
|
|
|
@ApiParam(value = "clientId", required = true) @RequestParam("clientId") String clientId,
|
|
|
@ApiParam(value = "clientSecret", required = true) @RequestParam("clientSecret") String clientSecret) {
|
|
@@ -206,13 +210,8 @@ public class TokenController extends BaseController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 前端轮询登录(不需要鉴权)
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
@GetMapping("/pollingQRLoginCode")
|
|
|
- @ApiOperation(value = "获取二维码登录code")
|
|
|
+ @ApiOperation(value = "前端轮询登录(不需要鉴权)")
|
|
|
public HttpResponseResult<QRLoginDto> pollingQRLoginCode(@ApiParam(value = "二维码登录code", required = true) @RequestParam("code") String code) {
|
|
|
if (StringUtil.isEmpty(code)) {
|
|
|
return HttpResponseResult.failed("登录失败");
|
|
@@ -223,62 +222,80 @@ public class TokenController extends BaseController {
|
|
|
QRLoginDto dto = (QRLoginDto) data;
|
|
|
return HttpResponseResult.succeed(dto);
|
|
|
} else {
|
|
|
- return HttpResponseResult.failed("二维码已过期");
|
|
|
+ return HttpResponseResult.succeed("二维码已过期");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 二维码登录
|
|
|
- *
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
@GetMapping(value = "/qrLogin")
|
|
|
- @ApiOperation(value = "二维码登录")
|
|
|
+ @ApiOperation(value = "二维码登录-扫码")
|
|
|
public HttpResponseResult<QRLoginDto> qrLogin(
|
|
|
@ApiParam(value = "二维码登录code", required = true) @RequestParam("code") String code
|
|
|
) throws IOException {
|
|
|
+ AuthUser authUser = SecurityUtils.getUser();
|
|
|
+ if (authUser == null) {
|
|
|
+ return HttpResponseResult.failed("请先登录");
|
|
|
+ }
|
|
|
+ SysUser sysUser = userService.get(authUser.getUserId());
|
|
|
+
|
|
|
Object data = redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code))
|
|
|
.get();
|
|
|
if (null == data) {
|
|
|
return HttpResponseResult.failed("二维码已过期");
|
|
|
}
|
|
|
+ QRLoginDto dto = (QRLoginDto) data;
|
|
|
+ dto.setCodeStatus("scanned");
|
|
|
+ dto.setPhone(sysUser.getPhone());
|
|
|
+ dto.setUserType(authUser.getClientId().toUpperCase());
|
|
|
+ redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code)).set(dto, 5, TimeUnit.MINUTES);
|
|
|
+ return succeed(dto);
|
|
|
+ }
|
|
|
|
|
|
+ @GetMapping(value = "/doQrLogin")
|
|
|
+ @ApiOperation(value = "二维码登录-确认登录")
|
|
|
+ public HttpResponseResult<QRLoginDto> doQrLogin(
|
|
|
+ @ApiParam(value = "二维码登录code", required = true) @RequestParam("code") String code
|
|
|
+ ) throws IOException {
|
|
|
AuthUser authUser = SecurityUtils.getUser();
|
|
|
if (authUser == null) {
|
|
|
return HttpResponseResult.failed("请先登录");
|
|
|
}
|
|
|
+ SysUser sysUser = userService.get(authUser.getUserId());
|
|
|
+
|
|
|
+ Object data = redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code))
|
|
|
+ .get();
|
|
|
+ if (null == data) {
|
|
|
+ return HttpResponseResult.failed("二维码已过期");
|
|
|
+ }
|
|
|
QRLoginDto dto = (QRLoginDto) data;
|
|
|
- dto.setCodeStatus("scanned");
|
|
|
- redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code)).set(dto,5,TimeUnit.MINUTES);
|
|
|
|
|
|
- String url = "http://auth-server/oauth/token";
|
|
|
+ String url = "http://auth-server/smsLogin";
|
|
|
|
|
|
- String userType = TokenUtil.getClientIdFromHeader();
|
|
|
String base64ClientCredentials = Base64.getEncoder().encodeToString((dto.getClientId() + ":" + dto.getClientSecret()).getBytes());
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.add("Authorization", "Basic " + base64ClientCredentials);
|
|
|
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
|
- MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
|
|
|
- params.add("client_id", dto.getClientId());
|
|
|
- params.add("client_secret", dto.getClientSecret());
|
|
|
- params.add("username", authUser.getUsername());
|
|
|
- params.add("password", authUser.getPassword());
|
|
|
- params.add("grant_type", "password");
|
|
|
+ url += "?clientId=" + dto.getClientId();
|
|
|
+ url += "&clientSecret=" + dto.getClientSecret();
|
|
|
+ url += "&phone=" + sysUser.getPhone();
|
|
|
+ url += "&qrCode=" + code;
|
|
|
+ url += "&loginUserType=" + authUser.getClientId().toUpperCase();
|
|
|
|
|
|
- HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(headers);
|
|
|
|
|
|
ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
|
|
|
|
|
Map map = objectMapper.readValue(resp.getBody(), Map.class);
|
|
|
-
|
|
|
- dto.setUserType(userType);
|
|
|
- dto.setCodeStatus("succeed");
|
|
|
- dto.setToken(map);
|
|
|
- redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code)).set(dto,5,TimeUnit.MINUTES);
|
|
|
- return succeed(dto);
|
|
|
+ Boolean status = (Boolean) map.get("status");
|
|
|
+ if (status) {
|
|
|
+ dto.setCodeStatus("succeed");
|
|
|
+ Object obj = JSONObject.parseObject(JSONObject.toJSONString(map.get("data"))).get("authentication");
|
|
|
+ dto.setAuthentication(obj);
|
|
|
+ } else {
|
|
|
+ dto.setCodeStatus("filed");
|
|
|
+ }
|
|
|
+ redissonClient.getBucket(CacheNameEnum.QR_LOGIN_CODE.getRedisKey(code)).set(dto, 5, TimeUnit.MINUTES);
|
|
|
+ return HttpResponseResult.succeed(dto);
|
|
|
}
|
|
|
-
|
|
|
}
|