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.

118 lines
3.6 KiB

package com.eco.plugin.xx.tgjtsso.filter;
import com.eco.plugin.xx.tgjtsso.config.PluginSimpleConfig;
import com.eco.plugin.xx.tgjtsso.utils.FRUtils;
import com.eco.plugin.xx.tgjtsso.utils.HttpUtils;
import com.eco.plugin.xx.tgjtsso.utils.Utils;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.json.JSONObject;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.stable.fun.Authorize;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@EnableMetrics
@Authorize(callSignKey = "com.eco.plugin.xx.tgjtsso")
@FunctionRecorder
public class SSOFilter extends AbstractGlobalRequestFilterProvider {
@Override
public String filterName() {
return "tgjtssoFilter";
}
@Override
public String[] urlPatterns() {
return new String[]{"/*"};
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){
if(PluginContexts.currentContext().isAvailable()){
//是否放行
boolean release = isRelease(req);
if(release){
release(req,res,chain);
return;
}
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
//code
String code = req.getParameter("ticket");
if(Utils.isNullStr(code)){
//跳转认证中心
redirect(req,res,psc);
return;
}
//获取userInfo
String username = getUsername(code,psc);
String url = Utils.removeParam(FRUtils.getAllUrl(req),"ticket");
//登录
FRUtils.login(req,res,username,url);
}
release(req,res,chain);
}
private boolean isRelease(HttpServletRequest req) {
String url = FRUtils.getAllUrl(req);
FRUtils.FRLogInfo("requestUrl:"+url);
String reft = req.getParameter("ref_t");
boolean isLogin = FRUtils.isLogin(req);
boolean isRemote = url.contains("remote");
boolean isLoginPage = url.contains("login")||url.contains("decision/file")||url.contains("decision/resource")||url.contains("decision/system")||url.contains("query/ip");
boolean isViewlt = "design".equals(reft);
return isLogin || isRemote || isLoginPage || isViewlt;
}
//跳转认证中心
private void redirect(HttpServletRequest req,HttpServletResponse res, PluginSimpleConfig psc) {
String authurl = psc.getAuthurl()+"?service="+FRUtils.getAllUrl(req);
try {
res.sendRedirect(authurl);
} catch (IOException e) {
FRUtils.FRLogInfo("跳转认证中心异常:"+e.getMessage());
}
}
//获取用户名
private String getUsername(String openId,PluginSimpleConfig psc) {
String tokenurl = psc.getUserurl()+"?ticket="+openId;
String result = HttpUtils.httpGet(tokenurl,null,null);
if(Utils.isNullStr(result)){
return "";
}
JSONObject json = new JSONObject(result);
if(json == null ){
return "";
}
String username = json.getJSONObject("user").getString("username");
return username;
}
//放行拦截器
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) {
try{
chain.doFilter(req,res);
}catch (Exception e){
FRUtils.FRLogInfo("拦截失败");
}
}
}