zouxuan 1 år sedan
förälder
incheckning
07046b78de

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

@@ -10,6 +10,7 @@ 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.common.exceptions.InvalidTokenException;
 import org.springframework.security.oauth2.provider.OAuth2Authentication;
 import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
 import org.springframework.stereotype.Service;
@@ -33,7 +34,8 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
     @Override
     public CbsQrCodeScanWrapper.UserInfo userInfo(CbsQrCodeScanWrapper.QrCodeScanUserInfoReq req) {
         CbsQrCodeScanWrapper.UserInfo userInfo = new CbsQrCodeScanWrapper.UserInfo();
-        userInfo.setUsername(customTokenServices.loadAuthentication(req.getToken()).getName());
+        userInfo.setUsername(customTokenServices.loadAuthentication(req.getToken()).getName().split(":")[1]);
+        userInfo.setClientTypes("TEACHER");
         return userInfo;
     }
 
@@ -42,9 +44,9 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
         OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(req.getPassword());
         if (oAuth2AccessToken != null) {
             OAuth2Authentication authentication = redisTokenStore.readAuthentication(req.getPassword());
-            Map<String,String> hashMap = objectMapper.convertValue(authentication.getUserAuthentication().getPrincipal(), HashMap.class);
-            String userId = hashMap.get("userId");
-            SysUser sysUser = sysUserFeignService.queryUserById(Integer.parseInt(userId));
+            Map<String,Object> hashMap = objectMapper.convertValue(authentication.getUserAuthentication().getPrincipal(), HashMap.class);
+            Object userId = hashMap.get("userId");
+            SysUser sysUser = sysUserFeignService.queryUserById(Integer.parseInt(userId.toString()));
             if (sysUser == null) {
                 throw new BizException(HttpStatus.UNAUTHORIZED.value(), "用户不存在");
             }
@@ -56,6 +58,6 @@ public class CbsQrCodeScanServiceImpl implements CbsQrCodeScanService {
             qrCodeScanToken.setTokenData(result.getData().get("access_token"));
             return qrCodeScanToken;
         }
-        return null;
+        throw new InvalidTokenException("Invalid access token: " + req.getPassword());
     }
 }