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.
174 lines
5.8 KiB
174 lines
5.8 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2018-2022
|
||
|
* Project: starterBI
|
||
|
* FileName: LoginFilter
|
||
|
* Author: xx
|
||
|
* Date: 2022/7/5 10:28
|
||
|
*/
|
||
|
package com.fr.plugin.fbpa.request;
|
||
|
|
||
|
import com.fanruan.api.i18n.I18nKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.base.ServerConfig;
|
||
|
import com.fr.data.NetworkHelper;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.decision.mobile.terminal.TerminalHandler;
|
||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants;
|
||
|
import com.fr.decision.webservice.v10.login.LoginService;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.security.JwtUtils;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.web.Device;
|
||
|
import com.fr.third.org.apache.http.NameValuePair;
|
||
|
import com.fr.third.org.apache.http.client.utils.URIBuilder;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
import edu.yale.its.tp.cas.client.filter.CASFilter;
|
||
|
import org.jasig.cas.client.validation.Assertion;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.FilterConfig;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import javax.servlet.http.HttpSession;
|
||
|
import java.net.URISyntaxException;
|
||
|
import java.util.List;
|
||
|
|
||
|
import static com.fr.plugin.fbpa.request.IgnoreFilter.IGNORE_REQUEST;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <LoginFilter>
|
||
|
*
|
||
|
* @author xx
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class LoginFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
|
||
|
/**
|
||
|
* 过滤器名称
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "D_LoginFilter";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤规则
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public String[] urlPatterns() {
|
||
|
if (PluginContexts.currentContext() == null || !PluginContexts.currentContext().isAvailable()) {
|
||
|
LogKit.error(I18nKit.getLocText("Plugin-fbpa_Licence_Expired"));
|
||
|
return new String[]{};
|
||
|
}
|
||
|
return new String[]{"/" + ServerConfig.getInstance().getServletName()};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器初始化
|
||
|
*
|
||
|
* @param filterConfig
|
||
|
*/
|
||
|
@Override
|
||
|
public void init(FilterConfig filterConfig) {
|
||
|
super.init(filterConfig);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 过滤器处理
|
||
|
*
|
||
|
* @param req
|
||
|
* @param res
|
||
|
* @param filterChain
|
||
|
*/
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
|
||
|
try {
|
||
|
LogKit.info("fbpa-LoginFilter-doFilter-url:" + WebUtils.getOriginalURL(req));
|
||
|
HttpSession session = req.getSession(true);
|
||
|
String username;
|
||
|
//获取cas传递过来的username
|
||
|
username = req.getRemoteUser();
|
||
|
// Object object = req.getSession().getAttribute("_const_cas_assertion_");
|
||
|
// if (StringUtils.isEmpty(username) && object != null) {
|
||
|
// Assertion assertion = (Assertion) object;
|
||
|
// username = assertion.getPrincipal().getName();
|
||
|
// }
|
||
|
if (StringUtils.isEmpty(username)) {
|
||
|
username = (String) session.getAttribute(CASFilter.CAS_FILTER_USER);
|
||
|
}
|
||
|
|
||
|
if (StringUtils.isEmpty(username)) {
|
||
|
LogKit.info("fbpa-LoginFilter-doFilter-username is Empty");
|
||
|
filterChain.doFilter(req, res);
|
||
|
return;
|
||
|
}
|
||
|
LogKit.info("fbpa-LoginFilter-doFilter-username:{}", username);
|
||
|
// 放行请求
|
||
|
if (StringKit.equalsIgnoreCase(username, IGNORE_REQUEST)) {
|
||
|
session.setAttribute(CASFilter.CAS_FILTER_USER, null);
|
||
|
filterChain.doFilter(req, res);
|
||
|
return;
|
||
|
}
|
||
|
// 决策系统已登录
|
||
|
if (LoginService.getInstance().isLogged(req) && StringKit.equalsIgnoreCase(LoginService.getInstance().getCurrentUserNameFromRequestCookie(req), username)) {
|
||
|
filterChain.doFilter(req, res);
|
||
|
return;
|
||
|
}
|
||
|
login(req, res, session, username);
|
||
|
if (StringUtils.contains(getOriginalURL(req), "ticket")) {
|
||
|
res.sendRedirect(removeToken(req));
|
||
|
return;
|
||
|
}
|
||
|
filterChain.doFilter(req, res);
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 移除url的token参数
|
||
|
*
|
||
|
* @param request
|
||
|
* @return
|
||
|
*/
|
||
|
private String removeToken(HttpServletRequest request) throws URISyntaxException {
|
||
|
URIBuilder uriBuilder = new URIBuilder(getOriginalURL(request));
|
||
|
List<NameValuePair> params = uriBuilder.getQueryParams();
|
||
|
params.removeIf(pair -> ComparatorUtils.equals(pair.getName(), "ticket"));
|
||
|
uriBuilder.clearParameters();
|
||
|
if (!params.isEmpty()) {
|
||
|
uriBuilder.setParameters(params);
|
||
|
}
|
||
|
return uriBuilder.build().toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 得到请求url和参数
|
||
|
*
|
||
|
* @param request
|
||
|
* @return
|
||
|
*/
|
||
|
private String getOriginalURL(HttpServletRequest request) {
|
||
|
StringBuffer url = request.getRequestURL();
|
||
|
if (StringUtils.isNotBlank(request.getQueryString())) {
|
||
|
url.append("?").append(request.getQueryString());
|
||
|
}
|
||
|
return url.toString();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 后台登录方法
|
||
|
*/
|
||
|
private void login(HttpServletRequest req, HttpServletResponse res, HttpSession session, String username) throws Exception {
|
||
|
String token = LoginService.getInstance().login(req, res, username);
|
||
|
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token);
|
||
|
}
|
||
|
}
|