亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JSP 開發(fā)之Spring Security詳解

 更新時間:2017年08月02日 14:47:05   作者:快樂的燕子會飛  
這篇文章主要介紹了JSP 開發(fā)之Spring Security詳解的相關(guān)資料,spring Security是一個能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供描述性安全訪問控制解決方案的安全框架,需要的朋友可以參考下

JSP 開發(fā)之Spring Security詳解

前言:

spring Security是一個能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供描述性安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用了Spring IoC(依賴注入,也稱控制反轉(zhuǎn))和AOP(面向切面編程)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。

Spring Security 的前身是 Acegi Security ,是 Spring 項(xiàng)目組中用來提供安全認(rèn)證服務(wù)的框架。Spring Security 為基于J2EE企業(yè)應(yīng)用軟件提供了全面安全服務(wù)。特別是使用領(lǐng)先的J2EE解決方案-Spring框架開發(fā)的企業(yè)軟件項(xiàng)目。

功能

Spring Security對Web安全性的支持大量地依賴于Servlet過濾器。這些過濾器攔截進(jìn)入請求,并且在應(yīng)用程序處理該請求之前進(jìn)行某些安全處理。 Spring Security提供有若干個過濾器,它們能夠攔截Servlet請求,并將這些請求轉(zhuǎn)給認(rèn)證和訪問決策管理器處理,從而增強(qiáng)安全性。根據(jù)自己的需要,可以使用表7.4中所列的幾個過濾器來保護(hù)自己的應(yīng)用程序。

如果使用過Servlet過濾器,那么知道要讓它們生效,就必須在Web應(yīng)用程序的web.xml文件中使用<filter> 和<filter-mapping>元素配置它們。雖然這樣做能起作用,但是它并不適用于使用依賴注入進(jìn)行的配置。   

FilterToBeanProxy是一個特殊的Servlet過濾器,它本身做的工作并不多,而是將自己的工作委托給Spring應(yīng)用程序上下文 中的一個Bean來完成。被委托的Bean幾乎和其他的Servlet過濾器一樣,實(shí)現(xiàn)javax.servlet.Filter接 口,但它是在Spring配置文件而不是web.xml文件中配置的。   

實(shí)際上,F(xiàn)ilterToBeanProxy代理給的那個Bean可以是javax.servlet.Filter的任意實(shí)現(xiàn)。這可以是 Spring Security的任何一個過濾器,或者它可以是自己創(chuàng)建的一個過濾器。但是正如本書已經(jīng)提到的那樣,Spring Security要求至少配置四個而且可能一打或者更多的過濾器

通過在許多項(xiàng)目中實(shí)踐應(yīng)用以及社區(qū)的貢獻(xiàn),如今的Spring Security已經(jīng)成為Spring Framework下最成熟的安全系統(tǒng),它為我們提供了強(qiáng)大而靈活的企業(yè)級安全服務(wù),如:

  •              認(rèn)證授權(quán)機(jī)制
  •              Web資源訪問控制
  •              業(yè)務(wù)方法調(diào)用訪問控制
  •             領(lǐng)域?qū)ο笤L問控制Access Control List(ACL)
  •             單點(diǎn)登錄(Central Authentication Service)
  •             X509認(rèn)證
  •             信道安全(Channel Security)管理等功能

簡單例子

1、創(chuàng)建web工程springSecurity3

2、把從spring網(wǎng)站下載的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 容器啟動監(jiān)聽器 --> 
  <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下面創(chuàng)建securityConfig.xml文件內(nèi)容如下:

<?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的權(quán)限 --> 
  <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/> 
  <!--訪問/**資源的用戶必須具有ROLE_USER的權(quán)限 --> 
<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中創(chuàng)建login.jsp內(nèi)容如下:

<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中創(chuàng)建accessDenied.jsp,

<body> 
 您的訪問被拒絕,無權(quán)訪問該資源!<br> 
</body> 

  創(chuàng)建admin.jsp內(nèi)容如下:

<body> 
歡迎來到管理員頁面. <br> 
</body> 

 修改index.jsp內(nèi)容如下:

<body> 
    這是首頁,歡迎<sec:authentication property="name"/>!<br> 
  <a href="admin.jsp" rel="external nofollow" >進(jìn)入admin頁面</a> 
  <a href="other.jsp" rel="external nofollow" >進(jìn)入其它頁面</a> 
 
 </body>

好了,部署項(xiàng)目,并訪問index.jsp.

用戶名就是剛才部署的那個用戶名。什么?忘了。那好吧,我再給你指出來

<user name="john" password="john" authorities="ROLE_USER" />
 <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> 

權(quán)限不同訪問的頁面就不同??梢栽囋嚨?nbsp;

以上就是JSP 開發(fā)中Spring Security 的實(shí)例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論