Jelajahi Sumber

Merge branch 'zx_saas_qrLogin' of http://git.dayaedu.com/yonge/mec into test

zouxuan 1 tahun lalu
induk
melakukan
5762329980

+ 8 - 0
mec-auth/mec-auth-api/src/main/java/com/ym/mec/auth/api/client/SysUserFeignService.java

@@ -7,11 +7,13 @@ import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.common.config.FeignConfiguration;
 import com.ym.mec.common.entity.HttpResponseResult;
 import io.swagger.annotations.ApiOperation;
+import org.json.JSONObject;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.Map;
 
 @FeignClient(contextId = "sysUserFeignService", name = "auth-server", configuration = { FeignConfiguration.class }, fallback = SysUserFeignServiceFallback.class)
 public interface SysUserFeignService {
@@ -57,4 +59,10 @@ public interface SysUserFeignService {
 
 	@PostMapping(value = "open/qrcode/login")
 	CbsQrCodeScanWrapper.QrCodeScanToken login(@RequestBody CbsQrCodeScanWrapper.QrCodeScanReq req);
+
+	@PostMapping(value = "smsLogin")
+	HttpResponseResult<Map<String,Object>> smsLogin(@RequestParam("phone")String phone,
+													@RequestParam("clientId")String clientId,
+													@RequestParam("clientType")String clientType,
+													@RequestParam("clientSecret")String clientSecret);
 }

+ 14 - 6
mec-auth/mec-auth-api/src/main/java/com/ym/mec/auth/api/client/fallback/SysUserFeignServiceFallback.java

@@ -1,15 +1,15 @@
 package com.ym.mec.auth.api.client.fallback;
 
-import java.util.List;
-import java.util.Map;
-
 import com.dayaedu.cbs.openfeign.wrapper.qrcode.CbsQrCodeScanWrapper;
-import com.ym.mec.auth.api.dto.SysUserQueryInfo;
-import org.springframework.stereotype.Component;
-
 import com.ym.mec.auth.api.client.SysUserFeignService;
+import com.ym.mec.auth.api.dto.SysUserQueryInfo;
 import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.common.entity.HttpResponseResult;
+import org.json.JSONObject;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.util.List;
+import java.util.Map;
 
 //@Component
 public class SysUserFeignServiceFallback implements SysUserFeignService {
@@ -73,4 +73,12 @@ public class SysUserFeignServiceFallback implements SysUserFeignService {
 		return null;
 	}
 
+	@Override
+	public HttpResponseResult<Map<String,Object>> smsLogin(@RequestParam("phone")String phone,
+														   @RequestParam("clientId")String clientId,
+														   @RequestParam("clientType")String clientType,
+														   @RequestParam("clientSecret")String clientSecret) {
+		return null;
+	}
+
 }

+ 1 - 1
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/handler/BaseAuthenticationSuccessEventHandler.java

@@ -128,7 +128,7 @@ public class BaseAuthenticationSuccessEventHandler extends SavedRequestAwareAuth
 		sysUserLoginLogService.insert(sysUserLoginLog);
 		
 		try {
-			String clientId = request.getParameter("clientId").replace("QR_", "");
+			String clientId = request.getParameter("clientId");
 			String clientSecret = request.getParameter("clientSecret");
 			if (clientId == null || clientSecret == null) {
 				throw new UnapprovedClientAuthenticationException("请求头中client信息为空");

+ 4 - 4
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/provider/PhoneAuthenticationProvider.java

@@ -59,7 +59,7 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
         String clientId = loginEntity.getClientId();
 
         // 验证码验证
-        if (!clientId.startsWith("QR_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
+        if (!loginEntity.getClientId().toLowerCase().startsWith("qr_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
             throw new BadCredentialsException("验证码校验失败");
         }
 
@@ -71,7 +71,7 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
 
         SysUserInfo userInfo;
 
-        if (clientId.startsWith("QR_")) {
+        /*if (clientId.startsWith("QR_")) {
             Object data = redisCache.get(loginEntity.getPhone());
             if (data == null) {
                 throw new LockedException("用户不存在");
@@ -86,8 +86,8 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
                 }
             }
         } else {
-            userInfo = sysUserService.queryUserInfoByPhone(phone);
-        }
+        }*/
+        userInfo = sysUserService.queryUserInfoByPhone(phone);
 
 		if (userInfo == null) {
 

+ 1 - 1
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/service/CustomAuthenticationKeyGenerator.java

@@ -27,7 +27,7 @@ public class CustomAuthenticationKeyGenerator extends DefaultAuthenticationKeyGe
 			values.put(USERNAME, StringUtils.substringAfter(authentication.getName(), ":"));
 		}
 		String clientId = authorizationRequest.getClientId();
-		clientId = clientId.replace("QR_", "");
+//		clientId = clientId.replace("QR_", "");
 		values.put(CLIENT_ID, clientId);
 		if (authorizationRequest.getScope() != null) {
 			values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<String>(authorizationRequest.getScope())));

+ 2 - 1
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/service/CustomTokenServices.java

@@ -234,7 +234,8 @@ public class CustomTokenServices implements AuthorizationServerTokenServices, Re
 
 		OAuth2Authentication authentication = tokenStore.readAuthentication(accessToken);
 
-		if (StringUtils.equalsIgnoreCase("system", authentication.getOAuth2Request().getClientId())) {
+		if (StringUtils.equalsIgnoreCase("system", authentication.getOAuth2Request().getClientId())
+		|| StringUtils.equalsIgnoreCase("qr_teacher", authentication.getOAuth2Request().getClientId())) {
 			int validitySeconds = getAccessTokenValiditySeconds(authentication.getOAuth2Request());
 			if (validitySeconds > 0) {
 				oAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));

+ 20 - 5
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/service/impl/CbsQrCodeScanServiceImpl.java

@@ -1,16 +1,20 @@
 package com.ym.mec.auth.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.dayaedu.cbs.openfeign.wrapper.qrcode.CbsQrCodeScanWrapper;
+import com.ym.mec.auth.api.client.SysUserFeignService;
 import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.auth.core.service.CustomTokenServices;
 import com.ym.mec.auth.service.SysUserService;
+import com.ym.mec.common.entity.HttpResponseResult;
 import com.ym.mec.common.exception.BizException;
 import org.springframework.http.HttpStatus;
-import org.springframework.security.oauth2.common.OAuth2AccessToken;
 import org.springframework.security.oauth2.provider.OAuth2Authentication;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.Map;
+import java.util.UUID;
 
 @Service
 public class CbsQrCodeScanServiceImpl{
@@ -19,6 +23,8 @@ public class CbsQrCodeScanServiceImpl{
     private CustomTokenServices customTokenServices;
     @Resource
     private SysUserService sysUserService;
+    @Resource
+    private SysUserFeignService sysUserFeignService;
 
     public CbsQrCodeScanWrapper.UserInfo userInfo(CbsQrCodeScanWrapper.QrCodeScanUserInfoReq req) {
         CbsQrCodeScanWrapper.UserInfo userInfo = new CbsQrCodeScanWrapper.UserInfo();
@@ -28,15 +34,24 @@ public class CbsQrCodeScanServiceImpl{
     }
 
     public CbsQrCodeScanWrapper.QrCodeScanToken login(CbsQrCodeScanWrapper.QrCodeScanReq req) {
+        //校验是否过期
         OAuth2Authentication auth2Authentication = customTokenServices.loadAuthentication(req.getPassword());
         String phone = auth2Authentication.getName().split(":")[1];
         SysUser sysUser = sysUserService.queryByPhone(phone);
         if (sysUser == null) {
             throw new BizException(HttpStatus.UNAUTHORIZED.value(), "用户不存在");
         }
-        OAuth2AccessToken accessToken = customTokenServices.newAccessToken(auth2Authentication);
-        CbsQrCodeScanWrapper.QrCodeScanToken qrCodeScanToken = new CbsQrCodeScanWrapper.QrCodeScanToken();
-        qrCodeScanToken.setTokenData(accessToken.getValue());
-        return qrCodeScanToken;
+        //调用登陆接口
+        HttpResponseResult<Map<String,Object>> result = sysUserFeignService.smsLogin(phone, "qr_teacher", UUID.randomUUID().toString(), "qr_teacher");
+        if (result != null){
+            if(result.getCode() != 200){
+                throw new BizException("扫码登陆失败", result.getMsg());
+            }
+            CbsQrCodeScanWrapper.QrCodeScanToken qrCodeScanToken = new CbsQrCodeScanWrapper.QrCodeScanToken();
+            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(result.getData().get("authentication")));
+            qrCodeScanToken.setTokenData(jsonObject.getString("access_token"));
+            return qrCodeScanToken;
+        }
+        throw new BizException("扫码登陆失败", "调用登陆接口失败");
     }
 }