package com.infosec.sso.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import org.openid4java.message.ParameterList;
import org.springframework.util.StringUtils; import com.alibaba.fastjson.JSON;
import com.infosec.sso.authentication.IDInfo;
import com.infosec.sso.ssoprotocol.openid.util.OpenIdProviderService;
import com.netauth.utils.currentuser.LoginUserUtil; /**
*
* <p>
* {输出JSON}
* </p>
*
* @author: jlcui
* @date: 2019年9月21日下午5:17:41
*
*/
public class ResponseUtil { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ResponseUtil.class); /**
* 使用response输出JSON
* @param response
* @param resultMap
*/
public static void out(HttpServletResponse response, Map<String, Object> resultMap){ ServletOutputStream out = null;
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
out = response.getOutputStream();
out.write(JSON.toJSON(resultMap).toString().getBytes());
} catch (Exception e) {
e.printStackTrace();
log.error(e + "输出JSON出错");
} finally{
if(out!=null){
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static Map<String, Object> resultMap(boolean flag, Integer code, String msg){ return resultMap(flag, code, msg, null,null,false);
} public static Map<String, Object> resultMap(boolean flag, Integer code, String msg, String redirectUrl,Object data,boolean resetpwd){ Map<String, Object> resultMap = new HashMap<String, Object>(16);
resultMap.put("success", flag);
resultMap.put("message", msg);
resultMap.put("code", code);
resultMap.put("timestamp", System.currentTimeMillis());
resultMap.put("redirectUrl",redirectUrl);
resultMap.put(LoginUserUtil.USER_RESER_PWD,resetpwd);
if(data!=null){
resultMap.put("result", data);
}
return resultMap;
} public static Map<String, Object> resultMap(Integer code, String msg){ Map<String, Object> resultMap = new HashMap<String, Object>(16);
resultMap.put("msg", msg);
resultMap.put("ret", code);
resultMap.put("timestamp", System.currentTimeMillis());
return resultMap;
} public static Map<String, Object> resultMap(boolean flag, String code, String msg){ Map<String, Object> resultMap = new HashMap<String, Object>(16);
resultMap.put("success", flag);
resultMap.put("message", msg);
resultMap.put("code", code);
resultMap.put("timestamp", System.currentTimeMillis());
return resultMap;
}
public static Map<String, Object> resultMap(boolean flag, Integer code, String msg, String redirectUrl,Object data,boolean resetpwd,String token){ Map<String, Object> resultMap = new HashMap<String, Object>(16);
resultMap.put("success", flag);
resultMap.put("message", msg);
resultMap.put("code", code);
resultMap.put("timestamp", System.currentTimeMillis());
resultMap.put("redirectUrl",redirectUrl);
resultMap.put("token", token);
resultMap.put(LoginUserUtil.USER_RESER_PWD,resetpwd);
if(data!=null){
resultMap.put("result", data);
}
return resultMap;
} public static String isOpenid(String redirectUrl,String ssoHost,IDInfo inInfo,String tgc){
if(!StringUtils.isEmpty(redirectUrl) && redirectUrl.contains("openid.ns")) {
String[] requestParam = redirectUrl.split("&");
Map<String,String> parameters = new HashMap<String, String>();
for(String param : requestParam){
Integer arr = param.indexOf("=");
if(arr>0) {
parameters.put(param.substring(0, arr), param.substring(arr+1, param.length()));
}
}
ParameterList requestParameters = new ParameterList(parameters);
redirectUrl = new OpenIdProviderService().sendSuccessfulResponse(
requestParameters,
inInfo,
ssoHost+"/ssoserver/openid/getopenid",
tgc,
ssoHost);
}
return redirectUrl;
} public static Map<String, Object> resultJwtMap(String token, String username,String msg,String stat) { Map<String, Object> resultMap = new HashMap<String, Object>(16);
if (!StringUtils.isEmpty(token)) {
resultMap.put("token", token);
}
if (!StringUtils.isEmpty(username)) {
resultMap.put("username", username);
}
resultMap.put("message", msg);
resultMap.put("stat", stat);
return resultMap;
} /**
* <p>
* Description: 手机oauth认证,信息返回值构造
* </p>
* @param success
* @param code
* @param msg
* @return
*
* @author Tianzy
*
* 2019年10月24日 上午11:28:08
*
*/
public static Map<String, Object> buildResultByMobileOauth(boolean success, String code,String msg) { Map<String, Object> resultMap = new HashMap<String, Object>(16);
if(success) {
resultMap.put("ret", 0) ;
}else {
resultMap.put("ret", 1) ;
}
resultMap.put("code", code) ;
resultMap.put("msg", msg) ; return resultMap;
} }

最新文章

  1. 三周,用长轮询实现Chat并迁移到Azure测试
  2. Java 读写XML
  3. Java基础语法总结2
  4. 建模算法(一)&mdash;&mdash;线性规划
  5. Class.forName(&quot;ClassName&quot;)与ClassName.class的区别
  6. C++ 遇见的一些函数
  7. URAL 2034 : Caravans
  8. SQL Server T-SQL基础
  9. oracle --- spoon
  10. 使用Fiddler伪造服务端返回数据,绕过软件试用期验证
  11. StreamReader 读取文本文件乱码问题
  12. 蓝桥杯-隔行变色-java
  13. WPF使用RoutedCommand自己定义命令
  14. shell脚本启动语法错误syntax error near unexpected token &#39;{
  15. Strut2页面传参跳转 --Struts2
  16. 前端vue框架 脚手架
  17. Python3.X 安装Scrapy
  18. opencv 基本使用
  19. Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析
  20. Java 跨域 CrossOrigin注解 Filter拦截 Nginx配置

热门文章

  1. 西湖论剑2023-mp3[wp]
  2. Mac OS X 下安装Tableau Desktop Pro for Mac 10.2.0
  3. redis底层数据结构之简单动态字符串(SDS)
  4. 【SQL SERVER】DATEDIFF() :两个日期的日期差
  5. 《CSOL大灾变》Mobile开发进度记录——扔掉与拾取武器的逻辑
  6. css节流
  7. 无感刷新 Token
  8. 微信小程序图片和签名
  9. (0720) 【 表示 n&#39;b0; 】
  10. java.sql.SQLIntegrityConstraintViolationException: Duplicate entry &#39;1&#39; for key &#39;PRIMARY&#39;