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.
139 lines
5.0 KiB
139 lines
5.0 KiB
package com.fr.plugin.xx.sso.util; |
|
|
|
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.v10.login.LoginService; |
|
import com.fr.decision.webservice.v10.login.TokenResource; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
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.util.Map; |
|
import java.util.Properties; |
|
|
|
/** |
|
* @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 void login(String username, HttpServletRequest request, HttpServletResponse response) { |
|
try { |
|
User user = UserService.getInstance().getUserByUserName(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); |
|
} |
|
} |
|
|
|
public static boolean checkUser(String username) { |
|
try { |
|
User user = UserService.getInstance().getUserByUserName(username); |
|
return user != null; |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
|
} |
|
return false; |
|
} |
|
|
|
|
|
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 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); |
|
} |
|
} |
|
|
|
}
|
|
|