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.
111 lines
3.6 KiB
111 lines
3.6 KiB
package com.eco.plugin.xx.njnrfilter.filter; |
|
|
|
import com.eco.plugin.xx.njnrfilter.utils.FRUtils; |
|
import com.eco.plugin.xx.njnrfilter.utils.Utils; |
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
|
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.njnrfilter") |
|
@FunctionRecorder |
|
public class SSOFilter extends AbstractGlobalRequestFilterProvider { |
|
@Override |
|
public String filterName() { |
|
return "njnrFilter"; |
|
} |
|
|
|
@Override |
|
public String[] urlPatterns() { |
|
return new String[]{"/*"}; |
|
} |
|
|
|
@Override |
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){ |
|
|
|
if(PluginContexts.currentContext().isAvailable()){ |
|
FRUtils.FRLogInfo("url:"+FRUtils.getAllUrl(req)); |
|
if(isRelease(req)){ |
|
release(req,res,chain); |
|
return ; |
|
} |
|
|
|
//如果refer中有token |
|
String refer = req.getHeader("Referer"); |
|
FRUtils.FRLogInfo("refer:"+refer); |
|
if(refer.contains("fine_auth_token")){ |
|
String url = Utils.getRedirectUrl(req,""); |
|
FRUtils.FRLogInfo("reqUrl:"+url); |
|
//获取token |
|
String token = ""; |
|
String param = refer.split("\\?")[1]; |
|
String[] params = param.split("&"); |
|
for(int i=0;i<params.length;i++){ |
|
if(params[i].contains("fine_auth_token")){ |
|
token = params[i]; |
|
break; |
|
} |
|
} |
|
|
|
//token不为空时 |
|
if(Utils.isNotNullStr(token)){ |
|
url += url.contains("?")?"&"+token:"?"+token; |
|
try { |
|
FRUtils.FRLogInfo("跳转url:"+url); |
|
res.sendRedirect(url); |
|
} catch (IOException e) { |
|
FRUtils.FRLogInfo("跳转异常:"+e.getMessage()); |
|
} |
|
|
|
return ; |
|
} |
|
} |
|
} |
|
|
|
release(req,res,chain); |
|
} |
|
|
|
private boolean isRelease(HttpServletRequest req) { |
|
//非移动端不处理 |
|
if(!Utils.isMobile(req)){ |
|
FRUtils.FRLogInfo("不是移动端"); |
|
return true; |
|
} |
|
String url = FRUtils.getAllUrl(req); |
|
|
|
//资源文件不处理 |
|
if(url.contains("token/refresh")||url.contains("decision/file")||url.contains("decision/resource")||url.contains("decision/system")||url.contains("query/ip")){ |
|
FRUtils.FRLogInfo("资源文件"); |
|
return true; |
|
} |
|
|
|
//cookie,header,req中有token不处理 |
|
String cookieToken = Utils.getCookieByKey(req,"fine_auth_token"); |
|
String headerToken = req.getHeader("fine_auth_token"); |
|
String reqToken = req.getParameter("fine_auth_token"); |
|
|
|
if(Utils.isNotNullStr(cookieToken) || Utils.isNotNullStr(headerToken) || Utils.isNotNullStr(reqToken)){ |
|
FRUtils.FRLogInfo("有token"); |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
//放行拦截器 |
|
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) { |
|
try{ |
|
chain.doFilter(req,res); |
|
}catch (Exception e){ |
|
FRUtils.FRLogInfo("拦截失败"); |
|
} |
|
} |
|
} |
|
|
|
|