Parcourir la source

机构退出账号

liujc il y a 1 an
Parent
commit
acc600b37b

+ 22 - 0
cooleshow-auth/auth-server/src/main/java/com/yonge/cooleshow/auth/core/service/CustomTokenServices.java

@@ -30,6 +30,7 @@ import org.springframework.security.web.authentication.preauth.PreAuthenticatedA
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
+import java.text.MessageFormat;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Set;
@@ -294,6 +295,27 @@ public class CustomTokenServices implements AuthorizationServerTokenServices, Re
 		return true;
 	}
 
+
+    public boolean revokeTokenByOpenId(String clientId, String phone,String openId) {
+        Collection<OAuth2AccessToken> list = tokenStore.findTokensByClientIdAndUserName(clientId, SecurityConstants.PHONE_PRINCIPAL_PREFIX + phone);
+
+        if (list == null || list.size() == 0) {
+            list = tokenStore.findTokensByClientIdAndUserName(clientId, MessageFormat.format("{0}:{1}:{2}", SecurityConstants.MA_PRINCIPAL_PREFIX, openId, phone));
+        }
+
+        if (list != null) {
+            for (OAuth2AccessToken accessToken : list) {
+                if (accessToken != null) {
+                    if (accessToken.getRefreshToken() != null) {
+                        tokenStore.removeRefreshToken(accessToken.getRefreshToken());
+                    }
+                    tokenStore.removeAccessToken(accessToken);
+                }
+            }
+        }
+        return true;
+    }
+
 	public void revokeTokenByPhone(String phone) {
 		String[] clientIds = new String[] {"system", "student", "teacher","website"};
 		for (String cId : clientIds) {

+ 11 - 0
cooleshow-auth/auth-server/src/main/java/com/yonge/cooleshow/auth/web/controller/TokenController.java

@@ -1,5 +1,6 @@
 package com.yonge.cooleshow.auth.web.controller;
 
+import cn.hutool.core.net.URLEncodeUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -175,6 +176,16 @@ public class TokenController extends BaseController {
         return tokenService.revokeToken(clientId, phone) ? succeed("退出成功") : failed();
     }
 
+
+    @PostMapping(value = "exit/{clientId}/{phone}/{openId}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+    @ApiOperation(value = "指定机构用户退出登录")
+    public HttpResponseResult<String> exitByPhoneAndOpenId(@PathVariable("clientId") String clientId,
+                                                           @PathVariable("phone") String phone,@PathVariable("openId") String openId) {
+
+        return tokenService.revokeTokenByOpenId(clientId, phone,openId) ? succeed("退出成功") : failed();
+    }
+
+
     @GetMapping(value = "/checkToken", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
     @ApiOperation(value = "检查token")
     public HttpResponseResult<Object> checkToken(HttpServletRequest request) throws IOException {