LAPTOP-SB56SG4Q\86185
3 years ago
13 changed files with 947 additions and 1 deletions
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-7891 |
||||
|
||||
JSD-7891 开源任务材料 |
||||
JSD-7891 开源任务材料\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<id>com.fr.plugin.sso</id> |
||||
<name><![CDATA[单点登录]]></name> |
||||
<active>yes</active> |
||||
<version>1.0.13</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>author</vendor> |
||||
<description><![CDATA[单点登录]]></description> |
||||
<change-notes><![CDATA[ |
||||
]]></change-notes> |
||||
<main-package>com.fr.plugin.sso</main-package> |
||||
|
||||
<extra-decision> |
||||
<EmbedRequestFilterProvider class="com.fr.plugin.sso.filter.SSOFilter"/> |
||||
</extra-decision> |
||||
|
||||
<function-recorder class="com.fr.plugin.sso.filter.SSOFilter"/> |
||||
</plugin> |
@ -0,0 +1,160 @@
|
||||
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"); |
||||
|
||||
String ssoOp = httpServletRequest.getParameter("ssoOp"); |
||||
boolean changer = Utils.isNotNullStr(ssoOp) && ssoOp.equals("changeUser"); |
||||
|
||||
//如果已经登录则放行
|
||||
if(isLogin && !changer){ |
||||
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; |
||||
} |
||||
|
||||
if(changer){ |
||||
FRUtils.logout(httpServletRequest,httpServletResponse); |
||||
} |
||||
|
||||
String token = ""; |
||||
String mscid = ""; |
||||
|
||||
String requestUrl = url; |
||||
String cookie = httpServletRequest.getHeader("Cookie"); |
||||
|
||||
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,"单点登录失败,请联系管理员!"); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.fr.plugin.sso.test; |
||||
|
||||
import com.fr.io.utils.ResourceIOUtils; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.FileInputStream; |
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
import java.util.Properties; |
||||
|
||||
public class Test { |
||||
public static void main(String[] args) { |
||||
Properties p = new Properties(); |
||||
// try{
|
||||
// String path = Test.class.getClass().getResource("/").getPath();//得到工程名WEB-INF/classes/路径
|
||||
// path=path.substring(1, path.indexOf("classes"));//从路径字符串中取出工程路径
|
||||
// p.load(new FileInputStream(path+"wz.properties"));
|
||||
// }catch(Exception e){
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
try{ |
||||
InputStream is = ResourceIOUtils.read("/resources/wz.properties"); |
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); |
||||
p.load(bufferedReader); |
||||
}catch(Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
System.out.println(); |
||||
} |
||||
} |
@ -0,0 +1,111 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.privilege.TransmissionTool; |
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.decision.webservice.bean.user.UserUpdateBean; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
|
||||
public class FRUserUtils { |
||||
|
||||
/** |
||||
* 获取用户Service |
||||
* @return |
||||
*/ |
||||
public static UserService getUserService(){ |
||||
return UserService.getInstance(); |
||||
} |
||||
|
||||
/** |
||||
* 添加用户 |
||||
* @param userBean |
||||
*/ |
||||
public static void addUser(UserBean userBean) throws Exception { |
||||
userBean.setPassword(TransmissionTool.defaultEncrypt(userBean.getPassword())); |
||||
getUserService().addUser(userBean); |
||||
} |
||||
|
||||
/** |
||||
* 删除用户 |
||||
* @param userBean |
||||
*/ |
||||
public static void updateUser(UserBean userBean) throws Exception { |
||||
getUserService().editUser(userBean); |
||||
} |
||||
|
||||
/** |
||||
* 删除用户 |
||||
* @param user |
||||
* @return |
||||
*/ |
||||
public static int deleteUser(User user) throws Exception { |
||||
String userId = user.getId(); |
||||
|
||||
UserUpdateBean userUpdateBean = new UserUpdateBean(); |
||||
userUpdateBean.setRemoveUserIds(new String[]{userId}); |
||||
|
||||
return getUserService().deleteUsers(userUpdateBean); |
||||
} |
||||
|
||||
/** |
||||
* 根据用户名获取用户实体 |
||||
* @param userName |
||||
* @return |
||||
*/ |
||||
public static User getUserByUserName(String userName) throws Exception { |
||||
return getUserService().getUserByUserName(userName); |
||||
} |
||||
|
||||
/** |
||||
* 根据id获取用户 |
||||
* @param id |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static UserBean getUser(String id) throws Exception { |
||||
return getUserService().getUser(id); |
||||
} |
||||
|
||||
/** |
||||
* 判断是否是管理员 |
||||
* @param userId |
||||
* @return |
||||
*/ |
||||
public static boolean isAdmin(String userId){ |
||||
return getUserService().isAdmin(userId); |
||||
} |
||||
|
||||
/** |
||||
* 禁用启用用户 |
||||
* @param userId |
||||
* @param state false 禁用 true 启用 |
||||
* @throws Exception 异常说明失败 |
||||
*/ |
||||
public static void forbidUser(String userId,boolean state) throws Exception { |
||||
getUserService().forbidUser(userId,state); |
||||
} |
||||
|
||||
/** |
||||
* 修改用户部门 |
||||
* @param departmentId |
||||
* @param postId |
||||
* @param ud |
||||
* @throws Exception |
||||
*/ |
||||
public static void updateDepartmentPostUsers(String departmentId, String postId, UserUpdateBean ud) throws Exception { |
||||
getUserService().updateDepartmentPostUsers(departmentId,postId,ud); |
||||
} |
||||
|
||||
|
||||
// /**
|
||||
// * 验证密码是否正确
|
||||
// * @param psd 明文密码
|
||||
// * @param user 根据用户名获取得用户对象
|
||||
// * @return
|
||||
// */
|
||||
// public static boolean checkPsd(String psd,User user){
|
||||
// String shaPsd = CipherUtils.jdksha256(psd);
|
||||
//
|
||||
// return shaPsd.equals(user.getPassword());
|
||||
// }
|
||||
} |
@ -0,0 +1,167 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.decision.authority.AuthorityContext; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.webservice.login.LogInOutResultInfo; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.decision.webservice.v10.login.event.LogInOutEvent; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.stable.query.restriction.RestrictionFactory; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.servlet.http.HttpSession; |
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
import java.util.Properties; |
||||
|
||||
public class FRUtils { |
||||
/** |
||||
* 判断用户是否存在 |
||||
* @param userName |
||||
* @return |
||||
*/ |
||||
public static boolean isUserExist(String userName){ |
||||
if (StringUtils.isEmpty(userName)) { |
||||
return false; |
||||
} else { |
||||
try { |
||||
List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName))); |
||||
return var1 != null && !var1.isEmpty(); |
||||
} catch (Exception var2) { |
||||
FineLoggerFactory.getLogger().error(var2.getMessage()); |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 判断是否登录FR |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
public static boolean isLogin(HttpServletRequest req){ |
||||
return LoginService.getInstance().isLogged(req); |
||||
} |
||||
|
||||
/** |
||||
* 帆软登录 |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
* @param userName |
||||
* @param url |
||||
*/ |
||||
public static void login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){ |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName); |
||||
FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); |
||||
|
||||
|
||||
//判断用户名是否为空
|
||||
if(!Utils.isNullStr(userName)){ |
||||
if(isUserExist(userName)){ |
||||
String FRToken = ""; |
||||
|
||||
try { |
||||
HttpSession session = httpServletRequest.getSession(true); |
||||
|
||||
FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName); |
||||
|
||||
httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken); |
||||
|
||||
session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken); |
||||
EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true)); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登陆成功!"); |
||||
|
||||
if(!Utils.isNullStr(url)){ |
||||
httpServletResponse.sendRedirect(url); |
||||
} |
||||
} catch (Exception e) { |
||||
ResponseUtils.failedResponse(httpServletResponse,"登录异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登录异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); |
||||
} |
||||
}else{ |
||||
FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!"); |
||||
try { |
||||
Properties p = PropertiesUtils.getProperties2("/resources/wz.properties"); |
||||
String noUser = p.getProperty("userNotFoundURL"); |
||||
httpServletResponse.sendRedirect(noUser); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:重定向异常!"); |
||||
} |
||||
return ; |
||||
} |
||||
}else{ |
||||
ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param httpServletRequest |
||||
* @param httpServletResponse |
||||
*/ |
||||
public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) |
||||
{ |
||||
if(!isLogin(httpServletRequest)){ |
||||
return ; |
||||
} |
||||
|
||||
try { |
||||
LoginService.getInstance().logout(httpServletRequest,httpServletResponse); |
||||
} catch (Exception e) { |
||||
ResponseUtils.failedResponse(httpServletResponse,"登出异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOG:登出异常,请联系管理员!"); |
||||
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 打印FR日志 |
||||
* @param message |
||||
*/ |
||||
public static void FRLogInfo(String message){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:"+message); |
||||
} |
||||
|
||||
/** |
||||
* 根据用户名获取用户信息 |
||||
* @param userName |
||||
* @return |
||||
*/ |
||||
public static User getFRUserByUserName(String userName){ |
||||
try { |
||||
return UserService.getInstance().getUserByUserName(userName); |
||||
} catch (Exception e) { |
||||
FRLogInfo("获取用户信息异常:"+e.getMessage()); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 解密FR密码 |
||||
* @param password |
||||
* @return |
||||
*/ |
||||
// public static String decryptFRPsd(String password){
|
||||
// FRLogInfo("解密密码:"+password);
|
||||
// return TransmissionTool.decrypt(password);
|
||||
// }
|
||||
|
||||
/** |
||||
* 获取带参数的访问链接 |
||||
* @return |
||||
*/ |
||||
public static String getAllUrl(HttpServletRequest httpServletRequest){ |
||||
return WebUtils.getOriginalURL(httpServletRequest); |
||||
} |
||||
} |
@ -0,0 +1,237 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.org.apache.http.HttpEntity; |
||||
import com.fr.third.org.apache.http.HttpResponse; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.NameValuePair; |
||||
import com.fr.third.org.apache.http.client.CookieStore; |
||||
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import com.fr.third.org.apache.http.client.methods.HttpGet; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import com.fr.third.org.apache.http.entity.StringEntity; |
||||
import com.fr.third.org.apache.http.impl.client.BasicCookieStore; |
||||
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.impl.cookie.BasicClientCookie; |
||||
import com.fr.third.org.apache.http.message.BasicNameValuePair; |
||||
import com.fr.third.org.apache.http.ssl.SSLContexts; |
||||
import com.fr.third.org.apache.http.ssl.TrustStrategy; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import javax.servlet.http.Cookie; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.KeyManagementException; |
||||
import java.security.KeyStoreException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
public class HttpUtils { |
||||
/** |
||||
* httpGet请求 |
||||
* @param url |
||||
* @return |
||||
*/ |
||||
public static String get(String url,Cookie[] cookies,Map<String,String> header){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url); |
||||
|
||||
//创建httpClient
|
||||
CloseableHttpClient httpclient = createHttpClient(cookies); |
||||
|
||||
HttpGet getMethod = new HttpGet(url); |
||||
FRUtils.FRLogInfo("header:"+header.toString()); |
||||
if(header != null && header.size() > 0){ |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
getMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
try { |
||||
HttpResponse response = httpclient.execute(getMethod); |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+response.getStatusLine().getStatusCode()); |
||||
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
||||
HttpEntity entity = response.getEntity(); |
||||
String returnResult = EntityUtils.toString(entity, "utf-8"); |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult); |
||||
|
||||
return returnResult; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage()); |
||||
} |
||||
|
||||
return ""; |
||||
} |
||||
|
||||
/** |
||||
* HttpPost请求 |
||||
* @param postMethod |
||||
* @return |
||||
*/ |
||||
private static String HttpPost(HttpPost postMethod){ |
||||
CloseableHttpClient httpclient = createHttpClient(null); |
||||
|
||||
try { |
||||
HttpResponse response = httpclient.execute(postMethod); |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+response.getStatusLine().getStatusCode()); |
||||
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
||||
HttpEntity entity = response.getEntity(); |
||||
String returnResult = EntityUtils.toString(entity, "utf-8"); |
||||
|
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:returnResult:"+returnResult); |
||||
|
||||
return returnResult; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage()); |
||||
} |
||||
|
||||
return ""; |
||||
} |
||||
|
||||
public static String HttpPostXML(String url, String xmlParam){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
postMethod.setHeader("Content-type", "text/html"); |
||||
HttpEntity entity2 = null; |
||||
try { |
||||
entity2 = new StringEntity(xmlParam); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:参数异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
|
||||
postMethod.setEntity(entity2); |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
public static String HttpPostJson(String url, String param,Map<String,String> header){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
postMethod.setHeader("Content-Type","application/json"); |
||||
|
||||
if(header != null && header.size() > 0){ |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
postMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
if(!Utils.isNullStr(param)){ |
||||
HttpEntity entity2 = null; |
||||
try { |
||||
entity2 = new StringEntity(param); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:参数异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
|
||||
postMethod.setEntity(entity2); |
||||
} |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
public static String HttpPostWWWForm(String url, Map<String,String> header,Map<String,String> param){ |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:url:"+url); |
||||
|
||||
HttpPost postMethod = new HttpPost(url); |
||||
|
||||
if(header != null && header.size() > 0){ |
||||
Set<String> keySet = header.keySet(); |
||||
|
||||
for(String key : keySet){ |
||||
postMethod.setHeader(key,header.get(key)); |
||||
} |
||||
} |
||||
|
||||
if(param != null && param.size() > 0){ |
||||
List<NameValuePair> params = new ArrayList<NameValuePair>(param.size()); |
||||
|
||||
for(Map.Entry<String,String> map : param.entrySet()){ |
||||
params.add(new BasicNameValuePair(map.getKey(), map.getValue())); |
||||
} |
||||
|
||||
try { |
||||
postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:异常:"+e.getMessage()); |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
return HttpPost(postMethod); |
||||
} |
||||
|
||||
private static CloseableHttpClient createHttpClient(Cookie[] cookies){ |
||||
|
||||
SSLContext sslContext = null; |
||||
try { |
||||
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { |
||||
@Override |
||||
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
||||
return true; |
||||
} |
||||
}).build(); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage()); |
||||
} catch (KeyManagementException e) { |
||||
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage()); |
||||
} catch (KeyStoreException e) { |
||||
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage()); |
||||
} |
||||
|
||||
CloseableHttpClient httpclient = null; |
||||
|
||||
if(cookies != null && cookies.length > 0){ |
||||
CookieStore cookieStore = cookieToCookieStore(cookies); |
||||
|
||||
httpclient = HttpClients.custom().setSslcontext(sslContext). |
||||
setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultCookieStore(cookieStore).build(); |
||||
} |
||||
else{ |
||||
httpclient = HttpClients.custom().setSslcontext(sslContext). |
||||
setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); |
||||
} |
||||
|
||||
return httpclient; |
||||
} |
||||
|
||||
/** |
||||
* cookies转cookieStore |
||||
* @param cookies |
||||
* @return |
||||
*/ |
||||
public static CookieStore cookieToCookieStore(Cookie[] cookies){ |
||||
CookieStore cookieStore = new BasicCookieStore(); |
||||
|
||||
if(cookies != null && cookies.length>0){ |
||||
for(Cookie cookie : cookies){ |
||||
BasicClientCookie cookie1 = new BasicClientCookie(cookie.getName(), cookie.getValue()); |
||||
cookieStore.addCookie(cookie1); |
||||
} |
||||
} |
||||
|
||||
return cookieStore; |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.io.utils.ResourceIOUtils; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
import java.util.Properties; |
||||
import java.util.ResourceBundle; |
||||
|
||||
public class PropertiesUtils { |
||||
// private static final String RESOURCES_PATH = "config";
|
||||
// private static ResourceBundle bundle = null;
|
||||
//
|
||||
// static {
|
||||
// bundle = ResourceBundle.getBundle(RESOURCES_PATH);
|
||||
// }
|
||||
//
|
||||
// public static String getProperties(String key){
|
||||
// return bundle.getString(key);
|
||||
// }
|
||||
|
||||
/** |
||||
* 获取web-info下的配置文件 |
||||
* @param path |
||||
* @return |
||||
*/ |
||||
public static Properties getProperties2(String path){ |
||||
Properties p = new Properties(); |
||||
|
||||
try{ |
||||
InputStream is = ResourceIOUtils.read("/resources/wz.properties"); |
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); |
||||
p.load(bufferedReader); |
||||
}catch(Exception e){ |
||||
FRUtils.FRLogInfo("获取配置文件异常"); |
||||
} |
||||
|
||||
return p; |
||||
} |
||||
} |
@ -0,0 +1,80 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.PrintWriter; |
||||
|
||||
public class ResponseUtils { |
||||
private static final int SUCCESS = 200; |
||||
private static final int FAILED = -1; |
||||
|
||||
public static void successResponse(HttpServletResponse res, String body) { |
||||
response(res, body, SUCCESS); |
||||
} |
||||
|
||||
public static void failedResponse(HttpServletResponse res, String body) { |
||||
response(res, body, FAILED); |
||||
} |
||||
|
||||
private static void response(HttpServletResponse res, String body, int code) { |
||||
JSONObject object = new JSONObject(); |
||||
PrintWriter pw; |
||||
try { |
||||
object.put("code", code); |
||||
object.put("data", body); |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("application/json;charset=utf-8"); |
||||
String result = object.toString(); |
||||
pw.println(result); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void response(HttpServletResponse res,JSONObject json){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("application/json;charset=utf-8"); |
||||
String result = json.toString(); |
||||
pw.println(result); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
|
||||
public static void setCSRFHeader(HttpServletResponse httpServletResponse){ |
||||
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); |
||||
httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH"); |
||||
httpServletResponse.setHeader("Access-Control-Max-Age", "36000"); |
||||
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization,authorization"); |
||||
} |
||||
|
||||
public static void responseJsonp(HttpServletRequest req, HttpServletResponse res, JSONObject json){ |
||||
PrintWriter pw; |
||||
try { |
||||
pw = WebUtils.createPrintWriter(res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().info(e.getMessage()); |
||||
return; |
||||
} |
||||
res.setContentType("text/javascript;charset=utf-8;charset=utf-8"); |
||||
String result = json.toString(); |
||||
|
||||
String jsonp=req.getParameter("callback"); |
||||
|
||||
pw.println(jsonp+"("+result+")"); |
||||
pw.flush(); |
||||
pw.close(); |
||||
} |
||||
} |
@ -0,0 +1,92 @@
|
||||
package com.fr.plugin.sso.utils; |
||||
|
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.CodeUtils; |
||||
import com.fr.third.org.apache.commons.codec.digest.DigestUtils; |
||||
import sun.misc.BASE64Decoder; |
||||
import sun.misc.BASE64Encoder; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.security.MessageDigest; |
||||
import java.util.UUID; |
||||
|
||||
public class Utils { |
||||
|
||||
/** |
||||
* 判断字符串是否为空 |
||||
* @param str |
||||
* @return true 空字符串 false 非空字符串 |
||||
*/ |
||||
public static boolean isNullStr(String str){ |
||||
return !(str != null && !str.isEmpty() && !"null".equals(str)); |
||||
} |
||||
|
||||
/** |
||||
* 判断字符串是否非空 |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static boolean isNotNullStr(String str){ |
||||
return !isNullStr(str); |
||||
} |
||||
|
||||
/** |
||||
* MD5加密 |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String getMd5Str(String str) |
||||
{ |
||||
return DigestUtils.md5Hex(str); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取完整的访问路径 |
||||
*/ |
||||
public static String getAllUrl(HttpServletRequest req, String queryStr){ |
||||
String url = req.getRequestURL().toString(); |
||||
|
||||
if(isNullStr(queryStr)){ |
||||
return url; |
||||
} |
||||
|
||||
return url+"?"+queryStr; |
||||
} |
||||
|
||||
/** |
||||
* 帆软shaEncode加密 |
||||
*/ |
||||
|
||||
public static String shaEncode(String str){ |
||||
return CodeUtils.sha256Encode(str); |
||||
} |
||||
|
||||
/** |
||||
* 获取uuid |
||||
*/ |
||||
public static String uuid(){ |
||||
return UUID.randomUUID().toString(); |
||||
} |
||||
|
||||
public static JSONObject getRequestBody(HttpServletRequest req){ |
||||
StringBuffer sb = new StringBuffer(); |
||||
String line = null; |
||||
try { |
||||
BufferedReader reader = req.getReader(); |
||||
while ((line = reader.readLine()) != null) |
||||
sb.append(line); |
||||
} catch (Exception e) { |
||||
FRUtils.FRLogInfo("getRequestBody:exception:"+e.getMessage()); |
||||
} |
||||
//将空格和换行符替换掉避免使用反序列化工具解析对象时失败
|
||||
String jsonString = sb.toString().replaceAll("\\s","").replaceAll("\n",""); |
||||
|
||||
JSONObject json = new JSONObject(jsonString); |
||||
|
||||
return json; |
||||
} |
||||
} |
@ -0,0 +1,4 @@
|
||||
nullTokenRedirectURL=xxx |
||||
userNotFoundURL=xxx |
||||
noAccessPermissionURL=xxx |
||||
frDomain=localhost:8075 |
Binary file not shown.
Loading…
Reference in new issue