注:Springsecurity版本是4.2.4.RELEASE。html
在jsp中使用has('role')的方式能夠看下個人這篇博客。java
List-1.1web
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>4.2.4.RELEASE</version> </dependency>
List-2.1spring
<bean id="webInvocationFilter" class="org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator"> <constructor-arg ref="filterSecurityInterceptor"/> </bean> <security:http entry-point-ref="xx" use-expressions="true"> ... </security:http>
如List-2.1所示,單獨定義個DefaultWebInvocationPrivilegeEvaluator的bean就能夠了,將interceptor做爲構造函數的參數傳給它,Springsecurity會自動加載它,須要注意的是這個webInvocationFilter要放在security配置的最前面,否則會失效,至於爲何,如今還沒弄清楚。數據庫
List-3.1express
... <body> ... <sec:authorize url="/someUrl"> 有權限的用戶才能看到這段話。 </sec:authorize> ... </body> ...
我選擇使用url的方式而非has('role')的方式,理由:角色名稱、個數頗有可能會常常變更,若是咱們以has('role')的方式將role寫到了jsp中,那麼後面咱們修改角色時,頗有可能要去修改jsp中的角色;相反,若是以url的方式,那麼咱們只須要修改數據庫中url、role、用戶間的關係,不須要去關注是否要修改jsp中的權限。jsp
以下圖4.1所示,DefaultWebInvocationPrivilegeEvaluator的構造方法中須要AbstractSecurityInterceptor。maven
圖4.1ide
來看isAllowed方法,以下List-4.1所示:函數
List-4.1
public boolean isAllowed(String contextPath, String uri, String method, Authentication authentication) { Assert.notNull(uri, "uri parameter is required"); FilterInvocation fi = new FilterInvocation(contextPath, uri, method); Collection<ConfigAttribute> attrs = securityInterceptor .obtainSecurityMetadataSource().getAttributes(fi); if (attrs == null) { if (securityInterceptor.isRejectPublicInvocations()) { return false; } return true; } if (authentication == null) { return false; } try { securityInterceptor.getAccessDecisionManager().decide(authentication, fi, attrs); } catch (AccessDeniedException unauthorized) { if (logger.isDebugEnabled()) { logger.debug(fi.toString() + " denied for " + authentication.toString(), unauthorized); } return false; } return true; }