這篇文章主要介紹了Spring Security使用URL地址進(jìn)行權(quán)限控制的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)是一家專業(yè)提供三穗企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、H5場(chǎng)景定制、小程序制作等業(yè)務(wù)。10年已為三穗眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
目的是:系統(tǒng)內(nèi)存在很多不同的用戶,每個(gè)用戶具有不同的資源訪問權(quán)限,具體表現(xiàn)就是某個(gè)用戶對(duì)于某個(gè)URL是無權(quán)限訪問的。需要Spring Security忙我們過濾。
FilterSecurityInterceptor是Spring Security進(jìn)行URL權(quán)限判斷的,F(xiàn)ilterSecurityInterceptor又繼承于AbstractSecurityInterceptor,由此可推測(cè),我們可以新增一個(gè)Interceptor繼承AbstractSecurityInterceptor,實(shí)現(xiàn)我們自己的權(quán)限校驗(yàn)邏輯。
查看父類及其代碼邏輯,有幾點(diǎn)必須要注意:
1、主要鑒權(quán)方法是調(diào)用父類中accessDecisionManager的decide值,所以我們需要自己實(shí)現(xiàn)一個(gè)accessDecisionManager
2、父類中存在抽象方法public abstract SecurityMetadataSource obtainSecurityMetadataSource();作用是獲取URL及用戶角色對(duì)應(yīng)的關(guān)系。我們需要加入自己的實(shí)現(xiàn)。
以下是部分代碼實(shí)現(xiàn)
主要攔截器JwtUrlSecurityInterceptor,需要在WebSecurityConfig(Spring Security配置)文件中注冊(cè)
//這個(gè)攔截器用來實(shí)現(xiàn)按照用戶權(quán)限,對(duì)所請(qǐng)求的url進(jìn)行攔截 @Bean public JwtUrlSecurityInterceptor jwtUrlSecurityInterceptorBean() throws Exception{ return new JwtUrlSecurityInterceptor(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception { ... httpSecurity.addFilterBefore(jwtUrlSecurityInterceptorBean(), FilterSecurityInterceptor.class); ... }
實(shí)現(xiàn)自定義的accessDecisionManager
package org.zerhusen.security.dsuri; import org.springframework.security.access.AccessDecisionManager; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.authentication.InsufficientAuthenticationException; import org.springframework.security.core.Authentication; import java.util.Collection; /** * Created by dingshuo on 2017/6/28. */ public class MyAccessDecisionManager implements AccessDecisionManager { @Override public void decide(Authentication authentication, Object object, CollectionconfigAttributes) throws AccessDeniedException, InsufficientAuthenticationException { System.out.println("自定義的接口"); throw new AccessDeniedException("no right"); } @Override public Boolean supports(ConfigAttribute attribute) { return true; } @Override public Boolean supports(Class> clazz) { return true; } }
實(shí)現(xiàn)自定義的資源SecurityMetadataSource
package org.zerhusen.security.dsuri; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.access.SecurityConfig; import org.springframework.security.web.FilterInvocation; import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource; import java.util.*; /** * Created by dingshuo on 2017/6/28. */ public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource { private static Map> resourceMap = null; @Autowired UrlMatcher urlMatcher; public MyInvocationSecurityMetadataSource() { //這里可以查數(shù)據(jù)庫實(shí)現(xiàn) //注入dao即可 resourceMap = new HashMap >(); Collection atts = new ArrayList (); ConfigAttribute ca = new SecurityConfig("ROLE_USER1"); atts.add(ca); resourceMap.put("/index.jsp", atts); Collection attsno =new ArrayList (); ConfigAttribute cano = new SecurityConfig("ROLE_NO"); attsno.add(cano); resourceMap.put("/other.jsp", attsno); } @Override public Collection getAttributes(Object object) throws IllegalArgumentException { String url = ((FilterInvocation)object).getRequestUrl(); Iterator ite = resourceMap.keySet().iterator(); while (ite.hasNext()) { String resURL = ite.next(); if (url.equals("/protected")) { return resourceMap.get(resURL); } } return null; } @Override public Collection getAllConfigAttributes() { return null; } @Override public Boolean supports(Class> clazz) { return true; } }
實(shí)現(xiàn)JwtUrlSecurityInterceptor
package org.zerhusen.security.dsuri; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.security.access.AccessDecisionManager; import org.springframework.security.access.SecurityMetadataSource; import org.springframework.security.access.intercept.AbstractSecurityInterceptor; import org.springframework.security.access.intercept.InterceptorStatusToken; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.web.FilterInvocation; import javax.servlet.*; import java.io.IOException; /** * Created by dingshuo on 2017/6/28. */ public class JwtUrlSecurityInterceptor extends AbstractSecurityInterceptor implements Filter { @Autowired public void setMyAccessDecisionManager(){ super.setAccessDecisionManager(myAccessDecisionManagerBean()); } @Bean public MyAccessDecisionManager myAccessDecisionManagerBean(){ return new MyAccessDecisionManager(); } @Bean public MyInvocationSecurityMetadataSource myInvocationSecurityMetadataSourceBean(){ return new MyInvocationSecurityMetadataSource(); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { FilterInvocation fi = new FilterInvocation(request, response, chain); invoke(fi); } @Override public void destroy() { } @Override public Class> getSecureObjectClass() { return FilterInvocation.class; } @Override public SecurityMetadataSource obtainSecurityMetadataSource() { return this.myInvocationSecurityMetadataSourceBean(); } public void invoke(FilterInvocation fi) throws IOException, ServletException { InterceptorStatusToken token = super.beforeInvocation(fi); try { fi.getChain().doFilter(fi.getRequest(), fi.getResponse()); } finally { super.afterInvocation(token, null); } } }
如上是簡單的URL權(quán)限控制
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Spring Security使用URL地址進(jìn)行權(quán)限控制的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!