springMVC消息加密解密

2015年06月30日

springmvc消息进行全局加密和解密

springmvc消息进行全局加密和解密

protected  Map<String, Object> beforeAtMethod(HttpServletRequest request, Map<String, Object> resultMap) {
		Map<String, Object> acceptMap = null;
		Map<String, Object> hashMap = new HashMap<String, Object>();
		//把收到的参数放到hashMap中,转给解密的service处理,返回解密后的参数即明文
		try {
			acceptMap = setStreamToMap(request);
			//prepare for input parameter validation
			String requestURL = request.getRequestURI();
			String contextPath = request.getContextPath();
			//dEncrypt
			SysPrintln.errorPrintln("解密中.......................");
			hashMap=encryptService.DeEncrypt(acceptMap);
			//doSomething
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(hashMap == null){
			hashMap = this.createAcceptMap();
		}
		return hashMap;
	}

子类调用

	@RequestMapping("/getTotalByMonth")
	@ResponseBody
	public Map<String, Object> getTotalByMonth(HttpServletRequest request) {
		Map<String, Object> resultMap = createResultMap();
		Map<String, Object> acceptMap = beforeAtMethod(request, resultMap);
		Map<String, Object> resultInfoMap = new HashMap<String, Object>();
		if (resultMap.get("success") != null && !((Boolean) resultMap.get("success"))) {
			return resultMap;
		}
		//do SomeThing
		return resultMap;
	}

用到的方法

	protected Map<String, Object> setStreamToMap(HttpServletRequest request) throws IOException {
		Map<String, Object> acceptMap = null;
		if (request != null) {
			if (request.getInputStream()!=null||validationUtil.StrNotNull(request.getInputStream().toString())) {
				acceptMap = ActionUtils.getInstance().toMap(request.getInputStream());
			}
		}
		return acceptMap;
	}
	

工具类

package com.yxtar.base.server.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class ActionUtils {
	private static ActionUtils util;
	private ActionUtils() {
	}
	/**
	 * sington
	 * @return
	 */
	public static ActionUtils getInstance() {
		if (util == null) {
			synchronized (ActionUtils.class) {
				if (util == null) {
					util = new ActionUtils();
				}
			}
		}
		return util;
	}
	public String InputStream2String(InputStream in) throws IOException {
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedReader = null;
		try {
			InputStream inputStream = in;
			if (inputStream != null) {
				bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
				char[] charBuffer = new char[128];
				int bytesRead = -1;
				while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
					stringBuilder.append(charBuffer, 0, bytesRead);
				}
			} else {
				stringBuilder.append("");
			}
		} catch (IOException ex) {
			throw ex;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException ex) {
					throw ex;
				}
			}
		}
		return stringBuilder.toString();
	}
	public Map toMap(String jsonString) throws JSONException {
		JSONObject jsonObject = new JSONObject(jsonString);
		Map result = new HashMap();
		Iterator iterator = jsonObject.keys();
		String key = null;
		String value = null;
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			value = jsonObject.getString(key);
			result.put(key, value);
		}
		return result;
	}
	public Map toMap(InputStream in) {
		String jsonString = null;
		try {
			jsonString = InputStream2String(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map result = new HashMap();
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(jsonString);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Iterator iterator = jsonObject.keys();
		String key = null;
		String value = null;
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			try {
				value = jsonObject.getString(key);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			result.put(key, value);
		}
		return result;
	}
}