插件开发工具库,推荐依赖该工具库。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
4.3 KiB

package com.fanruan.api.decision.login;
import com.fr.decision.authorize.Passport;
import com.fr.decision.authorize.impl.HttpPassport;
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;
import javax.servlet.http.HttpServletResponse;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-16
*/
public class LoginKit {
/**
* 获取决策平台设置的认证类型
*
* @return 认证对象
*/
public static Passport getCurrentPassport() {
return FSConfig.getInstance().getPassport();
}
/**
* 获取指定类型的通行证,如果不存在,就返回null
*
* @param type 通行证类型
* @param <T> 类型
* @return 通行证
*/
public static <T> T getPassport(Class<? extends T> type) {
return null;
}
/**
* 获取http认证的地址
*
* @return http认证地址
*/
public static @Nullable String getHttpPassportUrl() {
Passport passport = getCurrentPassport();
if (passport instanceof HttpPassport) {
return ((HttpPassport) passport).getUrl();
}
return null;
}
/**
* 获取http认证的秘钥
*
* @return http认证地址
*/
public static @Nullable String getHttpPassportKey() {
Passport passport = getCurrentPassport();
if (passport instanceof HttpPassport) {
return ((HttpPassport) passport).getPublicKey();
}
return null;
}
/**
* 登录
*
* @param req http请求
* @param res http响应
* @param loginRequest 登录请求信息
* @return 登录响应信息
* @throws Exception 登录过程中发生错误,则抛出此异常
*/
public static OpenLoginResponse login(HttpServletRequest req, HttpServletResponse res, OpenLoginRequest loginRequest) throws Exception {
return OpenLoginResponse.wrap(LoginService.getInstance().login(req, res, loginRequest.select()));
}
/**
* 刷新用户名对应的token,如果非保持登录,会使用tokenTimeout参数设置时长
*
* @param oldJwt 老的token
* @param socketIOClient socket客户端
* @param tokenTimeout 设置的超时时长,仅对非保持登录有用,0<= tokenTimeOut <= LoginConfig.LoginTimeOut
* @throws Exception 异常
*/
public static void refreshToken(String oldJwt, long tokenTimeout, SocketIOClient socketIOClient) throws Exception {
LoginService.getInstance().refreshToken(oldJwt, tokenTimeout, socketIOClient);
}
/**
* 刷新用户名对应的token,并将响应利用web工具封装至res
*
* @param oldJwt 老的token
* @param socketIOClient socket客户端
* @param tokenTimeout 设置的超时时长,仅对非保持登录有用,0<= tokenTimeOut <= LoginConfig.LoginTimeOut
* @param res 刷新后,得到的响应会封装至此HttpServletResponse中
* @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));
}
}
/**
* 获取选择的登录页插件id
*
* @return id
*/
public static String getLoginPageId() {
return AppearanceConfig.getInstance().getLoginPageId();
}
}