Browse Source

Merge pull request #39 in TC/finekit from ~LIDONGY/finekit:master to master

* commit '5de53db4f11b35d814f05e07c0a4304d8891653f':
  KERNEL-1444 刷新token并输出结果
pull/1/head
superman 5 years ago
parent
commit
5df95e1841
  1. 37
      src/main/java/com/fanruan/api/decision/login/LoginKit.java
  2. 8
      src/test/java/com/fanruan/api/decision/login/LoginKitTest.java

37
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));
}
}
/**

8
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);
}

Loading…
Cancel
Save