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
3.6 KiB

3 years ago
package com.eco.plugin.xx.cookiesso.filter;
import com.eco.plugin.xx.cookiesso.config.PluginSimpleConfig;
import com.eco.plugin.xx.cookiesso.utils.FRUtils;
import com.eco.plugin.xx.cookiesso.utils.ResponseUtils;
import com.eco.plugin.xx.cookiesso.utils.Utils;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.context.PluginContexts;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.stable.fun.Authorize;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@EnableMetrics
@Authorize(callSignKey = "com.eco.plugin.xx.cookiesso")
public class SSOFilter extends AbstractGlobalRequestFilterProvider {
@Override
public String filterName() {
return "fqlssoFilter";
}
@Override
public String[] urlPatterns() {
return new String[]{"/*"};
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){
if(PluginContexts.currentContext().isAvailable()){
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String fromsso = req.getParameter("fromsso");
String cookie = Utils.getCookieByKey(req,psc.getCookiename());
if(Utils.isNullStr(fromsso) || !"true".equals(fromsso) || Utils.isNullStr(cookie)){
release(req,res,chain);
return ;
}
cookie = cookie.replace("PubSingleSignOn=","");
String jmstr = null;
try {
jmstr = desEncrypt(cookie,psc);
} catch (Exception e) {
ResponseUtils.failedResponse(res,"token解密失败");
return ;
}
String username = jmstr.split("###")[0];
String url = Utils.encodeCH(Utils.removeParam(FRUtils.getAllUrl(req),"fromsso"));
FRUtils.login(req,res,username,url);
}
release(req,res,chain);
}
//放行拦截器
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) {
try{
chain.doFilter(req,res);
}catch (Exception e){
FRUtils.FRLogInfo("拦截失败");
}
}
private String desEncrypt(String token,PluginSimpleConfig psc) throws Exception {
String key = psc.getSecret().substring(0,8);
DESKeySpec dks = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(psc.getIv().getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE,secretKey,iv);
byte[] bytes = cipher.doFinal(base64DecodeB(token));
String str = new String(bytes);
return str;
}
/**
* base64解密
* @param key
* @return
*/
public static byte[] base64DecodeB(String key){
byte[] result = null;
try {
result = (new BASE64Decoder()).decodeBuffer(key);
} catch (IOException e) {
FineLoggerFactory.getLogger().info("FRLOG:BASE64解密异常:"+e.getMessage());
}
return result;
}
}