SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限信息的方法
①數(shù)據(jù)庫(kù)中資源與角色對(duì)應(yīng)關(guān)系,以及角色和用戶對(duì)應(yīng)關(guān)系如下圖所示:
②實(shí)現(xiàn)FilterInvocationSecurityMetadataSource類
(1)List<Menu> menus = menuService.getMenusWithRoles();這個(gè)是你自己的資源對(duì)應(yīng)角色的查詢方法。
(2)重寫(xiě)的support方法都返回true
@Configuration public class MyFilterInvocation implements FilterInvocationSecurityMetadataSource { @Autowired private MenuService menuService; AntPathMatcher antPathMatcher = new AntPathMatcher(); @Override public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException { String requestUrl = ((FilterInvocation) object).getRequestUrl(); List<Menu> menus = menuService.getMenusWithRoles(); //- 遍歷數(shù)據(jù)庫(kù)的url,看請(qǐng)求路徑是否與其匹配 for (Menu menu : menus) { //- 如果請(qǐng)求路徑和數(shù)據(jù)庫(kù)的路徑匹配 if (antPathMatcher.match(menu.getUrl(),requestUrl)){ //- 訪問(wèn)該路徑需要的角色 List<Role> roles = menu.getRoles(); String[] strs = new String[roles.size()]; for (int i = 0; i < roles.size(); i++) { strs[i] = roles.get(i).getName(); } return SecurityConfig.createList(strs); } } //- 如果請(qǐng)求路徑和數(shù)據(jù)庫(kù)的所有路徑都不匹配,說(shuō)明這個(gè)資源是登錄后即可訪問(wèn)的 //- 用戶登錄即可訪問(wèn),相當(dāng)于在SecurityConfig中配置了.anyRequest().authenticated() return SecurityConfig.createList("ROLE_LOGIN"); } @Override public Collection<ConfigAttribute> getAllConfigAttributes() { return null; } @Override public boolean supports(Class<?> clazz) { return true; } }
③實(shí)現(xiàn)AccessDecisionManager類
重寫(xiě)的support方法都返回true
@Configuration public class MyDecisionManager implements AccessDecisionManager { @Override public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException { for (ConfigAttribute configAttribute : configAttributes) { String needRole = configAttribute.getAttribute(); if ("ROLE_LOGIN".equals(needRole)) { //- 用戶登錄即可訪問(wèn),相當(dāng)于在SecurityConfig中配置了.anyRequest().authenticated() if (authentication instanceof AnonymousAuthenticationToken) { throw new AccessDeniedException("尚未登錄,請(qǐng)先登錄"); } else { return; } } Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); //這里我寫(xiě)的是只要訪問(wèn)該資源的用戶具有`訪問(wèn)該資源所需要角色`的其中一個(gè)即可 for (GrantedAuthority authority : authorities) { if (authority.getAuthority().equals(needRole)) { return; } } } throw new AccessDeniedException("權(quán)限不足,請(qǐng)聯(lián)系管理員"); } @Override public boolean supports(ConfigAttribute attribute) { return true; } @Override public boolean supports(Class<?> clazz) { return true; } }
④到SecurityConfig配置類中完成相應(yīng)配置
@Autowired private MyDecisionManager myDecisionManager; @Autowired private MyFilterInvocation myFilterInvocation; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() { @Override public <O extends FilterSecurityInterceptor> O postProcess(O object) { object.setAccessDecisionManager(myDecisionManager); object.setSecurityMetadataSource(myFilterInvocation); return object; } }); http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler()); } @Bean MyAccessDeniedHandler myAccessDeniedHandler(){ return new MyAccessDeniedHandler(); }
⑤可選,實(shí)現(xiàn)AccessDeniedHandler類
public class MyAccessDenied implements AccessDeniedHandler { @Override public void handle(HttpServletRequest req, HttpServletResponse resp, AccessDeniedException accessDeniedException) throws IOException, ServletException { resp.setContentType("application/json;charset=utf-8"); PrintWriter pw = resp.getWriter(); pw.write(new ObjectMapper().writeValueAsString(RespBean.error("權(quán)限不夠,請(qǐng)聯(lián)系管理員"))); pw.flush(); pw.close(); } }
到此這篇關(guān)于SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限信息的文章就介紹到這了,更多相關(guān)SpringSecurity動(dòng)態(tài)加載權(quán)限內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法
這篇文章主要介紹了在Java的MyBatis框架中建立接口進(jìn)行CRUD操作的方法,CRUD是指在做計(jì)算處理時(shí)的增加(Create)、重新取得數(shù)據(jù)(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫(xiě),需要的朋友可以參考下2016-04-04java-RGB調(diào)色面板的實(shí)現(xiàn)(事件監(jiān)聽(tīng)器之匿名內(nèi)部類)
這篇文章主要介紹了java-RGB調(diào)色面板的實(shí)現(xiàn)(事件監(jiān)聽(tīng)器之匿名內(nèi)部類),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Data Source與數(shù)據(jù)庫(kù)連接池簡(jiǎn)介(JDBC簡(jiǎn)介)
DataSource是作為DriverManager的替代品而推出的,DataSource 對(duì)象是獲取連接的首選方法,這篇文章主要介紹了Data Source與數(shù)據(jù)庫(kù)連接池簡(jiǎn)介(JDBC簡(jiǎn)介),需要的朋友可以參考下2022-11-11[Spring MVC]-詳解SpringMVC的各種參數(shù)綁定方式
本篇文章主要介紹了SpringMVC的各種參數(shù)綁定方式 ,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12MyBatis-Plus如何最優(yōu)雅最簡(jiǎn)潔地完成數(shù)據(jù)庫(kù)操作
Mybatis-Plus是一個(gè)?Mybatis?的增強(qiáng)工具,在?Mybatis?的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus如何最優(yōu)雅最簡(jiǎn)潔地完成數(shù)據(jù)庫(kù)操作的相關(guān)資料,需要的朋友可以參考下2022-03-03