SpringBoot中間件之封裝統(tǒng)一白名單配置
背景
上線初期提供可配置的白名單用戶進(jìn)行訪問驗(yàn)證,把控整體運(yùn)行風(fēng)險(xiǎn)。
解決痛點(diǎn):
APOLLO企業(yè)控制也可以,多個(gè)業(yè)務(wù)功能,要配置多個(gè)apollo,如ASwitch,BSwitch,這樣會(huì)非常耗時(shí)耗力,而且等功能穩(wěn)定,要放開企業(yè)/用戶限制時(shí),只能將APOLLO的值置為空串或者某個(gè)特殊值(已表示對(duì)全部企業(yè)/用戶放開),日后會(huì)殘留在代碼里。
方案設(shè)計(jì)
白名單屬于業(yè)務(wù)系統(tǒng)開發(fā)過程中可重復(fù)使用的通用功能,所以我們把這樣的工具型功能提煉為技術(shù)組件,各個(gè)需要的使用此功能的系統(tǒng)引入此組件。
技術(shù)點(diǎn)
自定義注解、切面和springboot讀取配置文件的處理
代碼實(shí)現(xiàn)
1.切面
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface DoWhiteList { /** * key:當(dāng)前接口入?yún)⑿枰崛〉膶傩? * @return */ String key() default ""; /** * returnJson: 攔截用戶請(qǐng)求后返回的提示msg * @return */ String returnJson() default ""; }
擴(kuò)展
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
字如其名,目標(biāo),即自定義注解DoWhiteList是要作用在類、接口還是方法上
2.配置類
/** * @ConfigurationProperties:用于創(chuàng)建指定前綴的自定義配置信息,這樣就在ymL或者properties中讀取到配置信息 */ @ConfigurationProperties("com.whitelist") public class WhiteListProperties { private String users; public String getUsers() { return users; } public void setUsers(String users) { this.users = users; } }
/** * * @ConditionalOnMissingBean * 只會(huì)實(shí)例化一個(gè)Bean對(duì)象 */ @Configuration @EnableConfigurationProperties(WhiteListProperties.class) public class WhiteListAutoConfigure { @Bean("whiteListConfig") @ConditionalOnMissingBean public String whiteListConfig(WhiteListProperties properties) { return properties.getUsers(); } }
3.切面
@Aspect @Component public class DoJoinPoint { private Logger logger = LoggerFactory.getLogger(DoJoinPoint.class); @Resource private String whiteListConfig; @Pointcut("@annotation(cn.bugstack.middleware.whitelist.annotation.DoWhiteList)") public void aopPoint() { } @Around("aopPoint()") public Object doRouter(ProceedingJoinPoint jp) throws Throwable { // 獲取內(nèi)容 Method method = getMethod(jp); DoWhiteList whiteList = method.getAnnotation(DoWhiteList.class); // 獲取字段值 String keyValue = getFiledValue(whiteList.key(), jp.getArgs()); logger.info("middleware whitelist handler method:{} value:{}", method.getName(), keyValue); if (null == keyValue || "".equals(keyValue)) return jp.proceed(); String[] split = whiteListConfig.split(","); // 白名單過濾 for (String str : split) { if (keyValue.equals(str)) { return jp.proceed(); } } // 攔截 return returnObject(whiteList, method); } private Method getMethod(JoinPoint jp) throws NoSuchMethodException { Signature sig = jp.getSignature(); MethodSignature methodSignature = (MethodSignature) sig; return jp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes()); } // 返回對(duì)象 private Object returnObject(DoWhiteList whiteList, Method method) throws IllegalAccessException, InstantiationException { Class<?> returnType = method.getReturnType(); String returnJson = whiteList.returnJson(); if ("".equals(returnJson)) { return returnType.newInstance(); } return JSON.parseObject(returnJson, returnType); } // 獲取屬性值 private String getFiledValue(String filed, Object[] args) { String filedValue = null; for (Object arg : args) { try { if (null == filedValue || "".equals(filedValue)) { filedValue = BeanUtils.getProperty(arg, filed); } else { break; } } catch (Exception e) { if (args.length == 1) { return args[0].toString(); } } } return filedValue; } }
測(cè)試
白名單中只允許userId為123的訪問
測(cè)試場(chǎng)景一
userId為123:
切面攔截判斷該userid在白名單配置中,放行
測(cè)試場(chǎng)景二
userId為【非123】:
切面攔截判斷該userid不在白名單配置中,攔截
總結(jié)
日后每上線一個(gè)新功能/接口,用該注解可以實(shí)現(xiàn)批量梯度控制訪問來把控整體運(yùn)行風(fēng)險(xiǎn),等功能穩(wěn)定后,將該注解干掉即可。
到此這篇關(guān)于SpringBoot中間件——封裝統(tǒng)一白名單配置的文章就介紹到這了,更多相關(guān)SpringBoot白名單配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目接入MQTT的詳細(xì)指南
MQTT是一種輕量級(jí)的消息傳輸協(xié)議,特別適用于物聯(lián)網(wǎng)(IoT)場(chǎng)景,具有低帶寬、高延遲網(wǎng)絡(luò)環(huán)境下的優(yōu)勢(shì),SpringBoot作為流行的 Java開發(fā)框架,能夠方便地與MQTT集成,實(shí)現(xiàn)高效的消息通信,本文將詳細(xì)介紹如何在SpringBoot項(xiàng)目中接入MQTT,需要的朋友可以參考下2025-03-03SpringBoot項(xiàng)目多層級(jí)多環(huán)境yml設(shè)計(jì)詳解
這篇文章主要為大家介紹了SpringBoot項(xiàng)目多層級(jí)多環(huán)境yml設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03java通過HttpServletRequest獲取post請(qǐng)求中的body內(nèi)容的方法
本篇文章主要介紹了java通過HttpServletRequest獲取post請(qǐng)求中的body內(nèi)容的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Java 高并發(fā)三:Java內(nèi)存模型和線程安全詳解
本文主要介紹Java高并發(fā)內(nèi)存模型和線程安全的資料,這里整理詳細(xì)的資料及1.原子性 2.有序性 3.可見性 4.Happen-Before 5.線程安全的概念,有需要的小伙伴可以參考下2016-09-09SpringBoot項(xiàng)目依賴和配置最新示例講解
這篇文章主要介紹了SpringBoot項(xiàng)目依賴和配置,這里主要是搭建項(xiàng)目常用到的maven依賴以及搭建項(xiàng)目會(huì)需要用到的一些配置文件,本文通過示例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11spring boot 測(cè)試單元修改數(shù)據(jù)庫(kù)不成功的解決
這篇文章主要介紹了spring boot 測(cè)試單元修改數(shù)據(jù)庫(kù)不成功的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09