CustomMessageInterceptor flex 防跨站点脚本 - 无私的奉献

flex开发页面很炫,但生成的页面和传递的参数容易引起跨站点脚本攻击,这里是针对性的解决方案,这里是在spring框架下,其它的情况视情况修改,但原理一样;





package com.romeo.backbone.msg;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.flex.core.MessageInterceptor;
import org.springframework.flex.core.MessageProcessingContext;

import com.romeo.backbone.untils.DangerString;

import flex.messaging.messages.Message;

/**
 * <h2>消息拦截器</h2>
 * <p>
 * 用于处理特殊逻辑在AMF传入传出java 表单的时候,用拦截器检查传入消息的内容,或者给返回信息进行额外统一的操作。
 * </p>
 * 
 * @author aGuang
 * 
 */
public class CustomMessageInterceptor implements MessageInterceptor {

	/**
	 * <h2>输入输出</h2>
	 * <p>
	 * 异常定位信息,后续扩展 , 跨站点脚本拦截在此处理 
	 * </p>
	 */
	public Message postProcess(MessageProcessingContext context,
			Message inputMessage, Message outputMessage) {

		Object msgBody = inputMessage.getBody();

		if (msgBody.getClass().isArray()) {
			Object[] params = (Object[]) msgBody;

			for (int i = 0; i < params.length; i++) {
				Object param = params[i];
				// log.info("post Process ---: " + param + " " +
				// param.getClass());
				if (typeConversion(param.getClass())) { // 如果等于自定义对象
					Object conv = convString(param);
					params[i] = conv;// 填回去
				} else if (stringConversion(param.getClass())) { // 如果等于字符类型
					String conv = DangerString.filter(param.toString());
					params[i] = conv;// 填回去
				}
			}
			inputMessage.setBody(params);
		}
		// log.info("output Body: " + outMsgBody);
		return outputMessage;
	}

	/**
	 * <p>
	 * 异常定位信息,后续扩展 , 跨站点脚本拦截在此处理 
	 * </p>
	 */
	public Message preProcess(MessageProcessingContext context,
			Message inputMessage) {
		Object msgBody = inputMessage.getBody();
		/**
		 * 反转所有参数,将字符对象的非法字符转换 反转对象参数,将对象的属性对象的非法字符转换
		 */
		if (msgBody.getClass().isArray()) {
			Object[] params = (Object[]) msgBody;
			for (int i = 0; i < params.length; i++) {
				Object param = params[i];
				if (typeConversion(param.getClass())) { // 如果等于自定义对象
					Object conv = convString(param);
					params[i] = conv;// 填回去
				} else if (stringConversion(param.getClass())) { // 如果等于字符类型
					String conv = DangerString.filter(param.toString());
					params[i] = conv;// 填回去
				}
			}
			inputMessage.setBody(params);
		}
		return inputMessage;
	}

	/** * 将对象反转并过滤掉危险字符 */
	private Object convString(Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		Field[] fields = obj.getClass().getDeclaredFields();// 获得属性
		for (Field field : fields) {
			Object type = field.getType().getSimpleName();
			if ("String".equals(type)) {
				PropertyDescriptor pd = null;
				try {
					pd = new PropertyDescriptor(field.getName(), clazz);
				} catch (IntrospectionException e) {
					pd = null;
				}
				Method getMethod = null;
				if (pd != null) {
					/** 获得get方法 */
					getMethod = pd.getReadMethod();
				}
				Object o = null;
				if (getMethod != null) {
					try {
						o = getMethod.invoke(obj);
						String value = "";// DangerString.filter(o.toString());
						field.setAccessible(true); // 设置些属性是可以访问的
						field.set(obj, value);// 给属性设值
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				/** 把值反转后重新设置进去 */
			}
		}
		return obj;
	}

	/**
	 * <h2>返回映射对象的类型</h2>
	 * <p>
	 * 如果是八党员之一,则返回flase;否则返回true;
	 * </p>
	 * 
	 * @param cls
	 * @return
	 */
	private boolean typeConversion(Class<?> cls) {
		String nameType = cls.getSimpleName();
		// log.info(nameType);
		if ("Integer".equals(nameType)) {
			return false;
		}
		if ("String".equals(nameType)) {
			return false;
		}
		if ("Float".equals(nameType)) {
			return false;
		}
		if ("Double".equals(nameType)) {
			return false;
		}
		if ("Boolean".equals(nameType)) {
			return false;
		}
		if ("Long".equals(nameType)) {
			return false;
		}
		if ("Short".equals(nameType)) {
			return false;
		}
		if ("Character".equals(nameType)) {
			return false;
		}
		return true;
	}

	/**
	 * <h2>返回映射对象的类型</h2>
	 * <p>
	 * 如果是String、Character,则返回flase;否则返回true;
	 * </p>
	 * 
	 * @param cls
	 * @return
	 */
	private boolean stringConversion(Class<?> cls) {
		String nameType = cls.getSimpleName();
		// log.info(nameType);
		if ("String".equals(nameType)) {
			return true;
		} else if ("Character".equals(nameType)) {
			return true;
		} else {
			return false;
		}
	}
}

package com.romeo.backbone.untils;

/**
 * 过滤危险的字符
 * @author aGuang
 *
 */
public class DangerString {

	static public String filter(String param) {

		return param;
	}

}





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值