Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉(zhuǎn)Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。
前言
在Spring Security源碼分析十三:Spring Security 基于表達(dá)式的權(quán)限控制中,我們只是在后臺增加了權(quán)限控制,并未在頁面做任何處理,與之對應(yīng)的按鈕和鏈接還是會顯示在頁面上,用戶體驗(yàn)較差。本章使用Spring Security標(biāo)簽庫來包裹需要保護(hù)的內(nèi)容。
freemarker中使用security標(biāo)簽
增加security標(biāo)簽庫依賴
<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();
}
頁面中使用標(biāo)簽
<!-- 引入標(biāo)簽-->
<#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標(biāo)簽格式如下-->
<@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標(biāo)簽
authorize
authorize用于判斷用戶是否具有對應(yīng)權(quán)限,從而控制其限制的內(nèi)容,包含以下屬性。
access
access屬性需要使用表達(dá)式來判斷權(quán)限,當(dāng)表達(dá)式的返回結(jié)果為true時(shí)表示擁有對應(yīng)的權(quán)限。
<@sec.authorize access="hasRole('ROLE_ADMIN')">
此內(nèi)容僅對在授予權(quán)限列表中擁有“ROLE_ADMIN”權(quán)限的用戶可見
</@sec.authorize>
--------------------------
<@sec.authorize access="hasPermission(#domain,'read') or hasPermission(#domain,'write')">
只有具有讀取或?qū)懭霗?quán)限的用戶才能看到此內(nèi)容,該用戶被發(fā)現(xiàn)為名為“domain”的請求屬性。
</@sec.authorize>
-----------------------------
<@sec.authorize url="/admin">
此內(nèi)容僅對有權(quán)將請求發(fā)送到“/ admin”鏈接的用戶可見
</@sec.authorize>
authentication
authentication標(biāo)簽用來代表當(dāng)前Authentication對象,主要用于獲取當(dāng)前Authentication的相關(guān)信息。包含以下屬性。
property
property屬性只允許指定Authentication所擁有的屬性。
<!--獲取當(dāng)前用戶的用戶名-->
<@sec:authentication property="principal.username" />
var屬性
var屬性用于指定一個(gè)屬性名,這樣當(dāng)獲取到了authentication的相關(guān)信息后會將其以var指定的屬性名進(jìn)行存放,默認(rèn)是存放在pageConext中??梢酝ㄟ^scope屬性進(jìn)行指定。此外,當(dāng)指定了var屬性后,authentication標(biāo)簽不會將獲取到的信息在頁面上進(jìn)行展示,如需展示用戶應(yīng)該通過var指定的屬性進(jìn)行展示,或去掉var屬性。
<@sec.authentication property="principal.username" scope="session" var="username"/>
${username }
accesscontrollist
該標(biāo)簽只有在與spring security的acl模塊一起使用時(shí)才有效。它會檢查指定域?qū)ο蟮谋匦铏?quán)限的逗號分隔列表。如果當(dāng)前用戶擁有所有這些權(quán)限,則會評估標(biāo)簽正文。如果他們不這樣做,它將被跳過。
<@sec.accesscontrollist hasPermission="1,2" domainObject="${someObject}">
如果用戶具有給定對象上的值“1”或“2”表示的所有權(quán)限,則會顯示此信息
</@sec.accesscontrollist>
代碼下載
從我的 github 中下載,https://github.com/longfeizheng/logback
[圖片上傳失敗...(image-48b4a2-1520346223416)]
??????關(guān)注微信小程序java架構(gòu)師歷程
上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提高自己的技術(shù)嗎?來吧這里有你需要的java架構(gòu)文章,1.5w+的java工程師都在看,你還在等什么?