應用標簽庫:<%@ taglib prefix='
security' uri='http://www.springframework.org/
security/tags' %>
< security:authorize>是一個流程控制標簽,能夠在滿足特定安全需求的條件下顯示它的內容體。它有三個互斥的參數:
ifAllGranted——是一個由逗號分隔的權限列表,用戶必須擁有所有列出的權限時顯示;
ifAnyGranted——是一個由逗號分隔的權限列表,用戶必須至少擁有其中的一個權限時才能顯示;
ifNotGranted——是一個由逗號分隔的權限列表,用戶未擁有所有列出的權限時才能顯示。
< security:authentication>獲得屬性的值比如要獲得用戶名可以這么寫:
< security:authentication property="principal.username"></ security:authentication>
他有三個屬性,property是必須的,另外scope和var,var定義一個變量,scope定義var存在的范圍
< security:authorize>是一個流程控制標簽,能夠在滿足特定安全需求的條件下顯示它的內容體。它有三個互斥的參數:
ifAllGranted——是一個由逗號分隔的權限列表,用戶必須擁有所有列出的權限時顯示;
ifAnyGranted——是一個由逗號分隔的權限列表,用戶必須至少擁有其中的一個權限時才能顯示;
ifNotGranted——是一個由逗號分隔的權限列表,用戶未擁有所有列出的權限時才能顯示。
< security:authentication>獲得屬性的值比如要獲得用戶名可以這么寫:
< security:authentication property="principal.username"></ security:authentication>
他有三個屬性,property是必須的,另外scope和var,var定義一個變量,scope定義var存在的范圍
例子:
有時需要在頁面顯示用戶名,或者根據用戶角色顯示或者不顯示一些內容。這需要使用到spring security提供的標簽庫。
在頁面中引入標簽庫:
<
%@ taglib
prefix
="sec"
uri
="http://www.springframework.org/security/tags"
%
>
使用標簽庫的示例:
<
sec:authentication
property
="principal"
var
="authentication"
/>
<
sec:authorize
ifAllGranted
="ROLE_USER"
>可以訪問
</
sec:authorize
> 用戶名:${authentication.username }
<
br
/>
前台 ROLE_ANONYMOUS表示匿名用戶
在配置文件中可以設置頁面進入的權限
<intercept-url pattern="/Homepage.*" access="ROLE_ADMIN,IS_AUTHENTICATED_ANONYMOUSLY"/>
IS_AUTHENTICATED_ANONYMOUSLY允許匿名用戶進入
IS_AUTHENTICATED_FULLY 允許登錄用戶進入
IS_AUTHENTICATED_REMEMBERED 允許登錄用戶和rememberMe用戶進入
IS_AUTHENTICATED_FULLY:是則滿足以下情況返回通過: **.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的實例 IS_AUTHENTICATED_REMEMBERED:是則滿足以下任一情況返回通過: a*.Authentication是RememberMeAuthenticationToken的實例 b*.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的實例 IS_AUTHENTICATED_ANONYMOUSLY:是則滿足以下任一情況返回通過: a*.Authentication是AnonymousAuthenticationToken的實例 b*.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的實例 c*.Authentication是RememberMeAuthenticationToken的實例
spring-security 在jsp中的標簽庫 1.在jsp中聲明
當然,它不必使用JSP 標簽來實現這些功能,一些人更願意在視圖中保持邏輯越少越好。你可以在你的MVC 控制器中訪問Authentication 對象( 通過調用 SecurityContextHolder.getContext().getAuthentication()) 然后直接在模型中添加數據,來渲染視圖。
2.3、accesscontrollist標簽 這個標簽紙在使用Spring Security ACL 模塊時才可以使用。它檢測一個用逗號分隔的特 定領域對象的需要權限列表。如果當前用戶擁有這些權限的任何一個,標簽內容就會被執行。 否則,就會被略過。
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>2.標簽 目前共有三個標簽
<sec:authorize></sec:authorize> <sec:authentication property=""></sec:authentication> <sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>2.1、authorize標簽 這個標簽用來決定它的內容是否會被執行.
<sec:authorize access="hasRole('supervisor')"> This content will only be visible to users who have the "supervisor" authority in their list of GrantedAuthoritys. </sec:authorize>顯示一個特定的鏈接,如果用戶允許點擊它.
<sec:authorize url="/admin"> This content will only be visible to users who are authorized to send requests to the "/admin" URL. </sec:authorize>2.2、authentication標簽 這個標簽允許訪問當前的Authentication 對象, 保存在安全上下文中。 比如,如果Authentication 的principal 屬性是Spring Security 的UserDetails 對象的一個實例, 就要使用
<sec:authentication property="principal.username" />來渲染當前用戶的名稱。
當然,它不必使用JSP 標簽來實現這些功能,一些人更願意在視圖中保持邏輯越少越好。你可以在你的MVC 控制器中訪問Authentication 對象( 通過調用 SecurityContextHolder.getContext().getAuthentication()) 然后直接在模型中添加數據,來渲染視圖。
2.3、accesscontrollist標簽 這個標簽紙在使用Spring Security ACL 模塊時才可以使用。它檢測一個用逗號分隔的特 定領域對象的需要權限列表。如果當前用戶擁有這些權限的任何一個,標簽內容就會被執行。 否則,就會被略過。
<sec:accesscontrollist hasPermission="1,2" domainObject="${someObject}"> This will be shown if the user has either of the permissions represented by the values "1" or "2" on the given object. </sec:accesscontrollist>
applicationContext_security.xml
<?xml version="1.0" encoding="UTF-8"?> <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http auto-config="true" access-denied-page="/accessDenied.jsp"> <!-- 不要過濾圖片等靜態資源 filters="none"--> <intercept-url pattern="/**/*.jpg" filters="none" /> <intercept-url pattern="/**/*.png" filters="none" /> <intercept-url pattern="/**/*.gif" filters="none" /> <intercept-url pattern="/**/*.css" filters="none" /> <intercept-url pattern="/**/*.js" filters="none" /> <!-- 登陸頁和忘記密碼或注冊等不需要過濾的頁面 --> <intercept-url pattern="/login.jsp" filters="none" /> <intercept-url pattern="/jsp/forgotpassword.jsp" filters="none" /> <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" default-target-url="/index.jsp" /> <logout logout-success-url="/login.jsp" /> <!-- "記住我"功能,采用持久化策略(將用戶的登錄信息存放在數據庫表中)需要創建一張persistent_logins 表 <remember-me data-source-ref="dataSource" /> --><!-- 檢測失效的sessionId,超時時定位到另外一個URL --> <session-management invalid-session-url="/sessionTimeout.jsp" /> <!-- 增加一個自定義的filter,放在FILTER_SECURITY_INTERCEPTOR之前,實現用戶、角色、權限、資源的數據庫管理。 --> <custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR" /> </http> <!-- 一個自定義的filter 必須包含authenticationManager,accessDecisionManager,securityMetadataSource三個屬性。 --> <b:bean id="myFilter" class="org.joshua.ss.MyFilterSecurityInterceptor"> <b:property name="authenticationManager" ref="authenticationManager" /> <b:property name="accessDecisionManager" ref="myAccessDecisionManager" /> <b:property name="securityMetadataSource" ref="mySecurityMetadataSource" /> </b:bean> <!-- 注意能夠為authentication-manager 設置alias別名 --> <authentication-manager alias="authenticationManager"> <authentication-provider user-service-ref="myUserDetailService"><!-- <password-encoder hash="md5" /> --></authentication-provider> </authentication-manager> <b:bean id="myUserDetailService" class="org.joshua.ss.MyUserDetailService" /> <!-- 訪問決策器,決定某個用戶具有的角色,是否有足夠的權限去訪問某個資源。11/3/23 --> <b:bean id="myAccessDecisionManager" class="org.joshua.ss.MyAccessDecisionManager"> </b:bean> <!-- 資源源數據定義,將所有的資源和權限對應關系建立起來,即定義某一資源可以被哪些角色去訪問。11/3/23 --> <b:bean id="mySecurityMetadataSource" class="org.joshua.ss.MyInvocationSecurityMetadataSource"> </b:bean> </b:beans>
dbConfig.properties
jdbc.user=scott jdbc.pwd=snail jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:oracle jdbc.driver=oracle.jdbc.driver.OracleDriver
ehcache.xml 沒有深入的研究,暫且擱置
<?xml version="1.0" encoding="UTF-8" ?> <ehcache> <diskStore path="user.dir"></diskStore> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
如果只是想從頁面上顯示當前登陸的用戶名,可以直接使用Spring Security提供的taglib。
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> <div>username : <sec:authentication property="name"/></div> 如果想在程序中獲得當前登陸用戶對應的對象。
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext() .getAuthentication() .getPrincipal(); 如果想獲得當前登陸用戶所擁有的所有權限。
GrantedAuthority[] authorities = userDetails.getAuthorities();