|
@@ -2,28 +2,23 @@ package io.github.nnkwrik.authservice.controller;
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaUserService;
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
-import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import io.github.nnkwrik.authservice.config.WxMaConfiguration;
|
|
|
import io.github.nnkwrik.authservice.dto.AuthDTO;
|
|
|
import io.github.nnkwrik.authservice.dto.DetailAuthDTO;
|
|
|
import io.github.nnkwrik.authservice.model.vo.AuthVo;
|
|
|
import io.github.nnkwrik.authservice.mq.RegisterStreamSender;
|
|
|
-import io.github.nnkwrik.authservice.token.TokenCreator;
|
|
|
-import io.github.nnkwrik.common.dto.JWTUser;
|
|
|
+import io.github.nnkwrik.authservice.service.AuthService;
|
|
|
import io.github.nnkwrik.common.dto.Response;
|
|
|
import io.github.nnkwrik.common.mq.UserRegisterStream;
|
|
|
-import io.github.nnkwrik.common.util.JsonUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.stream.annotation.EnableBinding;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
@@ -33,25 +28,20 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
@EnableBinding(UserRegisterStream.class)
|
|
|
+@RequestMapping("/auth")
|
|
|
public class AuthController {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
- private TokenCreator creator;
|
|
|
+ private AuthService authService;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private RegisterStreamSender registerSender;
|
|
|
|
|
|
-// @GetMapping("/test/{message}")
|
|
|
-// public String test(@PathVariable String message) {
|
|
|
-// registerSender.send(message);
|
|
|
-// return "test is success";
|
|
|
-// }
|
|
|
|
|
|
-
|
|
|
- @PostMapping("/auth/loginByWeixin")
|
|
|
- public Response loginByWeixin(@RequestBody AuthDTO authDTO) throws IOException {
|
|
|
+ @PostMapping("/loginByWeixin")
|
|
|
+ public Response loginByWeixin(@RequestBody AuthDTO authDTO) {
|
|
|
|
|
|
log.info("用户登录 : {}", authDTO);
|
|
|
WxMaUserService wxUserService = WxMaConfiguration.getMaServices().getUserService();
|
|
@@ -76,50 +66,37 @@ public class AuthController {
|
|
|
return Response.fail(Response.CHECK_USER_WITH_SESSION_FAIL, message);
|
|
|
}
|
|
|
|
|
|
- String userData = setOpenId4Data(detail.getRawData(), openId);
|
|
|
- if (authDTO.getFirstLogin()) {
|
|
|
- //异步调用user-service注册到数据库
|
|
|
- registerSender.send(userData);
|
|
|
- }
|
|
|
+ String userData = authService.setOpenId4Data(detail.getRawData(), openId);
|
|
|
+ //异步调用user-service注册到数据库
|
|
|
+ registerSender.send(userData);
|
|
|
|
|
|
//构造JWT token
|
|
|
- AuthVo vo = createToken(userData);
|
|
|
+ AuthVo vo = authService.createToken(userData);
|
|
|
|
|
|
log.info("认证成功,用户信息 : {}", vo);
|
|
|
return Response.ok(vo);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 在原有的rawData中附上openId
|
|
|
+ * 测试环境专用
|
|
|
*
|
|
|
- * @param rawData
|
|
|
- * @param openId
|
|
|
+ * @param
|
|
|
* @return
|
|
|
- * @throws IOException
|
|
|
*/
|
|
|
- private String setOpenId4Data(String rawData, String openId) throws IOException {
|
|
|
-
|
|
|
+ @PostMapping("/loginByWeixinDev")
|
|
|
+ public Response loginByWeixinDev(@RequestBody Map<String, String> jsonMap, @RequestHeader("Authorization") String token) throws JsonProcessingException {
|
|
|
+ log.info("用户登录 : {}", "测试");
|
|
|
+// JsonUtil.fromJson()
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
- ObjectNode node = (ObjectNode) mapper.readTree(rawData);
|
|
|
- node.remove("openId");
|
|
|
- node.put("openId", openId);
|
|
|
- return node.toString();
|
|
|
- }
|
|
|
+ String userData = mapper.writeValueAsString(jsonMap);
|
|
|
+
|
|
|
+ //异步调用user-service注册到数据库
|
|
|
+ registerSender.send(userData);
|
|
|
+
|
|
|
+ AuthVo vo = authService.createToken(userData);
|
|
|
+ return Response.ok(vo);
|
|
|
|
|
|
- /**
|
|
|
- * 通过json用户信息构造vo
|
|
|
- * @param userData
|
|
|
- * @return
|
|
|
- */
|
|
|
- private AuthVo createToken(String userData) {
|
|
|
- Map<String, String> rawData =
|
|
|
- JsonUtil.fromJson(userData, new TypeReference<Map<String, String>>() {
|
|
|
- });
|
|
|
- String openId = rawData.get("openId");
|
|
|
- String nickname = rawData.get("nickName");
|
|
|
- String avatar = rawData.get("avatarUrl");
|
|
|
- String token = creator.create(openId, nickname, avatar);
|
|
|
- return new AuthVo(token, nickname, avatar);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|