HTML特殊字符过滤器

22 篇文章 0 订阅


/**
* HTML标签过滤,防止用户恶意提交带HTML标签的数据
*
* @author liulg
*/
public class HTMLCharacterRequest extends HttpServletRequestWrapper {

public HTMLCharacterRequest(HttpServletRequest request) {
super(request);
}

@Override
public String getParameter(String name) {
String value = super.getParameter(name);
return filter(value);
}

@Override
@SuppressWarnings("unchecked")
public Map getParameterMap() {
Map<String, String[]> map = super.getParameterMap();
Map<String, String[]> map_ = new HashMap<String, String[]>();

Set<Entry<String, String[]>> set = map.entrySet();
Iterator<Entry<String, String[]>> it = set.iterator();
while (it.hasNext()) {
Entry<String, String[]> entry = it.next();
if (entry != null && entry.getValue() != null
&& entry.getValue().length > 0) {
String[] array = new String[entry.getValue().length];
for (int i = 0; i < entry.getValue().length; i++) {
array[i] = filter(entry.getValue()[i]);
}
map_.put(entry.getKey(), array);
}
}
return map_;
}

@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
if (values == null || values.length == 0)
return values;
for (int i = 0; i < values.length; i++) {
String str = values[i];
values[i] = filter(str);
}
return values;
}

private String filter(String value) {
if (StringUtil.isNotBlank(value)) {
value = value.replaceAll("<", "[").replaceAll(">", "]");
}
return value;
}

}






/**
* 权限访问控制
*
* @author luo
*/
public class PermitFilter implements Filter {

private static Map<String, Object> sysMenusMap = new HashMap<String, Object>();

private static String[] params = null;

private final static String LOGIN_VIEW = "/jsp/index/login.jsp";

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;

String uri = request.getRequestURI();
String contextPath = request.getContextPath();
uri = uri.substring(contextPath.length());

Object obj = request.getSession().getAttribute(Constant.USER_SESSION);


if(obj == null){

//未登录可放行的访问
for (String p : params) {
if (uri.startsWith(p)) {
chain.doFilter(req, resp);
return;
}
}
//aJax访问 (未登录不可放行)
if (request.getHeader("x-requested-with") != null
&& request.getHeader("x-requested-with").equalsIgnoreCase( // ajax超时处理
"XMLHttpRequest")) {
JspUtils.print(response, "timeout");
return;
}
//普通访问 (未登录不可放行)
request.getRequestDispatcher(LOGIN_VIEW).forward(req, resp);

}else{

//是否越权访问
Users user = (Users) obj;
List<Menu> menus = new ArrayList<Menu>(user.getRose().getMenus());

boolean flag =false;
if(sysMenusMap.get(uri) != null){
for (Menu menu : menus) {
if(StringUtil.isNotBlank(menu.getMenuUrl()))
if(uri.equals("/"+menu.getMenuUrl())){
flag=true;
break;
}
}
}else{
chain.doFilter(new HTMLCharacterRequest(request), resp);
return;
}


if(flag){
// chain.doFilter(request, resp);
chain.doFilter(new HTMLCharacterRequest(request), resp);
return;
}else{
JspUtils.print(response, "<script>alert('无权访问');</script>");
return ;
}

}

}

public void init(FilterConfig config) throws ServletException {
params = config.getInitParameter("p").split(",");
RoseDao roseDao = (RoseDao) Springs.getApp().getBean("roseDaoImpl");
List<Menu> sysMenus = roseDao.findAllMenuBy_URL_NOTNULL();
for (Menu menu : sysMenus) {
sysMenusMap.put("/"+menu.getMenuUrl(), menu.getMenuName());
}
}

public void destroy() { }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值