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.
155 lines
4.8 KiB
155 lines
4.8 KiB
package com.fr.plugin.sso.filter; |
|
|
|
import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider; |
|
import com.fr.decision.webservice.bean.user.UserBean; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.sso.utils.*; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import javax.servlet.http.HttpSession; |
|
import java.io.IOException; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import java.util.Properties; |
|
|
|
@FunctionRecorder |
|
public class SSOFilter extends AbstractEmbedRequestFilterProvider { |
|
|
|
@Override |
|
public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { |
|
boolean isLogin = FRUtils.isLogin(httpServletRequest); |
|
String url = FRUtils.getAllUrl(httpServletRequest); |
|
|
|
Properties p = PropertiesUtils.getProperties2("/resources/wz.properties"); |
|
|
|
//如果已经登录则放行 |
|
if(isLogin){ |
|
return ; |
|
} |
|
|
|
//如果是自带登录页资源则放行 |
|
if(url.contains("login")||url.contains("decision/file")||url.contains("decision/resource")||url.contains("decision/system")||url.contains("query/ip")){ |
|
return; |
|
} |
|
|
|
//如果是远程设计则放行 |
|
if(url.contains("remote")){ |
|
return; |
|
} |
|
|
|
String token = ""; |
|
String mscid = ""; |
|
|
|
String requestUrl = url; |
|
// String mscid = httpServletRequest.getHeader("ms_cid"); |
|
String cookie = httpServletRequest.getHeader("Cookie"); |
|
// String cookie1 = httpServletRequest.getCookies().toString(); |
|
|
|
if(Utils.isNullStr(cookie)){ |
|
return; |
|
} |
|
|
|
String[] cookies = cookie.split(";"); |
|
|
|
for(String ck : cookies){ |
|
String[] cks = ck.split("="); |
|
if(cks[0].equals(" ms_member_token") ){ |
|
token = cks[1]; |
|
} |
|
|
|
if( cks[0].equals(" ms_cid")){ |
|
mscid = cks[1]; |
|
} |
|
} |
|
|
|
FRUtils.FRLogInfo("cookie="+cookie); |
|
|
|
FRUtils.FRLogInfo("token="+token+"&requestUrl="+requestUrl+"&mscid="+mscid); |
|
|
|
//跳转首页 配置文件中取 |
|
String index = p.getProperty("nullTokenRedirectURL"); |
|
|
|
if(Utils.isNullStr(token)){ |
|
|
|
try { |
|
httpServletResponse.sendRedirect(index); |
|
} catch (IOException e) { |
|
FRUtils.FRLogInfo("重定向异常:"+e.getMessage()); |
|
} |
|
return ; |
|
} |
|
|
|
//认证地址 配置文件中取 |
|
String authUrl = p.getProperty("authUrl");; |
|
authUrl += "?requestUrl="+requestUrl; |
|
|
|
Map<String,String> header = new HashMap<String,String>(); |
|
|
|
header.put("Mscid",mscid); |
|
header.put("Authorization",token); |
|
|
|
String returnResult = HttpUtils.get(authUrl,null,header); |
|
|
|
if(Utils.isNullStr(returnResult)){ |
|
ResponseUtils.failedResponse(httpServletResponse,"获取用户信息失败,请联系管理员!"); |
|
return; |
|
} |
|
|
|
JSONObject json = new JSONObject(returnResult); |
|
|
|
String result = json.getString("result"); |
|
|
|
if("801".equals(result)){ |
|
String noPermission = p.getProperty("noAccessPermissionURL"); |
|
try { |
|
httpServletResponse.sendRedirect(noPermission); |
|
} catch (IOException e) { |
|
FRUtils.FRLogInfo("重定向异常:"+e.getMessage()); |
|
} |
|
|
|
return ; |
|
} |
|
|
|
if("401".equals(result)){ |
|
try { |
|
httpServletResponse.sendRedirect(index); |
|
} catch (IOException e) { |
|
FRUtils.FRLogInfo("重定向异常:"+e.getMessage()); |
|
} |
|
|
|
return ; |
|
} |
|
|
|
if("0".equals(result)){ |
|
JSONObject data =json.getJSONObject("data"); |
|
String userId = data.getString("ipTokenId"); |
|
String userId2 = data.getString("userId"); |
|
UserBean user = null; |
|
try { |
|
user = FRUserUtils.getUser(userId); |
|
} catch (Exception e) { |
|
String noUser = p.getProperty("userNotFoundURL"); |
|
try { |
|
httpServletResponse.sendRedirect(noUser); |
|
} catch (IOException ioException) { |
|
FRUtils.FRLogInfo("重定向异常:"+e.getMessage()); |
|
} |
|
|
|
return ; |
|
} |
|
|
|
HttpSession session = httpServletRequest.getSession(true); |
|
session.setAttribute("userid",userId2); |
|
session.setAttribute("mscid",mscid); |
|
|
|
FRUtils.login(httpServletRequest,httpServletResponse,user.getUsername(),""); |
|
|
|
return ; |
|
} |
|
|
|
ResponseUtils.failedResponse(httpServletResponse,"单点登录失败,请联系管理员!"); |
|
|
|
} |
|
} |