java 防止反射_解决反射型XSS漏洞攻击

对于程序员来说安全防御,无非从两个方面考虑,要么前端要么后台。

一、首先从前端考虑过滤一些非法字符。

前端的主控js中,在 输入框标签中,

找到点击发送按钮后,追加到聊天panel前 进行过滤input输入内容

1 // 过滤xss反射型漏洞

2 filterinputtxt: function (html) {

3 html = html.replace(/(.*]+>.*)/g,""); // html标记

4 html = html.replace(/([\r\n])[\s]+/g, ""); // 换行、空格

5 html = html.replace(//g, ""); // html注释

6 html = html.replace(/['"‘’“”!@#$%^&*{}!¥()()×+=]/g, ""); // 非法字符

7 html = html.replace("alert","");

8 html = html.replace("eval","");

9 html = html.replace(/(.*javascript.*)/gi,"");

10 if (html === "") {

11 html = "你好";

12 }

13 return html;

14 }

二、在后台api服务解决反射型xss漏洞

thinking:一般来说前端可以过滤一下基本的非法恶意代码攻击,如果恶意脚本被请求到服务端啦,那么就需要请求参数未请求接口前进行过滤一些非法字符。

handle:1、自定义过滤器实现filter接口

2、在dofilter方法中对request、response进行设置处理

##处理request请求参数。

1 /*

2 * copyright (c), 2001-2019, xiaoi机器人

3 * author: han.sun

4 * date: 2019/2/28 11:39

5 * history:

6 *

7 * 作者姓名 修改时间 版本号 描述

8 */

9 package com.eastrobot.robotdev.filter;

10

11 import javax.servlet.http.httpservletrequest;

12 import javax.servlet.http.httpservletrequestwrapper;

13 import java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值