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.
170 lines
5.5 KiB
170 lines
5.5 KiB
package com.fr.plugin.gzwauth.filter; |
|
|
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.context.PluginContexts; |
|
import com.fr.plugin.gzwauth.config.PluginSimpleConfig; |
|
import com.fr.plugin.gzwauth.utils.FRUtils; |
|
import com.fr.plugin.gzwauth.utils.TokenUtil; |
|
import com.fr.plugin.gzwauth.utils.Utils; |
|
import com.fr.record.analyzer.EnableMetrics; |
|
import com.fr.security.JwtUtils; |
|
import com.fr.stable.fun.Authorize; |
|
|
|
import javax.servlet.FilterChain; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.BufferedReader; |
|
import java.io.IOException; |
|
import java.io.InputStreamReader; |
|
import java.io.PrintWriter; |
|
import java.net.URL; |
|
import java.net.URLConnection; |
|
|
|
@EnableMetrics |
|
@Authorize(callSignKey = "com.fr.plugin.gzwauth") |
|
public class SSOFilter extends AbstractGlobalRequestFilterProvider { |
|
@Override |
|
public String filterName() { |
|
return "gzwssoFilter"; |
|
} |
|
|
|
@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 sysId = req.getParameter("sysId"); |
|
String jwtToken = req.getParameter("jwtToken"); |
|
|
|
if(Utils.isNullStr(sysId) || Utils.isNullStr(jwtToken)){ |
|
release(req,res,chain); |
|
return; |
|
} |
|
|
|
FRUtils.FRLogInfo("sysId:"+sysId+";token:"+jwtToken); |
|
//访问接口 |
|
boolean success = checkToken(sysId,jwtToken, psc); |
|
|
|
//判断 |
|
if (!success) { |
|
Utils.toErrorPage(res, "/com/fr/plugin/gzwauth/error.html", null); |
|
} |
|
} |
|
release(req,res,chain); |
|
} |
|
|
|
private boolean checkToken(String sysid,String token, PluginSimpleConfig psc) { |
|
String url = psc.getCheckTokenUrl(); |
|
String result = sendGETSync(sysid,token,url); |
|
|
|
if(Utils.isNullStr(result)){ |
|
return true; |
|
} |
|
|
|
JSONObject json = new JSONObject(result); |
|
String code = json.getString("code"); |
|
|
|
if(code.equals("2")){ |
|
return false; |
|
} |
|
|
|
return true; |
|
} |
|
|
|
/** |
|
* 发送请求 |
|
* @param sysId 系统id |
|
* @param jwtToken token |
|
* @param url 请求url |
|
* @return |
|
*/ |
|
private String sendGETSync(String sysId, String jwtToken, String url){ |
|
//参数拼接json |
|
String parameterJson = "{\"sysId\":\""+sysId+"\",\"jwtToken\":\""+jwtToken+"\"}"; |
|
//发送请求 |
|
String result = sendPost(url, parameterJson,jwtToken); |
|
return result; |
|
} |
|
public static String sendPost(String url, String param,String jwtToken) { |
|
PrintWriter out = null; |
|
BufferedReader in = null; |
|
String result = ""; |
|
try { |
|
URL realUrl = new URL(url); |
|
// 打开和URL之间的连接 |
|
URLConnection conn = realUrl.openConnection(); |
|
// 设置通用的请求属性 |
|
//接收数据格式 |
|
conn.setRequestProperty("Accept", "*/*"); |
|
conn.setRequestProperty("connection", "Keep-Alive"); |
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
|
//发送数据格式 |
|
conn.setRequestProperty("content-type", "application/json;charset=UTF-8"); |
|
conn.setRequestProperty("slw.jwt.token", jwtToken); |
|
// 发送POST请求必须设置如下两行 |
|
conn.setDoOutput(true); |
|
conn.setDoInput(true); |
|
// 获取URLConnection对象对应的输出流 |
|
out = new PrintWriter(conn.getOutputStream()); |
|
// 发送请求参数 |
|
out.print(param); |
|
// flush输出流的缓冲 |
|
out.flush(); |
|
// 定义BufferedReader输入流来读取URL的响应 |
|
in = new BufferedReader( |
|
new InputStreamReader(conn.getInputStream())); |
|
String line; |
|
while ((line = in.readLine()) != null) { |
|
result += line; |
|
} |
|
} catch (Exception e) { |
|
FRUtils.FRLogError("发送 POST 请求出现异常!"+e.getMessage()); |
|
return ""; |
|
} |
|
//使用finally块来关闭输出流、输入流 |
|
finally{ |
|
try{ |
|
if(out!=null){ |
|
out.close(); |
|
} |
|
if(in!=null){ |
|
in.close(); |
|
} |
|
} |
|
catch(IOException ex){ |
|
FRUtils.FRLogError("发送 POST 请求出现异常!"+ex.getMessage()); |
|
|
|
return ""; |
|
} |
|
} |
|
|
|
return result; |
|
} |
|
|
|
private boolean isRelease(HttpServletRequest req) { |
|
String url = FRUtils.getAllUrl(req); |
|
FRUtils.FRLogInfo("requestUrl:"+url); |
|
|
|
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"); |
|
return isRemote || isLoginPage ; |
|
} |
|
|
|
|
|
|
|
//放行拦截器 |
|
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) { |
|
try{ |
|
chain.doFilter(req,res); |
|
}catch (Exception e){ |
|
FRUtils.FRLogInfo("拦截失败"); |
|
} |
|
} |
|
} |
|
|
|
|