亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring boot如何基于攔截器實現訪問權限限制

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

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

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

攔截器配置類

@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是否可以有權限訪問而返回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/**

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 九九在线观看免费视频 | 免费观看wwwwwww| 九九九精品在线观看 | 色天天综合网色鬼综合 | www.毛片在线观看 | 日韩精品久久久毛片一区二区 | 女神穿上情趣丝袜啪啪一整晚 | 欧美特级特黄a大片免费 | 91香蕉国产 | 免费大片在线观看 | 香蕉99| 伊人伊成久久人综合网777 | 免费国产精成人品 | 国产成人18黄网站在线观看网站 | 欧美视频第一区 | 日本一级毛片在线观看 | 美国毛片毛片全部免费 | 日本一二三精品黑人区 | 午夜视频偷拍在线观看免费 | 久久久久美女 | 国产成人毛片毛片久久网 | 婷婷午夜 | 免费a级| 国产一二三区在线 | 国产一级一级一级国产片 | 欧美视频三区 | 亚洲精品国产精品乱码视色 | 国产成人爱片免费观看视频 | 久久综合一本 | 国产情侣真实露脸在线 | 精品中文字幕一区二区三区四区 | 99热视| 欧美日韩一区二区三在线 | 国产另类在线观看 | 国产精品亚洲片在线花蝴蝶 | 男人午夜免费视频 | 色伊人色成人婷婷六月丁香 | 国外成人免费视频 | 香蕉视频网站在线观看 | 欧美三级在线观看黄 | 69视频最新在线观看 |