av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

Spring boot如何基于攔截器實現(xiàn)訪問權(quán)限限制

瀏覽:99日期:2023-08-08 15:12:24

遇到一個需求是:要為用戶設(shè)置不同的菜單、數(shù)據(jù)訪問權(quán)限。對于一些特定類型的數(shù)據(jù),有的用戶可以看有的用戶則不可以。一開始沒有太多思路,后來一想是不是可以把'特定類型'這個參數(shù)通過@PathVariable注解加到路徑上,這樣就可以通過攔截器攔截后,校驗此用戶是否可以訪問這個路徑(類型)下的數(shù)據(jù)了。

話不多說,以下為具體實踐

攔截器配置類

@Configurationpublic class UserInterceptorConfig { //為了保證IDbnetUserService提前實例化,能在userInterceptor使用 //ConditionalOnMissingBean可以保證只有一個IDbnetUserService的實例 @Bean @ConditionalOnMissingBean(IDbnetUserService.class) public IDbnetUserService dbnetUserService() { return new DbnetUserServiceImpl(); } //攔截器 @Bean(name = 'userInterceptor') public HandlerInterceptor userInterceptor(IDbnetUserService dbnetUserService) { return new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//url = request.getRequestURI() 判斷url是否可以有權(quán)限訪問而返回true或者false } }; }}

注冊攔截器

//注冊攔截器 @Bean public WebMvcConfigurer registerInterceptor(@Qualifier('userInterceptor') HandlerInterceptor userInterceptor) { return new WebMvcConfigurerAdapter() { @Override public void addInterceptors(InterceptorRegistry registry) {//要攔截的路徑List<String> path = interceptorProperties.getPath();//要排除的路徑List<String> excludePath = interceptorProperties.getExcludePath();registry.addInterceptor(userInterceptor).addPathPatterns(path.stream().toArray(String[]::new)) .excludePathPatterns(excludePath.stream().toArray(String[]::new)); } }; }

配置要攔截的路徑

@Component@ConfigurationProperties(prefix = 'dbnet.interceptor')public class InterceptorProperties { /** * 需要攔截的接口通配 */ private List<String> path = new ArrayList<>(); /** * 需要忽略的接口通配 */ private List<String> excludePath = new ArrayList<>(); public List<String> getPath() { return path; } public void setPath(List<String> path) { this.path = path; } public List<String> getExcludePath() { return excludePath; } public void setExcludePath(List<String> excludePath) { this.excludePath = excludePath; }}

dbnet: interceptor: path: /dbnet/**,/datanet/** excludePath: /dbnet/detail,/datanet/recommend,/datanet/count,/datanet/getKeys,/datenet/metadata/**

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 毛片日韩| 天天亚洲 | 日韩精品一区二区三区四区 | 四虎影院新地址 | 午夜免费视频 | 怡红院免费的全部视频 | 亚洲一区二区在线播放 | 中文字幕亚洲一区二区三区 | 91社区在线高清 | 日本成人在线网址 | 91av免费观看 | 亚洲欧洲一区二区 | 免费三级黄 | 97色在线视频 | 天堂资源视频 | 国产激情在线播放 | 午夜色播 | 精精国产xxxx视频在线播放7 | 精品欧美色视频网站在线观看 | 日本精品视频在线 | 中文字幕国产一区 | av免费入口 | 精品国产乱码久久久久久丨区2区 | caoporon| 日日日日操 | 天天搞天天操 | 免费在线观看一区二区三区 | 丁香五月缴情综合网 | 精品久久久久久久人人人人传媒 | 日韩www| 干干干操操操 | a级大片免费观看 | 亚洲成人av一区二区 | 在线观看中文字幕 | 最近免费日本视频在线 | 欧美精品久久久久久久久老牛影院 | 日韩影院一区 | 久久久久久高潮国产精品视 | 国产精品久久精品 | 91av在线免费观看 | 美女爽到呻吟久久久久 |