有时会经常遇到对接口包装一层,先要对用户进行认证,调用该接口的用户是否合法,下面我对用到检查cookie信息的方法进行下梳理
public class CheckCookie {
//获取请求中的cookie信息
public static String getCookie(Request request) {
StringBuilder stringCookie = new StringBuilder("");
String stringHeader = "";
Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (Cookie cookie : cookies) {
stringCookie.append(cookie.getName().trim());
stringCookie.append("=");
stringCookie.append(cookie.getValue().trim());
stringCookie.append(";");
}
stringHeader = stringCookie.toString().substring(0, stringCookie.length() - 1);
}
return stringHeader;
}
//调用其他url,检查用户信息
public static Map<String, String> checkLogin(Request request) {
Map<String, String> loginInfo = Maps.newConcurrentMap();
try {
String cookieVal = getCookie(request); //获取cookie信息
Map<String, String> param = Maps.newConcurrentMap();
param.put("cookie", cookieVal);
param.put("redirect_uri", "回调地址url"); //这边是我自己用到的
HttpClient client = new HttpClient();
// 有效性3秒
client.setTimeout(3000);
String par = "";
StringBuilder url = new StringBuilder("要调用的url"); //可以验证用户信息的url
if (param != null && !param.isEmpty()) {
Iterator ite = param.entrySet().iterator();
while (ite.hasNext()) {
Entry en = (Entry) ite.next();
String key = en.getKey().toString();
String value = null;
if (en.getValue() != null) {
value = en.getValue().toString();
} else {
value = "";
}
if (par.trim().length() == 0) {
par = "?" + key + "=" + URLEncoder.encode(value, "UTF-8");
} else {
par = "&" + key + "=" + URLEncoder.encode(value, "UTF-8");
}
url.append(par);
}
}
HttpMethod method = new GetMethod(url.toString());
method.setRequestHeader(new Header("Cookie", getCookie(request)));
client.executeMethod(method);
InputStream inputStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
String response = stringBuffer.toString();
try{
System.out.print(response);
LoginModel login = JSONObject.parseObject(response, LoginModel.class); //LoginModel这个类是我调用url封装返回信息的一个类
if (login != null) {
loginInfo.put("code", login.getCode());
loginInfo.put("employId", login.getData().getEmployId());
loginInfo.put("employName", login.getData().getName());
} else {
loginInfo.put("code", String.valueOf(ApiMessage.FAIL_NEED_LOGIN.code));//提示需要登录的code
loginInfo.put("data", "回调地址的url");
}
}catch(Exception e){
e.printStackTrace();
loginInfo.put("code", String.valueOf(ApiMessage.FAIL_NEED_LOGIN.code));//提示需要登录的code
loginInfo.put("data", CubeApiConfig.App.callBackUrl);
}
} catch (Exception e) {
e.printStackTrace();
}
return loginInfo;
}
//另外一种方法,拿调用url中具体的数据信息
public static Map<String, Set<String>> getRoleInfoByUser(Request request) {
Map<String, Set<String>> setMap = Maps.newConcurrentMap();
try {
//获取cookie 信息
String cookieVal = getCookie(request);
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
StringBuilder url = new StringBuilder(CubeApiConfig.App.roleinfo_authority);
HttpMethod method = new GetMethod(url.toString());
method.setRequestHeader("Cookie", cookieVal);
client.executeMethod(method);
InputStream inputStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
String response = stringBuffer.toString();
long existedAuditor = 0;
Map<String, Integer> map = JSONObject.parseObject(response, Map.class);
if (map != null) {
if (map.get("code") == ApiMessage.SUCCESS.code) {
AuthorityResponseModel<Map<String, Object>> authority = JSONObject.parseObject(response, AuthorityResponseModel.class); //封装返回信息的类
Map<String, Object> data = authority.getData();
Map<String, String> regionMap = Maps.newConcurrentMap();
// 已登录,无权限访问
if (data == null) {
Set<String> idsSet = Sets.newHashSet();
idsSet.add(String.valueOf(ApiMessage.WU_QUAN_FANG_WEN.code));
setMap.put("code", idsSet);
return setMap;
}
List<Map<String, Object>> authRoleList = (List<Map<String, Object>>) data.get("authRolesList");
if(authRoleList != null) {
existedAuditor = authRoleList.stream()
.filter((mapper) -> Constants.ROLE_AUDITOR.equals(mapper.get("name"))).count()
;
}
Set<String> idsSet = Sets.newHashSet();
idsSet.add(Boolean.toString(true));
if(existedAuditor > 0){
setMap.put(Constants.ROLE_AUDITOR, idsSet);
}
} else if (map.get("code") == ApiMessage.FAIL_NEED_LOGIN.code) {
Set<String> idsSet = Sets.newHashSet();
idsSet.add(String.valueOf(ApiMessage.FAIL_NEED_LOGIN.code));
setMap.put("code", idsSet);
} else {
Set<String> idsSet = Sets.newHashSet();
idsSet.add(String.valueOf(ApiMessage.FAIL.code));
setMap.put("code", idsSet);
}
} else {
Set<String> idsSet = Sets.newHashSet();
idsSet.add(String.valueOf(ApiMessage.FAIL.code));
setMap.put("code", idsSet);
}
}catch(Exception e){
e.printStackTrace();
Set<String> idsSet = Sets.newHashSet();
idsSet.add(String.valueOf(ApiMessage.FAIL.code));
setMap.put("code", idsSet);
}
return setMap;
}
/**
*
* @param request
* @return String
*/
private static String parseHttp(Request request,String urlAuth){
// 获取cookie信息
String cookieVal = null;
String response = "";
try {
cookieVal = getCookie(request);
HttpClient client = new HttpClient();
StringBuffer url = new StringBuffer(urlAuth);
HttpMethod method = new GetMethod(url.toString());
method.setRequestHeader("Cookie",cookieVal);
client.executeMethod(method);
InputStream inputStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
response = stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}