SpringBoot中間件之封裝統(tǒng)一白名單配置
背景
上線初期提供可配置的白名單用戶進(jìn)行訪問驗證,把控整體運行風(fēng)險。
解決痛點:
APOLLO企業(yè)控制也可以,多個業(yè)務(wù)功能,要配置多個apollo,如ASwitch,BSwitch,這樣會非常耗時耗力,而且等功能穩(wěn)定,要放開企業(yè)/用戶限制時,只能將APOLLO的值置為空串或者某個特殊值(已表示對全部企業(yè)/用戶放開),日后會殘留在代碼里。
方案設(shè)計
白名單屬于業(yè)務(wù)系統(tǒng)開發(fā)過程中可重復(fù)使用的通用功能,所以我們把這樣的工具型功能提煉為技術(shù)組件,各個需要的使用此功能的系統(tǒng)引入此組件。
技術(shù)點
自定義注解、切面和springboot讀取配置文件的處理
代碼實現(xiàn)
1.切面
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface DoWhiteList { /** * key:當(dāng)前接口入?yún)⑿枰崛〉膶傩? * @return */ String key() default ""; /** * returnJson: 攔截用戶請求后返回的提示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 * 只會實例化一個Bean對象 */ @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()); } // 返回對象 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; } }
測試
白名單中只允許userId為123的訪問
測試場景一
userId為123:
切面攔截判斷該userid在白名單配置中,放行
測試場景二
userId為【非123】:
切面攔截判斷該userid不在白名單配置中,攔截
總結(jié)
日后每上線一個新功能/接口,用該注解可以實現(xiàn)批量梯度控制訪問來把控整體運行風(fēng)險,等功能穩(wěn)定后,將該注解干掉即可。
到此這篇關(guān)于SpringBoot中間件——封裝統(tǒng)一白名單配置的文章就介紹到這了,更多相關(guān)SpringBoot白名單配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項目多層級多環(huán)境yml設(shè)計詳解
這篇文章主要為大家介紹了SpringBoot項目多層級多環(huán)境yml設(shè)計詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03java通過HttpServletRequest獲取post請求中的body內(nèi)容的方法
本篇文章主要介紹了java通過HttpServletRequest獲取post請求中的body內(nèi)容的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02Java 高并發(fā)三:Java內(nèi)存模型和線程安全詳解
本文主要介紹Java高并發(fā)內(nèi)存模型和線程安全的資料,這里整理詳細(xì)的資料及1.原子性 2.有序性 3.可見性 4.Happen-Before 5.線程安全的概念,有需要的小伙伴可以參考下2016-09-09spring boot 測試單元修改數(shù)據(jù)庫不成功的解決
這篇文章主要介紹了spring boot 測試單元修改數(shù)據(jù)庫不成功的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09