package com.uas.sso.common;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import com.uas.sso.common.encrypt.MD5;

/**
 * <p>
 * 验证浏览器基本信息
 * </p>
 * 
 */
public class BrowserHelper {

	private static final Logger logger = Logger.getLogger(BrowserHelper.class.getName());

	/**
	 * 混淆浏览器版本信息
	 * 
	 * @Description 获取浏览器客户端信息签名值
	 * @param request
	 * @return
	 */
	public static String getUserAgent(HttpServletRequest request, String value) {
		StringBuffer sf = new StringBuffer();
		sf.append(value);
		sf.append("-");
		sf.append(request.getHeader("user-agent"));

		/**
		 * MD5 浏览器版本信息
		 */
		logger.fine("Browser info:" + sf.toString());
		return MD5.toMD5(sf.toString());
	}

	/**
	 * <p>
	 * 请求浏览器是否合法 (只校验客户端信息不校验domain)
	 * </p>
	 * 
	 * @param request
	 * @param userAgent
	 *            浏览器客户端信息
	 * @return
	 */
	public static boolean isLegalUserAgent(HttpServletRequest request, String value, String userAgent) {
		String rlt = getUserAgent(request, value);

		if (rlt.equalsIgnoreCase(userAgent)) {
			logger.fine("Browser isLegalUserAgent is legal. Browser getUserAgent:" + rlt);
			return true;
		}

		logger.fine("Browser isLegalUserAgent is illegal. Browser getUserAgent:" + rlt);
		return false;
	}

}
