Browse Source

自动添加token到请求的session/跨域请求允许携带自定义的请求头

zhuth 7 years ago
parent
commit
8f61b5de24

+ 72 - 4
src/main/java/com/uas/eis/controller/HelloWorldController.java

@@ -1,10 +1,14 @@
 package com.uas.eis.controller;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.http.Header;
+import org.apache.http.HeaderElement;
+import org.apache.http.ParseException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,6 +18,8 @@ import com.uas.eis.service.UserService;
 import com.uas.eis.utils.HttpUtil;
 import com.uas.eis.utils.HttpUtil.Response;
 
+import net.sf.json.JSONObject;
+
 @RestController
 public class HelloWorldController {
 
@@ -33,9 +39,15 @@ public class HelloWorldController {
 	/**
 	 * 首次登陆请求token
 	 */
-	@RequestMapping("/login")
-	public String login(String username, String password){
-		return "<pre style=\"width:50%;font-family: mic;white-space: pre-wrap;word-wrap: break-word;\">"+userService.login(username, password)+"</pre>";
+	@RequestMapping(value = "/login", produces = "application/json;charset=utf-8")
+	public String login(HttpServletRequest request, String username, String password){
+		String token = userService.login(username, password);
+		request.getSession().removeAttribute("token");
+		if(token != null) {
+			request.getSession().setAttribute("token", token);
+			return "登录成功";
+		}
+		return "账号有误";
 	}
 	
 	/**
@@ -53,7 +65,63 @@ public class HelloWorldController {
 	@RequestMapping(value = "/baidu", produces = "text/html;charset=utf-8")
 	public String testHttpRequest() {
 		try {
-			return HttpUtil.sendGetRequest("https://www.baidu.com/", null).getResponseText();
+			return HttpUtil.sendGetRequest("https://www.baidu.com/", null, null).getResponseText();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	/**
+	 * http请求测试
+	 * @return 
+	 */
+	@RequestMapping(value = "/requestUas", produces = "text/json;charset=utf-8")
+	public String testHttpRequest(String url, String params) {
+		try {
+			JSONObject jb = JSONObject.fromObject(params);
+			Map<String, String> p = jb;
+			for (Map.Entry<String, String> entry : p.entrySet()) {
+				System.out.println(entry.getKey()+": "+entry.getValue());
+			}
+			return HttpUtil.sendGetRequest("http://192.168.253.38:8098/ERP/"+url, null, p).getResponseText();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	@RequestMapping(value = "/getInfo", produces = "text/json;charset=utf-8")
+	public String getInfo(String url, Map<String, String> params) {
+		try {
+			Map<String, String> p = new HashMap<String, String>();
+			p.put("parentId", "0");
+			HashMap<String, String> header = new HashMap<String, String>();
+			header.put("Cookie", " JSESSIONID=B8DA7A516B9ABCE045B3BFC28E7817DA;");
+			return HttpUtil.sendGetRequest("http://192.168.253.38:8098/ERP/common/lazyTree.action", header, p).getResponseText();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+	@RequestMapping(value = "/postInfo", produces = "text/json;charset=utf-8")
+	public String postInfo(String url, Map<String, String> params) {
+		try {
+			Map<String, String> p = new HashMap<String, String>();
+			p.put("caller", "WorkDaily");
+			p.put("page", "1");
+			p.put("pageSize", "17");
+			p.put("fromHeader", "false");
+			p.put("condition", "");
+			p.put("_noc", "");
+			p.put("_f", "");
+			p.put("orderby", "");
+			p.put("_self", "");
+			p.put("_config", "");
+			HashMap<String, String> header = new HashMap<String, String>();
+			header.put("Cookie", "JSESSIONID=B8DA7A516B9ABCE045B3BFC28E7817DA;");
+			return HttpUtil.sendPostRequest("http://192.168.253.38:8098/ERP/common/datalist.action", header, p).getResponseText();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}

+ 1 - 1
src/main/java/com/uas/eis/core/support/InterceptorConfig.java

@@ -21,7 +21,7 @@ public class InterceptorConfig implements HandlerInterceptor{
 	
 	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 		//token认证
-		String token = request.getParameter("token");
+		String token = (String) request.getSession().getAttribute("token");
 		boolean flag = false;
 		String message = new String("程序错误");
 		if(token != null && !token.isEmpty()) { // 验证是否包含token

+ 6 - 1
src/main/java/com/uas/eis/serviceImpl/UserServiceImpl.java

@@ -20,8 +20,13 @@ public class UserServiceImpl implements UserService {
 	private BaseDao baseDao;
 	
 	@Override
+	@Cacheable(value="loginCache")
 	public String login(String username, String password) {
-		return TokenHandler.createToken(username, password);
+		if(checkUser(username, password)) {
+			return TokenHandler.createToken(username, password);
+		}else {
+			return null;
+		}
 	}
 	
 	@Override

+ 61 - 31
src/main/java/com/uas/eis/utils/HttpUtil.java

@@ -16,6 +16,8 @@ import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.CertificateException;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -26,6 +28,7 @@ import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
 
 import org.apache.http.Consts;
+import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
@@ -110,8 +113,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendGetRequest(String url, Map<String, String> params) throws Exception {
-		return sendGetRequest(url, params, false, null);
+	public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
+		return sendGetRequest(url, header, params, false, null);
 	}
 
 	/**
@@ -124,8 +127,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendGetRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
-		return sendRequest(RequestMethod.GET, url, params, sign, signKey);
+	public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
+		return sendRequest(RequestMethod.GET, url, header, params, sign, signKey);
 	}
 
 	/**
@@ -138,8 +141,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendGetRequest(String url, Map<String, String> params, boolean sign) throws Exception {
-		return sendRequest(RequestMethod.GET, url, params, sign, null);
+	public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
+		return sendRequest(RequestMethod.GET, url, header, params, sign, null);
 	}
 
 	/**
@@ -150,8 +153,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendPostRequest(String url, Map<String, String> params) throws Exception {
-		return sendPostRequest(url, params, false, null);
+	public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
+		return sendPostRequest(url, header, params, false, null);
 	}
 
 	/**
@@ -176,8 +179,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendPostRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
-		return sendRequest(RequestMethod.POST, url, params, sign, signKey);
+	public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
+		return sendRequest(RequestMethod.POST, url, header, params, sign, signKey);
 	}
 
 	/**
@@ -208,8 +211,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendPostRequest(String url, Map<String, String> params, boolean sign) throws Exception {
-		return sendRequest(RequestMethod.POST, url, params, sign, null);
+	public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
+		return sendRequest(RequestMethod.POST, url, header, params, sign, null);
 	}
 
 	/**
@@ -350,8 +353,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendPutRequest(String url, Map<String, String> params, boolean sign) throws Exception {
-		return sendRequest(RequestMethod.PUT, url, params, sign, null);
+	public static Response sendPutRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
+		return sendRequest(RequestMethod.PUT, url, header, params, sign, null);
 	}
 
 	/**
@@ -389,8 +392,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendDeleteRequest(String url, Map<String, String> params) throws Exception {
-		return sendDeleteRequest(url, params, false, null);
+	public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
+		return sendDeleteRequest(url, header, params, false, null);
 	}
 
 	/**
@@ -403,8 +406,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendDeleteRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
-		return sendRequest(RequestMethod.DELETE, url, params, sign, signKey);
+	public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
+		return sendRequest(RequestMethod.DELETE, url, header, params, sign, signKey);
 	}
 
 	/**
@@ -417,8 +420,8 @@ public class HttpUtil {
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendDeleteRequest(String url, Map<String, String> params, boolean sign) throws Exception {
-		return sendRequest(RequestMethod.DELETE, url, params, sign, null);
+	public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
+		return sendRequest(RequestMethod.DELETE, url, header, params, sign, null);
 	}
 
 	/**
@@ -430,24 +433,51 @@ public class HttpUtil {
 	 *            请求链接
 	 * @param params
 	 *            参数
+	 *            
 	 * @param sign
 	 *            是否签名
 	 * @return
 	 * @throws Exception
 	 */
-	public static Response sendRequest(RequestMethod method, String url, Map<String, String> params, boolean sign, String signKey)
+	public static Response sendRequest(RequestMethod method, String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey)
 			throws Exception {
+		
 		switch (method) {
-		case GET:
-			return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign, signKey)));
-		case POST:
-			return sendHttpEntityEnclosingRequest(new HttpPost(getRequestUrl(url, sign, signKey)), params);
-		case PUT:
-			return sendHttpEntityEnclosingRequest(new HttpPut(getRequestUrl(url, sign, signKey)), params);
-		case DELETE:
-			return sendHttpUriRequest(new HttpDelete(getRequestUrl(url, params, sign, signKey)));
-		default:
-			return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign, signKey)));
+		case GET: {
+			HttpRequestBase request = new HttpGet(getRequestUrl(url, params, sign, signKey));
+			for (Map.Entry<String, String> entry : header.entrySet()) {
+				request.setHeader(entry.getKey(), entry.getValue());
+			}
+			return sendHttpUriRequest(request);
+		}
+		case POST: {
+			HttpPost request = new HttpPost(getRequestUrl(url, sign, signKey));
+			for (Map.Entry<String, String> entry : header.entrySet()) {
+				request.setHeader(entry.getKey(), entry.getValue());
+			}
+			return sendHttpEntityEnclosingRequest(request, params);
+		}
+		case PUT: {
+			HttpPut request = new HttpPut(getRequestUrl(url, sign, signKey));
+			for (Map.Entry<String, String> entry : header.entrySet()) {
+				request.setHeader(entry.getKey(), entry.getValue());
+			}
+			return sendHttpEntityEnclosingRequest(request, params);
+		}
+		case DELETE: {
+			HttpDelete request = new HttpDelete(getRequestUrl(url, params, sign, signKey));
+			for (Map.Entry<String, String> entry : header.entrySet()) {
+				request.setHeader(entry.getKey(), entry.getValue());
+			}
+			return sendHttpUriRequest(request);
+		}
+		default: {
+			HttpGet request = new HttpGet(getRequestUrl(url, params, sign, signKey));
+			for (Map.Entry<String, String> entry : header.entrySet()) {
+				request.setHeader(entry.getKey(), entry.getValue());
+			}
+			return sendHttpUriRequest(request);
+		}
 		}
 	}