From ea7804b54e773ead341e69b3cb71c7071db9e500 Mon Sep 17 00:00:00 2001 From: lidongy <1879087903@qq.com> Date: Tue, 29 Oct 2019 10:44:13 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-1444=20=E5=88=B7=E6=96=B0token=E5=B9=B6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lidongy <1879087903@qq.com> --- .../fanruan/api/decision/login/LoginKit.java | 37 +++++++++++++++---- .../api/decision/login/LoginKitTest.java | 8 +--- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/fanruan/api/decision/login/LoginKit.java b/src/main/java/com/fanruan/api/decision/login/LoginKit.java index 85f7ce8..8b6596f 100644 --- a/src/main/java/com/fanruan/api/decision/login/LoginKit.java +++ b/src/main/java/com/fanruan/api/decision/login/LoginKit.java @@ -6,7 +6,11 @@ import com.fr.decision.config.AppearanceConfig; import com.fr.decision.config.FSConfig; import com.fr.decision.webservice.Response; import com.fr.decision.webservice.v10.login.LoginService; +import com.fr.json.JSONObject; +import com.fr.stable.StringUtils; +import com.fr.third.fasterxml.jackson.databind.ObjectMapper; import com.fr.third.socketio.SocketIOClient; +import com.fr.web.utils.WebUtils; import org.jetbrains.annotations.Nullable; import javax.servlet.http.HttpServletRequest; @@ -87,13 +91,32 @@ public class LoginKit { * @return 新的token * @throws Exception 异常 */ - public static Object refreshToken(String oldJwt, long tokenTimeout, SocketIOClient socketIOClient, Object responseData, String errorCode, String errorMsg) throws Exception{ - Response response = new Response(); - response = LoginService.getInstance().refreshToken(oldJwt, tokenTimeout, socketIOClient); - responseData = response.getData(); - errorCode = response.getErrorCode(); - errorMsg = response.getErrorMsg(); - return response; + public static void refreshToken(String oldJwt, long tokenTimeout, SocketIOClient socketIOClient) throws Exception { + LoginService.getInstance().refreshToken(oldJwt, tokenTimeout, socketIOClient); + } + + /** + * 刷新用户名对应的token,并输出response + * + * @param oldJwt 老的token + * @param socketIOClient socket客户端 + * @param tokenTimeout 设置的超时时长,仅对非保持登录有用,0<= tokenTimeOut <= LoginConfig.LoginTimeOut + * @param res HttpServletResponse + * + * @return 新的token + * @throws Exception 异常 + */ + public static void refreshTokenAndWriteResponse(String oldJwt, long tokenTimeout, SocketIOClient socketIOClient, HttpServletResponse res) throws Exception { + Response response = LoginService.getInstance().refreshToken(oldJwt, tokenTimeout, socketIOClient); + if (StringUtils.isNotEmpty(response.getErrorCode())) { + JSONObject object = new JSONObject(); + object.put("errorCode", response.getErrorCode()); + object.put("errorMsg", response.getErrorMsg()); + WebUtils.printAsJSON(res, object); + } else if (response.getData() != null) { + ObjectMapper mapper = new ObjectMapper(); + WebUtils.printAsString(res, mapper.writeValueAsString(response)); + } } /** diff --git a/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java b/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java index 76bf3c3..16deed6 100644 --- a/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java +++ b/src/test/java/com/fanruan/api/decision/login/LoginKitTest.java @@ -6,7 +6,6 @@ import com.fr.config.dao.impl.LocalClassHelperDao; import com.fr.config.dao.impl.LocalEntityDao; import com.fr.config.dao.impl.LocalXmlEntityDao; import com.fr.decision.config.AppearanceConfig; -import com.fr.decision.webservice.Response; import com.fr.decision.webservice.bean.authentication.LoginClientBean; import com.fr.decision.webservice.exception.login.LoginInfoNotAvailableException; import com.fr.decision.webservice.utils.DecisionStatusService; @@ -41,18 +40,15 @@ public class LoginKitTest { @Test public void testRefreshToken() throws Exception{ String jwtString = JwtKit.createDefaultJWT("123"); - Object responseData = new Object(); - String errorCode = ""; - String errorMsg = ""; try { - Response response = (Response) LoginKit.refreshToken(jwtString, 1000 * 60, null, responseData, errorCode, errorMsg); + LoginKit.refreshToken(jwtString, 1000 * 60, null); } catch (Exception e) { Assert.assertTrue(e instanceof LoginInfoNotAvailableException); } LoginClientBean loginClientBean = new LoginClientBean(); DecisionStatusService.loginStatusService().put(jwtString, loginClientBean); try { - Response response = (Response) LoginKit.refreshToken(jwtString, 1000 * 60, null, responseData, errorCode, errorMsg); + LoginKit.refreshToken(jwtString, 1000 * 60, null); } catch (Exception e) { Assert.assertFalse(e instanceof LoginInfoNotAvailableException); }