Browse Source

扫码登陆

zouxuan 1 year ago
parent
commit
57f89f3a98

+ 14 - 0
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/service/CustomTokenServices.java

@@ -115,6 +115,20 @@ public class CustomTokenServices implements AuthorizationServerTokenServices, Re
 
 	}
 
+	@Transactional
+	public OAuth2AccessToken newAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
+		OAuth2RefreshToken refreshToken = createRefreshToken(authentication);
+		OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
+		tokenStore.storeAccessToken(accessToken, authentication);
+		// In case it was modified
+		refreshToken = accessToken.getRefreshToken();
+		if (refreshToken != null) {
+			tokenStore.storeRefreshToken(refreshToken, authentication);
+		}
+		return accessToken;
+
+	}
+
 	@Transactional(noRollbackFor={InvalidTokenException.class, InvalidGrantException.class})
 	public OAuth2AccessToken refreshAccessToken(String refreshTokenValue, TokenRequest tokenRequest)
 			throws AuthenticationException {

+ 14 - 2
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/service/impl/CbsQrCodeScanServiceImpl.java

@@ -41,7 +41,18 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
 
     @Override
     public CbsQrCodeScanWrapper.QrCodeScanToken login(CbsQrCodeScanWrapper.QrCodeScanReq req) {
-        OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(req.getPassword());
+        OAuth2Authentication auth2Authentication = customTokenServices.loadAuthentication(req.getPassword());
+        String phone = auth2Authentication.getName().split(":")[1];
+        SysUser sysUser = sysUserFeignService.queryUserByMobile(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;
+
+        /*OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(req.getPassword());
         if (oAuth2AccessToken != null) {
             OAuth2Authentication authentication = redisTokenStore.readAuthentication(req.getPassword());
             Map<String,Object> hashMap = objectMapper.convertValue(authentication.getUserAuthentication().getPrincipal(), HashMap.class);
@@ -50,6 +61,7 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
             if (sysUser == null) {
                 throw new BizException(HttpStatus.UNAUTHORIZED.value(), "用户不存在");
             }
+            customTokenServices.loadAuthentication()
             HttpResponseResult<Map<String, Object>> result = sysUserFeignService.usernameLogin(sysUser.getPhone(), sysUser.getPassword(), req.getClientId(), req.getClientSecret());
             if (result.getCode() != 200) {
                 throw new BizException(result.getCode(), result.getMsg());
@@ -58,6 +70,6 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
             qrCodeScanToken.setTokenData(result.getData().get("access_token"));
             return qrCodeScanToken;
         }
-        throw new InvalidTokenException("Invalid access token: " + req.getPassword());
+        throw new InvalidTokenException("Invalid access token: " + req.getPassword());*/
     }
 }