国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring boot攔截器實(shí)現(xiàn)IP黑名單的完整步驟

瀏覽:4日期:2023-05-11 14:25:41

一·業(yè)務(wù)場(chǎng)景和需要實(shí)現(xiàn)的功能

以redis作為IP存儲(chǔ)地址實(shí)現(xiàn)。

業(yè)務(wù)場(chǎng)景:針對(duì)秒殺活動(dòng)或者常規(guī)電商業(yè)務(wù)場(chǎng)景等,防止惡意腳本不停的刷接口。

實(shí)現(xiàn)功能:寫一個(gè)攔截器攔截掉黑名單IP,額外增加一個(gè)接口,將ip地址添加到redis中,并且返回redis中當(dāng)前全部ip

二·Springboot中定義一個(gè)攔截器

@Order(0)@Aspect@Componentpublic class AopInterceptor { /** * 定義攔截器規(guī)則 */ @Pointcut('execution(* com.test.test.api.controller.test.test.*(..))') public void pointCut() { } /** * 攔截器具體實(shí)現(xiàn) * * @throws Throwable */ @Around(value = 'pointCut()') public Object around(ProceedingJoinPoint point) throws Throwable { try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); //判斷是否為黑名單用戶 String ip = getIpAddress(request); if (checkIpBlack(ip)) { //ip在黑名單中返回false //return false; DefaultResponse defaultResponse = new DefaultResponse(); defaultResponse.setCode(-1); defaultResponse.setMessage('ip在黑名單中,拒絕訪問.'); SysLogHelper.log('IpBlackAopInterceptor', '當(dāng)前請(qǐng)求ip' + ip, 'ip在黑名單中,拒絕訪問'); return defaultResponse; } else { //ip不在黑名單中返回true SysLogHelper.log('IpBlackAopInterceptor', '當(dāng)前請(qǐng)求ip' + ip, 'ip正常,允許訪問'); return point.proceed(); } } catch (Exception e) { e.printStackTrace(); SysLogHelper.error('IpBlackAopInterceptor黑名單攔截異常:', ExceptionUtils.getMessage(e) + '詳細(xì)' + ExceptionUtils.getStackTrace(e), null); } return point.getArgs(); } //對(duì)比當(dāng)前請(qǐng)求IP是否在黑名單中,注意(對(duì)比黑名單ip存放在redis中) public boolean checkIpBlack(String ip) throws Exception { IpBlackBody body = new IpBlackBody(); body = cacheHelper.get('IpBlack:ips', IpBlackBody.class); if (body != null) { for (int i = 0; i < body.getIp().length; i++) { if (body.getIp()[i].equals(ip)) return true; } } return false; }}

三·獲取請(qǐng)求主機(jī)IP地址

public final static String getIpAddress(HttpServletRequest request) throws IOException { // 獲取請(qǐng)求主機(jī)IP地址,如果通過(guò)代理進(jìn)來(lái),則透過(guò)防火墻獲取真實(shí)IP地址 String ip = request.getHeader('x-forwarded-for'); if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { ip = request.getHeader('Proxy-Client-IP'); } if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { ip = request.getHeader('WL-Proxy-Client-IP'); } if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { ip = request.getHeader('HTTP_CLIENT_IP'); } if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { ip = request.getHeader('HTTP_X_FORWARDED_FOR'); } if (ip == null || ip.length() == 0 || 'unknown'.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } else if (ip.length() > 15) { String[] ips = ip.split(','); for (int index = 0; index < ips.length; index++) { String strIp = (String) ips[index]; if (!('unknown'.equalsIgnoreCase(strIp))) { ip = strIp; break; } } } return ip; }

四·擴(kuò)展接口,實(shí)現(xiàn)將黑名單IP寫入redis當(dāng)中,并返回當(dāng)前所有黑名單IP

@RestControllerpublic class IpBlackController { @Autowired(required = false) private CacheHelper cacheHelper; @PostMapping('/testIpBlack') public IpBlackBody IpBlack(@RequestBody IpBlackBody ipBlackBody) throws Exception { IpBlackBody body = new IpBlackBody(); body = cacheHelper.get('IpBlack:ips', IpBlackBody.class); if (body != null) { //拼接當(dāng)前IP與redis中現(xiàn)有ip linkArray(body.getIp(), ipBlackBody.getIp()); //將數(shù)據(jù)賦給body body.setIp(linkArray(body.getIp(), ipBlackBody.getIp())); //setex中第二個(gè)參數(shù)時(shí)間為S,根據(jù)業(yè)務(wù)場(chǎng)景相應(yīng)調(diào)整,此處我設(shè)置為一天 //將body中拼接后的ip地址數(shù)據(jù)寫入redis中 cacheHelper.setex('IpBlack:ips', 86400, body); } else { cacheHelper.setex('IpBlack:ips', 86400, ipBlackBody); body = cacheHelper.get('IpBlack:ips', IpBlackBody.class); return body; } return body; } //拼接兩個(gè)String[]的方法 public static String[] linkArray(String[] array1, String[] array2) { List<String> list = new ArrayList<>(); if (array1 == null) { return array2; } if (array2 == null) { return array1; } for (int i = 0; i < array1.length; i++) { list.add(array1[i]); } for (int i = 0; i < array2.length; i++) { list.add(array2[i]); } String[] returnValue = new String[list.size()]; for (int i = 0; i < list.size(); i++) { returnValue[i] = list.get(i); } return returnValue; }}

總結(jié):

首先根據(jù)需要攔截的controller攔截響應(yīng)請(qǐng)求controller層,然后根據(jù)編寫相關(guān)攔截器的具體實(shí)現(xiàn),其中包含兩部主要操作:

1.獲取到遠(yuǎn)程請(qǐng)求主機(jī)的實(shí)際ip地址

2.對(duì)比當(dāng)前ip是否在黑名單中(此次操作需要讀取redis中的黑名單ip列表)

然后根據(jù)當(dāng)前需求增加了一個(gè)redis接口,實(shí)現(xiàn)將需要封禁的IP地址增加到redis黑名單中并返回當(dāng)前所有的黑名單IP地址。

至此:至此springboot通過(guò)攔截器實(shí)現(xiàn)攔截黑名單功能已經(jīng)實(shí)現(xiàn)。

到此這篇關(guān)于Spring boot攔截器實(shí)現(xiàn)IP黑名單的文章就介紹到這了,更多相關(guān)Springboot攔截器IP黑名單內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 江城| 治多县| 麻江县| 西畴县| 平罗县| 梨树县| 辽阳县| 三门峡市| 合山市| 石河子市| 嘉荫县| 明溪县| 江华| 桦甸市| 阿克陶县| 宁海县| 呼伦贝尔市| 祁连县| 汨罗市| 宜春市| 稷山县| 政和县| 营口市| 邳州市| 自贡市| 通辽市| 祁阳县| 安多县| 大港区| 天津市| 图片| 青川县| 吉首市| 库伦旗| 长沙市| 江陵县| 英吉沙县| 耒阳市| 雷山县| 镇雄县| 革吉县|