Spring Security是一個可以爲基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組能夠在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,爲應用系統提供聲明式的安全訪問控制功能,減小了爲企業系統安全控制編寫大量重複代碼的工做。html
在Spring Security源碼分析十三:Spring Security 基於表達式的權限控制中,咱們只是在後臺增長了權限控制,並未在頁面作任何處理,與之對應的按鈕和連接仍是會顯示在頁面上,用戶體驗較差。本章使用Spring Security
標籤庫來包裹須要保護的內容。java
<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>
複製代碼
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);
}
}
複製代碼
@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>
複製代碼
authorize
用於判斷用戶是否具備對應權限,從而控制其限制的內容,包含如下屬性。git
access
屬性須要使用表達式來判斷權限,當表達式的返回結果爲true時表示擁有對應的權限。github
<@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
的相關信息。包含如下屬性。spring
property屬性只容許指定Authentication所擁有的屬性。apache
<!--獲取當前用戶的用戶名-->
<@sec:authentication property="principal.username" />
複製代碼
var屬性用於指定一個屬性名,這樣當獲取到了authentication的相關信息後會將其以var指定的屬性名進行存放,默認是存放在pageConext中。能夠經過scope屬性進行指定。此外,當指定了var屬性後,authentication標籤不會將獲取到的信息在頁面上進行展現,如需展現用戶應該經過var指定的屬性進行展現,或去掉var屬性。編程
<@sec.authentication property="principal.username" scope="session" var="username"/>
${username }
複製代碼
該標籤只有在與spring security
的acl模塊一塊兒使用時纔有效。它會檢查指定域對象的必需權限的逗號分隔列表。若是當前用戶擁有全部這些權限,則會評估標籤正文。若是他們不這樣作,它將被跳過。小程序
<@sec.accesscontrollist hasPermission="1,2" domainObject="${someObject}">
若是用戶具備給定對象上的值「1」或「2」表示的全部權限,則會顯示此信息
</@sec.accesscontrollist>
複製代碼
從個人 github 中下載,github.com/longfeizhen…微信小程序
🙂🙂🙂關注微信小程序java架構師歷程 上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提升本身的技術嗎?來吧這裏有你須要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?tomcat