1. 程式人生 > >Spring Security原始碼分析十五:Spring Security 頁面許可權控制

Spring Security原始碼分析十五:Spring Security 頁面許可權控制

Spring Security是一個能夠為基於Spring的企業應用系統提供宣告式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面程式設計)功能,為應用系統提供宣告式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重複程式碼的工作。

前言

Spring Security原始碼分析十三:Spring Security 基於表示式的許可權控制中,我們只是在後臺增加了許可權控制,並未在頁面做任何處理,與之對應的按鈕和連結還是會顯示在頁面上,使用者體驗較差。本章使用Spring Security

標籤庫來包裹需要保護的內容。

freemarker中使用security標籤

增加security標籤庫依賴

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId
>
<artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> </dependency>

新增ClassPathTldsLoader類載入security.tld

public class
ClassPathTldsLoader {
private static final String SECURITY_TLD = "/META-INF/security.tld"; final private List<String> classPathTlds; public ClassPathTldsLoader(String... classPathTlds) { super(); if(classPathTlds.length == 0){ this.classPathTlds = Arrays.asList(SECURITY_TLD); }else{ this.classPathTlds = Arrays.asList(classPathTlds); } } @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @PostConstruct public void loadClassPathTlds() { freeMarkerConfigurer.getTaglibFactory().setClasspathTlds(classPathTlds); } }

定義ClassPathTldsLoader Bean

   @Bean
    @ConditionalOnMissingBean(ClassPathTldsLoader.class)
    public ClassPathTldsLoader classPathTldsLoader(){
        return new ClassPathTldsLoader();
    }

頁面中使用標籤

<!-- 引入標籤-->
<#assign  sec=JspTaglibs["http://www.springframework.org/security/tags"] />
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主頁</title>
</head>
<body align="center">
<h2>Spring Security Demo</h2>
    <!-- freemarker使用security標籤格式如下-->
    <@sec.authorize access="hasRole('ROLE_ADMIN')">
    you can see this
    </@sec.authorize>
    <@sec.authorize access="isAnonymous()">
    you can see isAnonymous
    </@sec.authorize>
</body>
</html>

常用的Security標籤

authorize用於判斷使用者是否具有對應許可權,從而控制其限制的內容,包含以下屬性。

access

access屬性需要使用表示式來判斷許可權,當表示式的返回結果為true時表示擁有對應的許可權。

<@sec.authorize access="hasRole('ROLE_ADMIN')">
此內容僅對在授予許可權列表中擁有“ROLE_ADMIN”許可權的使用者可見
</@sec.authorize>
--------------------------
<@sec.authorize access="hasPermission(#domain,'read') or hasPermission(#domain,'write')">
只有具有讀取或寫入許可權的使用者才能看到此內容,該使用者被發現為名為“domain”的請求屬性。
</@sec.authorize>
-----------------------------
<@sec.authorize url="/admin">
此內容僅對有權將請求傳送到“/ admin”連結的使用者可見
</@sec.authorize>

authentication

authentication標籤用來代表當前Authentication物件,主要用於獲取當前Authentication的相關資訊。包含以下屬性。

property

property屬性只允許指定Authentication所擁有的屬性。

<!--獲取當前使用者的使用者名稱-->
<@sec:authentication property="principal.username" />
var屬性

var屬性用於指定一個屬性名,這樣當獲取到了authentication的相關資訊後會將其以var指定的屬性名進行存放,預設是存放在pageConext中。可以通過scope屬性進行指定。此外,當指定了var屬性後,authentication標籤不會將獲取到的資訊在頁面上進行展示,如需展示使用者應該通過var指定的屬性進行展示,或去掉var屬性。

 <@sec.authentication property="principal.username" scope="session" var="username"/>
${username }

accesscontrollist

該標籤只有在與spring security的acl模組一起使用時才有效。它會檢查指定域物件的必需許可權的逗號分隔列表。如果當前使用者擁有所有這些許可權,則會評估標籤正文。如果他們不這樣做,它將被跳過。

<@sec.accesscontrollist hasPermission="1,2" domainObject="${someObject}">
如果使用者具有給定物件上的值“1”或“2”表示的所有許可權,則會顯示此資訊
</@sec.accesscontrollist>

程式碼下載

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/wechat/xiaochengxu.png

������關注微信小程式java架構師歷程
上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提高自己的技術嗎?來吧這裡有你需要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?