Spring boot如何基于攔截器實現(xiàn)訪問權(quán)限限制
遇到一個需求是:要為用戶設(shè)置不同的菜單、數(shù)據(jù)訪問權(quán)限。對于一些特定類型的數(shù)據(jù),有的用戶可以看有的用戶則不可以。一開始沒有太多思路,后來一想是不是可以把"特定類型"這個參數(shù)通過@PathVariable注解加到路徑上,這樣就可以通過攔截器攔截后,校驗此用戶是否可以訪問這個路徑(類型)下的數(shù)據(jù)了。
話不多說,以下為具體實踐
攔截器配置類
@Configuration public 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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解FileInputStream讀取文件數(shù)據(jù)的兩種方式
這篇文章主要介紹了詳解FileInputStream讀取文件數(shù)據(jù)的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳解java.lang.NumberFormatException錯誤及解決辦法
這篇文章主要介紹了詳解java.lang.NumberFormatException錯誤及解決辦法,本文詳解的介紹了錯誤的解決方法,感興趣的可以一起來了解一下2020-05-05Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式
Spring Cloud OAuth2 生成access token的請求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細(xì)介紹一下,感興趣的可以了解一下2021-07-07Java常用的數(shù)據(jù)脫敏方法(手機、郵箱、身份證號)
這篇文章主要給大家介紹了關(guān)于Java常用的數(shù)據(jù)脫敏(手機、郵箱、身份證號)的相關(guān)資料,信息脫敏對某些敏感信息通過脫敏規(guī)則進行數(shù)據(jù)的變形,實現(xiàn)敏感隱私數(shù)據(jù)的可靠保護,需要的朋友可以參考下2023-07-07springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn)
本文主要介紹了springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08static關(guān)鍵字有何魔法?竟讓Spring Boot搞出那么多靜態(tài)內(nèi)部類(推薦)
這篇文章主要介紹了static關(guān)鍵字有何魔法?竟讓Spring Boot搞出那么多靜態(tài)內(nèi)部類,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07