JSP 開發之Spring Security詳解
前言:
spring Security是一個能夠為基于Spring的企業應用系統提供描述性安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC(依賴注入,也稱控制反轉)和AOP(面向切面編程)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重復代碼的工作。
Spring Security 的前身是 Acegi Security ,是 Spring 項目組中用來提供安全認證服務的框架。Spring Security 為基于J2EE企業應用軟件提供了全面安全服務。特別是使用領先的J2EE解決方案-Spring框架開發的企業軟件項目。
功能
Spring Security對Web安全性的支持大量地依賴于Servlet過濾器。這些過濾器攔截進入請求,并且在應用程序處理該請求之前進行某些安全處理。 Spring Security提供有若干個過濾器,它們能夠攔截Servlet請求,并將這些請求轉給認證和訪問決策管理器處理,從而增強安全性。根據自己的需要,可以使用表7.4中所列的幾個過濾器來保護自己的應用程序。
如果使用過Servlet過濾器,那么知道要讓它們生效,就必須在Web應用程序的web.xml文件中使用<filter> 和<filter-mapping>元素配置它們。雖然這樣做能起作用,但是它并不適用于使用依賴注入進行的配置。
FilterToBeanProxy是一個特殊的Servlet過濾器,它本身做的工作并不多,而是將自己的工作委托給Spring應用程序上下文 中的一個Bean來完成。被委托的Bean幾乎和其他的Servlet過濾器一樣,實現javax.servlet.Filter接 口,但它是在Spring配置文件而不是web.xml文件中配置的。
實際上,FilterToBeanProxy代理給的那個Bean可以是javax.servlet.Filter的任意實現。這可以是 Spring Security的任何一個過濾器,或者它可以是自己創建的一個過濾器。但是正如本書已經提到的那樣,Spring Security要求至少配置四個而且可能一打或者更多的過濾器
通過在許多項目中實踐應用以及社區的貢獻,如今的Spring Security已經成為Spring Framework下最成熟的安全系統,它為我們提供了強大而靈活的企業級安全服務,如:
簡單例子
1、創建web工程springSecurity3
2、把從spring網站下載的spring-security-3.1.0.RELEASE解壓,并將其中的spring-security-samples-contacts-3.1.0.RELEASE.war解壓,將jar包放到lib目錄下。
3、修改配置web.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--加載Spring XML配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:securityConfig.xml </param-value> </context-param> <!-- Spring Secutiry3.1的過濾器鏈配置 --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 容器啟動監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、在src下面創建securityConfig.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.1.xsd"> <!--登錄頁面不過濾 --> <http pattern="/login.jsp" security="none"/> <http access-denied-page="/accessDenied.jsp"> <form-login login-page="/login.jsp"/> <!--訪問/admin.jsp資源的用戶必須具有ROLE_ADMIN的權限 --> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/> <!--訪問/**資源的用戶必須具有ROLE_USER的權限 --> <intercept-url pattern="/**" access="ROLE_USER"/> <session-management> <concurrency-control max-sessions="1" error-if-maximum-exceeded="false"/> </session-management> </http> <authentication-manager> <authentication-provider> <user-service> <user name="john" password="john" authorities="ROLE_USER" /> <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="guest" password="guest" authorities="ROLE_GUEST" /> </user-service> </authentication-provider> </authentication-manager> </b:beans>
5、在WebRoot中創建login.jsp內容如下:
<body> <form action="j_spring_security_check" method="POST"> <table> <tr> <td>用戶:</td> <td><input type='text'name='j_username'></td> </tr> <tr> <td>密碼:</td> <td><input type='password'name='j_password'></td> </tr> <tr> <td><input name="reset"type="reset"></td> <td><input name="submit"type="submit"></td> </tr> </table> </form> </body>
6、在WebRoot中創建accessDenied.jsp,
<body> 您的訪問被拒絕,無權訪問該資源!<br> </body>
創建admin.jsp內容如下:
<body> 歡迎來到管理員頁面. <br> </body>
修改index.jsp內容如下:
<body> 這是首頁,歡迎<sec:authentication property="name"/>!<br> <a href="admin.jsp" rel="external nofollow" >進入admin頁面</a> <a href="other.jsp" rel="external nofollow" >進入其它頁面</a> </body>
好了,部署項目,并訪問index.jsp.
用戶名就是剛才部署的那個用戶名。什么?忘了。那好吧,我再給你指出來
<user name="john" password="john" authorities="ROLE_USER" /> <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
權限不同訪問的頁面就不同。可以試試的
以上就是JSP 開發中Spring Security 的實例詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答