CONAN ZONE

          你越掙扎我就越興奮

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            0 Posts :: 282 Stories :: 0 Comments :: 0 Trackbacks
          1. Spring Security
            11個步驟為應(yīng)用程序添加安全防護(hù)
          2. 歷史與現(xiàn)狀
            自2003年出現(xiàn)的Spring擴(kuò)展插件Acegi Security發(fā)展而來。
            目前最新版本為3.x,已成為Spring的一部分。
            為J2EE企業(yè)應(yīng)用程序提供可靠的安全性服務(wù)。
          3. Authentication vs. Authorization
            區(qū)分概念驗(yàn)證與授權(quán)
            驗(yàn)證
            這個用戶是誰?
            用戶身份可靠嗎?
            授權(quán)
            某用戶A是否可以訪問資源R
            某用戶A是否可以執(zhí)行M操作
            某用戶A是否可以對資源R執(zhí)行M操作
          4. SS中的驗(yàn)證特點(diǎn)
            支持多種驗(yàn)證方式
            支持多種加密格式
            支持組件的擴(kuò)展和替換
            可以本地化輸出信息
          5. SS中的授權(quán)特點(diǎn)
            支持多種仲裁方式
            支持組件的擴(kuò)展和替換
            支持對頁面訪問、方法訪問、對象訪問的授權(quán)。
          6. SS核心安全實(shí)現(xiàn)
            Web安全
            通過配置Servlet Filter激活SS中的過濾器鏈
            實(shí)現(xiàn)Session一致性驗(yàn)證
            實(shí)現(xiàn)免登陸驗(yàn)證(Remember-Me驗(yàn)證)
            提供一系列標(biāo)簽庫進(jìn)行頁面元素的安全控制
            方法安全
            通過AOP模式實(shí)現(xiàn)安全代理
            Web安全與方法安全均可以使用表達(dá)式語言定義訪問規(guī)則
          7. 配置SS
            配置Web.xml,應(yīng)用安全過濾器
            配置Spring,驗(yàn)證與授權(quán)部分
            在web頁面中獲取用戶身份
            在web頁面中應(yīng)用安全標(biāo)簽庫
            實(shí)現(xiàn)方法級安全
          8. 1:配置web.xml
            Java代碼
            1. <filter>  
            2. <filter-name>springSecurityFilterChain</filter-name>  
            3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
            4. </filter>  
            5. <filter-mapping>  
            6. <filter-name>springSecurityFilterChain</filter-name>  
            7. <url-pattern>/*</url-pattern>  
            8. </filter-mapping>  
            9. <context-param>  
            10. <param-name>contextConfigLocation</param-name>  
            11. <param-value>classpath:spring.xml</param-value>  
            12. </context-param>  
            13. <listener>  
            14. <listener-class>  
            15. org.springframework.web.context.ContextLoaderListener  
            16. </listener-class>  
            17. </listener>  
             
          9. 2:Spring配置文件中設(shè)置命名空間
            Java代碼
            1. <?xml version="1.0" encoding="UTF-8"?>  
            2. <beans:beansxmlns="http://www.springframework.org/schema/security"  
            3. xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            4. xsi:schemaLocation="http://www.springframework.org/schema/beans  
            5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            6. http://www.springframework.org/schema/security  
            7. http://www.springframework.org/schema/security/spring-security-3.0.xsd">  
            8. </beans:beans>  
             
          10. 3:配置最基本的驗(yàn)證與授權(quán)
            Java代碼
            1. <http auto-config="true">  
            2. <intercept-url pattern="/**" access="ROLE_USER" />  
            3. </http>  
            4. <authentication-manager>  
            5. <authentication-provider>  
            6. <user-service>  
            7. <user name="tom" password="123" authorities="ROLE_USER, ROLE_A" />  
            8. <user name="jerry" password="123" authorities="ROLE_USER, ROLE_B" />  
            9. </user-service>  
            10. </authentication-provider>  
            11. </authentication-manager>  
             
          11. 4:通過數(shù)據(jù)庫驗(yàn)證用戶身份
            Java代碼
            1. <authentication-manager>  
            2. <authentication-provider>  
            3. <password-encoder hash=“md5”/>  
            4. <jdbc-user-service data-source-ref="dataSource"/>  
            5. </authentication-provider>  
            6. </authentication-manager>  
            7. 數(shù)據(jù)表結(jié)構(gòu)見SS說明手冊附錄A  
             
          12. 5:完善web頁面驗(yàn)證規(guī)則
            Java代碼
            1. <http auto-config="true">  
            2. <intercept-url pattern="/js/**" filters="none"/>  
            3. <intercept-url pattern="/css/**" filters="none"/>  
            4. <intercept-url pattern="/images/**" filters="none"/>  
            5. <intercept-url pattern="/a.jsp" access="ROLE_A" />  
            6. <intercept-url pattern="/b.jsp" access="ROLE_B" />  
            7. <intercept-url pattern="/c.jsp" access="ROLE_A, ROLE_B" />  
            8. <intercept-url pattern="/**" access="ROLE_USER" />  
            9. </http>  
             
          13. 6:自定義驗(yàn)證配置
            Java代碼
            1. <http auto-config="true">  
            2. <!-- 指定登陸頁面、成功頁面、失敗頁面-->  
            3. <form-login login-page="/login.jsp" default-target-url="/index.jsp" authentication-failure-url="/login.jsp" />  
            4. <!-- 嘗試訪問沒有權(quán)限的頁面時跳轉(zhuǎn)的頁面 -->  
            5. <access-denied-handler error-page="/accessDenied.jsp"/>  
            6. <!-- 使用記住用戶名、密碼功能,指定數(shù)據(jù)源和加密的key -->  
            7. <remember-me data-source-ref="dataSource" />  
            8. <!-- logout頁面,logout后清除session -->  
            9. <logout invalidate-session="true" logout-success-url="/login.jsp" />  
            10. <!-- session 失效后跳轉(zhuǎn)的頁面,最大登陸次數(shù) -->  
            11. <session-management invalid-session-url="/sessionTimeout.htm">  
            12. <concurrency-control max-sessions="1" expired-url="/sessionTimeout.htm" />  
            13. </session-management>  
            14. </http>  
            15. 可以使用SS自帶的登陸頁面作為login.jsp的模板  
          14. 7:本地化消息輸出
            拷貝本地化資源文件后,在配置文件中加載該文件:
            Java代碼
            1. <!-- 加載錯誤信息資源文件 -->  
            2. <beans:bean id="messageSource"   
            3. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
            4. <beans:property name="basename" value="classpath:messages"/>  
            5. </beans:bean>  
            6. 資源文件在SS核心包:spring-security-core-3.0.2.RELEASE.jar的orgspringframeworksecurity目錄中  
             
          15. 8:在web頁面中獲取用戶信息
            Java代碼
            1. 方式一:Java代碼  
            2. Authentication auth = SecurityContextHolder.getContext().getAuthentication();  
            3. Collection<GrantedAuthority> col = auth.getAuthorities();  
            4. 方式二:標(biāo)簽庫  
            5. <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>  
            6. <sec:authentication property="name“/>  
            7. <sec:authentication property="authorities“/>  
             
          16. 9:在web頁面進(jìn)行元素安全控制
            Java代碼
            1. 方式一  
            2. <sec:authorizeifAnyGranted="ROLE_A">  
            3. <a href="a.jsp">你可以訪問a.jsp</a>  
            4. </sec:authorize>  
            5. <sec:authorizeifNotGranted="ROLE_A">  
            6. 你不可以訪問a.jsp  
            7. </sec:authorize>  
            8. 方式二  
            9. <sec:authorizeurl="/a.jsp">  
            10. <a href="a.jsp">你可以訪問a.jsp</a>  
            11. </sec:authorize>  
             
          17. 10:全局方法安全控制
            Java代碼
            1. <global-method-security pre-post-annotations="enabled">  
            2. <protect-pointcut expression="execution(* com.xasxt.*Service.add*(..))" access="ROLE_A"/>  
            3. <protect-pointcut expression="execution(* com.xasxt.*Service.delete*(..))" access="ROLE_B"/>  
            4. </global-method-security>  
            5. 此處使用了AspectJ中常用的切入點(diǎn)表達(dá)式(百度:AspectJ execution)  
             
          18. 11:使用注解進(jìn)行方法安全控制
            Java代碼
            1. public class DemoService {  
            2. @PreAuthorize("hasRole(&apos;ROLE_A&apos;)")  
            3. public void methodA() {  
            4. }  
            5. @PreAuthorize("hasAnyRole(&apos;ROLE_A, ROLE_B&apos;)")  
            6. public void methodB() {  
            7. }  
            8. }  
            9. hasRole與hasAnyRole為SS通用內(nèi)置表達(dá)式(google : spring security Common Built-In Expressions)  
             
          19. 12:下一步做什么???
            采用更安全的驗(yàn)證方式
            采用安全的數(shù)據(jù)傳輸方式
            實(shí)現(xiàn)動態(tài)授權(quán)
            自定義驗(yàn)證與授權(quán)部件
            實(shí)現(xiàn)數(shù)據(jù)級安全
          posted on 2010-04-12 13:36 CONAN 閱讀(1096) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 云阳县| 阿尔山市| 临安市| 台前县| 梅州市| 陇川县| 阿尔山市| 华蓥市| 紫云| 青川县| 陵水| 历史| 和平区| 五台县| 如皋市| 翁牛特旗| 永和县| 宜兴市| 大同县| 安康市| 内黄县| 凉城县| 革吉县| 漯河市| 伊通| 新兴县| 手游| 恩施市| 晴隆县| 柳林县| 惠东县| 尼木县| 丰台区| 新建县| 岳阳市| 恩平市| 区。| 绥江县| 通山县| 岳阳县| 西林县|