Browse Source

修改接口适应账户中心

yingp 7 years ago
parent
commit
7032f979fa

+ 5 - 5
base-servers/auth/auth-server/src/main/java/com/usoftchina/saas/auth/controller/AuthController.java

@@ -124,9 +124,9 @@ public class AuthController {
      * @param info
      * @return
      */
-    @GetMapping("/sso/callback")
+    @GetMapping("/sso/callback/{clientId}")
     public void ssoCallback(HttpServletRequest request, HttpServletResponse response,
-                            String clientId, CookieInfo info) throws IOException{
+                            @PathVariable(required = false) String clientId, CookieInfo info, String callback) throws IOException{
         if (null != info && null != info.getMobile()) {
             AccountDTO accountDTO = null;
             Result<AccountDTO> result = accountApi.getAccount(info.getMobile());
@@ -136,7 +136,7 @@ public class AuthController {
                     accountDTO = createAccountByCookieInfo(info);
                 } else {
                     logger.error(result.getMessage());
-                    ServletErrorUtils.writeMessage(response, "successCallback({success:'0'})");
+                    ServletErrorUtils.writeJsonPMessage(response, callback, false);
                     return;
                 }
             } else {
@@ -147,7 +147,7 @@ public class AuthController {
                     Result updateResult = accountApi.update(BeanMapper.map(accountDTO, AccountUpdateDTO.class));
                     if (!updateResult.isSuccess()) {
                         logger.error(updateResult.getMessage());
-                        ServletErrorUtils.writeMessage(response, "successCallback({success:'0'})");
+                        ServletErrorUtils.writeJsonPMessage(response, callback, false);
                         return;
                     }
                 }
@@ -169,7 +169,7 @@ public class AuthController {
                 TokenDTO tokenDTO = BeanMapper.map(jwtToken, TokenDTO.class);
                 socketMessageApi.sendToClient(clientId, JsonUtils.toJsonString(new AuthDTO(tokenDTO, accountDTO)));
             }
-            ServletErrorUtils.writeMessage(response, "successCallback({success:'1'})");
+            ServletErrorUtils.writeJsonPMessage(response, callback, true);
         }
     }
 

+ 1 - 1
framework/server-starter/src/main/java/com/usoftchina/saas/server/ServerAutoConfiguration.java

@@ -38,7 +38,7 @@ public class ServerAutoConfiguration {
 //                }
 //                logger.error(ServletErrorUtils.buildMessage(request, cause), cause);
 //                try {
-//                    ServletErrorUtils.writerErrorResult(response, cause);
+//                    ServletErrorUtils.writeErrorResult(response, cause);
 //                } catch (IOException ex) {
 //                }
 //                return null;

+ 30 - 1
framework/server-starter/src/main/java/com/usoftchina/saas/server/error/ServletErrorUtils.java

@@ -43,10 +43,39 @@ public class ServletErrorUtils {
      * @throws IOException
      * @throws ServletException
      */
-    public static void writerErrorResult(HttpServletResponse response, Throwable e) throws IOException {
+    public static void writeErrorResult(HttpServletResponse response, Throwable e) throws IOException {
         response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
         PrintWriter writer = response.getWriter();
         writer.print(JsonUtils.toJsonString(Result.throwable(e)));
         writer.flush();
     }
+
+    public static void writeMessage(HttpServletResponse response, String text) throws IOException {
+        response.setContentType(MediaType.TEXT_HTML_VALUE);
+        response.setCharacterEncoding("UTF-8");
+        PrintWriter writer = response.getWriter();
+        writer.print(text);
+        writer.flush();
+    }
+
+    /**
+     * 输出jsonp
+     *
+     * @param response
+     * @param callbackFn
+     * @param success
+     * @throws IOException
+     */
+    public static void writeJsonPMessage(HttpServletResponse response, String callbackFn, boolean success) throws IOException {
+        response.setContentType(MediaType.TEXT_HTML_VALUE);
+        response.setCharacterEncoding("UTF-8");
+        PrintWriter writer = response.getWriter();
+        writer.print(buildJsonPMessage(callbackFn, success));
+        writer.flush();
+    }
+
+    public static String buildJsonPMessage(String callbackFn, boolean success) {
+        return String.format("%s({success:\"%s\"})", callbackFn, success ? 1 : 0);
+    }
+
 }

+ 1 - 1
framework/server-starter/src/main/java/com/usoftchina/saas/server/error/UnCaughtErrorFilter.java

@@ -30,7 +30,7 @@ public class UnCaughtErrorFilter extends OncePerRequestFilter {
                 cause = ((NestedServletException) ex).getRootCause();
             }
             logger.error(ServletErrorUtils.buildMessage(request, cause), cause);
-            ServletErrorUtils.writerErrorResult(response, cause);
+            ServletErrorUtils.writeErrorResult(response, cause);
         }
     }
 }