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.
110 lines
4.1 KiB
110 lines
4.1 KiB
3 years ago
|
package com.fr.plugin.xxxx.cqjg.jump;
|
||
|
|
||
|
import com.fr.cert.token.Claims;
|
||
|
import com.fr.cert.token.Jwts;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
|
||
|
import com.fr.decision.webservice.exception.user.UserNotExistException;
|
||
|
import com.fr.decision.webservice.utils.DecisionServiceConstants;
|
||
|
import com.fr.decision.webservice.v10.config.ConfigService;
|
||
|
import com.fr.decision.webservice.v10.login.LoginService;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.intelli.record.Focus;
|
||
|
import com.fr.intelli.record.Original;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.record.analyzer.EnableMetrics;
|
||
|
import com.fr.security.KeySecretSeedConfig;
|
||
|
import com.fr.security.SecurityToolbox;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.fun.Authorize;
|
||
|
|
||
|
import javax.servlet.FilterChain;
|
||
|
import javax.servlet.ServletException;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
/**
|
||
|
* @Author fr.open
|
||
|
* @Date 2021/10/29
|
||
|
* @Description
|
||
|
**/
|
||
|
@FunctionRecorder
|
||
|
@Authorize(callSignKey = Constants.PLUGIN_ID)
|
||
|
@EnableMetrics
|
||
|
public class GlobalFilter extends AbstractGlobalRequestFilterProvider {
|
||
|
|
||
|
private static final String flag = "00000";
|
||
|
|
||
|
@Override
|
||
|
public String filterName() {
|
||
|
return "global";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
@Focus(id = Constants.PLUGIN_ID, text = "重庆建工集团跳转", source = Original.PLUGIN)
|
||
|
public String[] urlPatterns() {
|
||
|
if (PluginContexts.currentContext().isAvailable()) {
|
||
|
String servletPathName = "decision";
|
||
|
try {
|
||
|
servletPathName = ConfigService.getInstance().getBasicParam().getServletPathName();
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
return new String[]{
|
||
|
"/" + servletPathName,
|
||
|
"/" + servletPathName + "/view/report",
|
||
|
|
||
|
};
|
||
|
} else {
|
||
|
return new String[0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
|
||
|
String query = req.getQueryString();
|
||
|
if (StringUtils.isNotBlank(query) && query.contains(flag)) {
|
||
|
int start = query.indexOf(flag) + 5;
|
||
|
int end = query.indexOf("&", start);
|
||
|
String token = query.substring(start, end == -1 ? query.length() : end);
|
||
|
FineLoggerFactory.getLogger().info("get token is {}", token);
|
||
|
try {
|
||
|
byte[] bytes = SecurityToolbox.base642Byte(KeySecretSeedConfig.getInstance().getTrustSeed());
|
||
|
Claims claims = Jwts.parser().setSigningKey(bytes).parseClaimsJws(token).getBody();
|
||
|
loginFromToken(req,res,claims.getSubject());
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
try {
|
||
|
filterChain.doFilter(req, res);
|
||
|
} catch (IOException e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage());
|
||
|
} catch (ServletException e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private boolean loginFromToken(HttpServletRequest req, HttpServletResponse res, String username) throws Exception {
|
||
|
try {
|
||
|
if (StringUtils.isNotEmpty(username)) {
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user == null) {
|
||
|
throw new UserNotExistException();
|
||
|
}
|
||
|
String token = LoginService.getInstance().login(req, res, username);
|
||
|
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, token);
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|