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.
192 lines
7.0 KiB
192 lines
7.0 KiB
3 years ago
|
package com.fr.plugin.xx.gxkg.utils;
|
||
|
|
||
|
import com.fr.data.NetworkHelper;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.mobile.terminal.TerminalHandler;
|
||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants;
|
||
|
import com.fr.decision.webservice.utils.DecisionStatusService;
|
||
|
import com.fr.decision.webservice.utils.WebServiceUtils;
|
||
|
import com.fr.decision.webservice.v10.login.LoginService;
|
||
|
import com.fr.decision.webservice.v10.login.TokenResource;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.locale.InterProviderFactory;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.web.Device;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.Properties;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
/**
|
||
|
* @author xx
|
||
|
* @since 2021/8/24
|
||
|
*/
|
||
|
public class CommonUtils {
|
||
|
|
||
|
public static String getProperty(Properties props, String key, String defaultValue, boolean allowBlank) {
|
||
|
String value = props.getProperty(key);
|
||
|
if (StringUtils.isNotBlank(value)) {
|
||
|
return value;
|
||
|
} else {
|
||
|
if (allowBlank) {
|
||
|
LogUtils.warn("Property[" + key + "] value is blank.");
|
||
|
return defaultValue;
|
||
|
} else {
|
||
|
throw new IllegalArgumentException("Property[" + key + "] cann't be blank.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String getProperty(Properties props, String key, boolean allowBlank) {
|
||
|
return getProperty(props, key, null, allowBlank);
|
||
|
}
|
||
|
|
||
|
public static String getProperty(Properties props, String key) {
|
||
|
return getProperty(props, key, null, true);
|
||
|
}
|
||
|
|
||
|
public static boolean isLogin(HttpServletRequest request) {
|
||
|
String oldToken = TokenResource.COOKIE.getToken(request);
|
||
|
return oldToken != null && checkTokenValid(request, (String) oldToken);
|
||
|
}
|
||
|
|
||
|
private static boolean checkTokenValid(HttpServletRequest req, String token) {
|
||
|
try {
|
||
|
Device device = NetworkHelper.getDevice(req);
|
||
|
LoginService.getInstance().loginStatusValid(token, TerminalHandler.getTerminal(req, device));
|
||
|
return true;
|
||
|
} catch (Exception ignore) {
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 跳转到过滤器链中的下一个过滤器
|
||
|
*
|
||
|
* @param request
|
||
|
* @param response
|
||
|
* @param chain
|
||
|
*/
|
||
|
public static void next(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
|
||
|
try {
|
||
|
chain.doFilter(request, response);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(),e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static boolean existUser(String username){
|
||
|
try {
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user == null) {
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
} catch (Exception e) {
|
||
|
|
||
|
}
|
||
|
return false;
|
||
|
|
||
|
}
|
||
|
|
||
|
public static void login(String username, HttpServletRequest request, HttpServletResponse response) {
|
||
|
try {
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user == null) {
|
||
|
throw new RuntimeException("系统未授权, 当前用户是\"" + username + "\"");
|
||
|
}
|
||
|
String token = LoginService.getInstance().login(request, response, user.getUserName());
|
||
|
request.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error("sso >> Failed to login with[" + username + "]", e);
|
||
|
throw new RuntimeException("用户\"" + username + "\"登录失败");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static boolean isMobileDevice(HttpServletRequest request) {
|
||
|
if (WebUtils.getDevice(request).isMobile()) {
|
||
|
LogUtils.debug4plugin("current request is is mobile request ,url is {}", request.getRequestURI());
|
||
|
return true;
|
||
|
}
|
||
|
String requestHeader = request.getHeader("user-agent");
|
||
|
String[] deviceArray = new String[]{"android", "iphone", "ipad", "ios", "windows phone", "wechat"};
|
||
|
if (requestHeader == null) {
|
||
|
return false;
|
||
|
}
|
||
|
requestHeader = requestHeader.toLowerCase();
|
||
|
for (int i = 0; i < deviceArray.length; i++) {
|
||
|
if (requestHeader.toLowerCase().contains(deviceArray[i])) {
|
||
|
LogUtils.debug4plugin("current request:{} is mobile request!", request.getRequestURI());
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
String op = WebUtils.getHTTPRequestParameter(request, "op");
|
||
|
return StringUtils.isNotBlank(op) && StringUtils.equals("h5", op);
|
||
|
}
|
||
|
|
||
|
public static String cacheParams(String values) {
|
||
|
try {
|
||
|
String key = UUID.randomUUID().toString();
|
||
|
DecisionStatusService.originUrlStatusService().put(key, values);
|
||
|
return key;
|
||
|
} catch (Exception e) {
|
||
|
LogUtils.error(e.getMessage(),e);
|
||
|
}
|
||
|
return StringUtils.EMPTY;
|
||
|
}
|
||
|
public static String getCachedParam(String key) {
|
||
|
try {
|
||
|
return DecisionStatusService.originUrlStatusService().get(key);
|
||
|
} catch (Exception e) {
|
||
|
return StringUtils.EMPTY;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void cacheParams(String key, Map<String, String> values) {
|
||
|
try {
|
||
|
DecisionStatusService.originUrlStatusService().put(key, values);
|
||
|
} catch (Exception e) {
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String getCachedParam(String key, String name) {
|
||
|
try {
|
||
|
Map<String, String> values = DecisionStatusService.originUrlStatusService().get(key);
|
||
|
return values.get(name);
|
||
|
} catch (Exception e) {
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void setError(HttpServletResponse res, String reason) {
|
||
|
try {
|
||
|
PrintWriter printWriter = WebUtils.createPrintWriter(res);
|
||
|
Map<String, Object> map = new HashMap<>();
|
||
|
map.put("result", InterProviderFactory.getProvider().getLocText("Fine-Engine_Error_Page_Result"));
|
||
|
map.put("reason", reason);
|
||
|
map.put("solution", InterProviderFactory.getProvider().getLocText("Fine-Engine_Please_Contact_Platform_Admin"));
|
||
|
String page = WebServiceUtils.parseWebPageResourceSafe("com/fr/web/controller/decision/entrance/resources/unavailable.html", map);
|
||
|
printWriter.write(page);
|
||
|
printWriter.flush();
|
||
|
printWriter.close();
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String getUrl(HttpServletRequest req) {
|
||
|
String uri = req.getRequestURI();
|
||
|
String query = req.getQueryString();
|
||
|
return StringUtils.isNotBlank(query) ? uri + "?" + query : uri;
|
||
|
}
|
||
|
|
||
|
}
|