Spring Security可以做的十件事

您可以在Spring XML配置文件中指定您選擇的授權提供者。 您可以通過配置Spring的http://www.springframework.org/schema/security/spring-security-3.1.xsd模式中定義的authentication-manager來實現。 簡化的authentication-manager元素定義看起來像這樣:

<xs:element name='authentication-manager'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='authentication-provider'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element ref='security:any-user-service'/><xs:element name='password-encoder'>...</xs:element></xs:choice><xs:attributeGroup ref='security:ap.attlist'/></xs:complexType></xs:element><!-- This is BIG --><xs:element name='ldap-authentication-provider'>...</xs:element></xs:choice><xs:attributeGroup ref='security:authman.attlist'/></xs:complexType>
</xs:element>

這意味著,例如,您可以使用任何數量的身份驗證提供程序,包括基本身份驗證和JDBC身份驗證,如下面的代碼段所示:

<authentication-manager alias='authenticationManager'><authentication-provider><user-service><user authorities='ROLE_GUEST' name='guest' password=''/></user-service></authentication-provider><authentication-provider><jdbc-user-service data-source-ref='dataSource'/></authentication-provider></authentication-manager>


您可以使用intercept-url元素將URL鏈接到用戶角色,從而在Spring XML文件中配置授權規則。 intercept-url元素是http元素的子元素,其簡短定義如下所示:

<xs:element name='http'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='intercept-url'><xs:complexType><xs:attributeGroup ref='security:intercept-url.attlist'/></xs:complexType></xs:element><!-- Details omitted for clarity --><xs:element name='access-denied-handler'>...</xs:element><xs:element name='form-login'>...</xs:element><xs:element name='openid-login'>...</xs:element><xs:element name='x509'>...</xs:element><xs:element ref='security:jee'/><xs:element name='http-basic'>...</xs:element><xs:element name='logout'>...</xs:element><xs:element name='session-management'>...</xs:element><xs:element name='remember-me'>...</xs:element><xs:element name='anonymous'>...</xs:element><xs:element name='port-mappings'>...</xs:element><xs:element ref='security:custom-filter'/><xs:element ref='security:request-cache'/><xs:element name='expression-handler'>...</xs:element></xs:choice><xs:attributeGroup ref='security:http.attlist'/></xs:complexType>
</xs:element>

用法示例:

<security:http><security:intercept-url pattern='/admin/**' access='hasRole('ROLE_ADMIN')'/><security:intercept-url pattern='/account/**' access='hasRole('ROLE_USER')' /><security:intercept-url pattern='/**' access='hasRole('ROLE_ANONYMOUS')' /><!-- other elements removed for clarity -->
</security:http>


您可以使用幾個實現Spring的org.springframework.security.authentication.encoding.PasswordEncoder接口的類對密碼進行編碼和驗證。 這只有兩種方法: encodePasswordisPasswordValid 。 它的許多實現包括:

  • BaseDigestPasswordEncoder
  • BasePasswordEncoder
  • LdapShaPasswordEncoder
  • Md4PasswordEncoder,
  • Md5PasswordEncoder
  • MessageDigestPasswordEncoder
  • MessageDigestPasswordEncoder
  • PlaintextPasswordEncoder
  • ShaPasswordEncoder


四個

您可以使用Spring Security的標簽庫來限制對頁面元素的訪問。 要使用此庫,您需要在JSP中包含以下taglib定義:

<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags' %>

taglib包含三個有用的標簽:

  • 授權
  • 認證方式
  • 訪問控制表

有用的似乎是authorize標記,以Spring文檔中的示例為例,可以以兩種方式使用它。 首先,您可以授權角色:

<sec:authorize access='hasRole('supervisor')'>
This content will only be visible to users who have
the 'supervisor' authority in their list of <tt>GrantedAuthority</tt>s.
</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>

指定的URL必須與第2項中所述的intercept-url標記配合使用。

您可以使用Spring的內部注釋執行方法級別的授權

  • @PreAuthorize('spEL expression')
  • @PostAuthorize('spEL expression')
  • @Secure

spEL表達式可以是任何東西,但通常類似于: hasRole('ROLE_USER')

要啟用@PreAuthorize(...)@PostAuthorize(...)請將以下內容添加到XML配置文件中:

<global-method-security pre-post-annotations='enabled' />

如以下示例所示,使用@PreAuthorize(...)

<span class='java0'><br /></span><span class='java16'>@PreAuthorize</span><span class='java8'>(</span><span class='java5'>'hasRole('ROLE_ADMIN')'</span><span class='java8'>) <br /></span><span class='java4'>public </span><span class='java9'>void </span><span class='java10'>deleteUser</span><span class='java8'>(</span><span class='java10'>String username</span><span class='java8'>)</span><span class='java10'>;<br />
</span>

要啟用@Secure請將以下內容添加到您的Spring配置文件中:

<global-method-security pre-post-annotations='enabled' />


您可以通過將以下內容添加到Spring配置文件中,使用Spring的JSR-250實現來執行方法級安全性:

<global-method-security jsr250-annotations=”enabled”/>

的JSR-250安全注解是一個子集的JSR-250注解和包括:

  • @RolesAllowed({“ROLE_USER”,”ROLE_ADMIN”})
  • @PermitAll
  • @DenyAll

使用時,JSR-250注釋看起來像這樣:

@RolesAllowed({"ROLE_ADMIN","ROLE_USER"})
public void deleteUser(String username);


您可以通過幾個簡單的步驟將Spring Security與OpenID身份驗證集成。 其中的第一步是編寫一個簡單的JSP表單,其中將操作值設置為j_spring_openid_security_check ,該表單的最小值看起來像這樣:

<form action='j_spring-openid-security-check' method='post'><label for='openid_idenifier'>Login</label>: <input id='openid_identifier' name='openid_identifier' type='text'/><input type='submit' value='Login' />
</form>

下一步是將openid-login元素添加到http

<xs:element name='http'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='openid-login'><xs:annotation><xs:documentation>Sets up form login for authentication with anOpen ID identity</xs:documentation></xs:annotation><xs:complexType><xs:sequence><xs:element minOccurs='0' maxOccurs='unbounded'ref='security:attribute-exchange' /></xs:sequence><xs:attributeGroup ref='security:form-login.attlist' /><xs:attribute name='user-service-ref' type='xs:token'><xs:annotation><xs:documentation>A reference to a user-service (orUserDetailsService bean) Id</xs:documentation></xs:annotation></xs:attribute></xs:complexType></xs:element><!-- Other elements omitted for clarity --></xs:choice></xs:complexType>
</xs:element>

由于所有openid-login子元素都是可選的,因此啟用OpenID的最簡單方法是編寫:

<http auto-config='true'><openid-login/><!-- other tags and attributes omitted for clarity -->
</http>

最后,您需要將spring-security-openid.jar到您的項目中。

您可以將應用程序配置為使用XML配置通過嵌入式LDAP(輕型目錄訪問協議)服務器對用戶進行身份驗證。 下面顯示的簡化XML模式對此進行了描述:

<xs:element name='ldap-server'><xs:complexType><xs:attributeGroup ref='security:ldap-server.attlist' /></xs:complexType>
</xs:element>
<xs:attributeGroup name='ldap-server.attlist'><xs:attribute name='id' type='xs:token'><xs:annotation><xs:documentation>A bean identifier, used for referring to the bean elsewhere in the context.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='port' type='xs:positiveInteger'/><xs:attribute name='ldif' type='xs:string'><xs:annotation><xs:documentation>Explicitly specifies an ldif file resource to loadinto an embedded LDAPserver. The default is classpath*:*.ldiff</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='root' type='xs:string'><xs:annotation><xs:documentation>Optional root suffix for the embedded LDAP server. Default is'dc=springframework,dc=org'</xs:documentation></xs:annotation></xs:attribute>
</xs:attributeGroup>

LDIF文件 (LDIF代表LDAP交換格式)是一種純文本文件格式,用于描述一組LDAP記錄。

ldap-server元素用法的一個示例是:

<ldap-server ldif='classpath:my-ldif-file.ldif' id='localserver' />

要使用Spring Security LDAP集成,請記住在項目的POM.XML中包含spring-security-ldap.jar jar。

您可以將應用程序配置為使用XML配置通過遠程LDAP(輕型目錄訪問協議)服務器對用戶進行身份驗證。 下面顯示的簡化XML模式對此進行了描述:

<xs:element name='ldap-server'><xs:complexType><xs:attributeGroup ref='security:ldap-server.attlist' /></xs:complexType>
</xs:element>
<xs:attributeGroup name='ldap-server.attlist'><xs:attribute name='id' type='xs:token'><xs:annotation><xs:documentation>A bean identifier, used for referring to the bean elsewhere in the context.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='url' type='xs:token'/><xs:attribute name='port' type='xs:positiveInteger'/><xs:attribute name='manager-dn' type='xs:string'><xs:annotation><xs:documentation>Username (DN) of the 'manager' user identity which will be used toauthenticate to a (non-embedded) LDAP server. If omitted, anonymousaccess will be used.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='manager-password' type='xs:string'><xs:annotation><xs:documentation>The password for the manager DN. This is requiredif the manager-dn is specified.</xs:documentation></xs:annotation></xs:attribute>
</xs:attributeGroup>

文檔指出ldap-server元素“定義LDAP服務器位置或啟動嵌入式服務器。 URL指示遠程服務器的位置。 如果未提供url,則將啟動嵌入式服務器,監聽提供的端口號。 該端口是可選的,默認為33389。將使用提供的ID為服務器注冊Spring LDAP ContextSource bean。

這是一個非常簡單的配置示例:

<ldap-server url='ldap://myServer/dc=captaindebug,dc=com:389' id='ldapExternal' manager-dn='uid=admin,ou=users,ou=systems' manager-password='s3cret'/>

配置服務器后,還需要配置LDAP身份驗證提供程序。 似乎有幾種方法可以做到這一點,但它并不是那么簡單,所以以后可能會更多……

您可以添加
Spring Security Config的<intercept-url />元素requires-channel='https'屬性強制所有匹配的URL使用HTTPS。 例如,如果要確保在發送密碼之前始終對密碼進行加密,則可以將此簡化的XML添加到配置中:

<http auto-config='true' use-expressions='true'><intercept-url pattern='/login' requires-channel='https'/><!-- Other attributes and elements omitted -->    
</https>

這里還有另外幾件事要做,但稍后會做更多……

您可能已經注意到,我已經使用Spring Security XML模式文件( http://www.springframework.org/schema/security/spring-security-3.1.xsd )來解釋清單中的某些功能。可以使用Spring Security。 這是因為我一直將Spring XSD視為Spring所有事物的確定參考點。 2011年11月,我在Spring的JSR-250的@PostConstruct Annotation上寫了一個博客,其中包含一個錯誤(是的,的確確實發生了),Spring的Chris Beams – @CBeams正確地指出了這一點,他在JavaLobby上發表了評論。此博客的版本 。 我決定檢查架構,發現我們倆都錯了(盡管我比克里斯錯了很多)–就我所知,《 機長調試 》一書現在是正確的。

應用程序安全性是一個相當復雜的主題,如果您要深入研究它,那么我建議您獲得Peter Mularien的Spring Security 3的副本- Spring的Guys也建議您這樣做。

最后,如果有一個關于Spring Security的關鍵想法值得欣賞,那就是,作為應用程序的附加功能,它提供了非常豐富的安全功能集。 因此,您應該嘗試讓Spring Security盡可能多地處理應用程序的安全細節,而不是深入研究和不必要地編寫自己的代碼。

參考: Captain Debug's Blog博客上的JCG合作伙伴 Roger Hughes提供的Spring Security可以做的十件事 。

翻譯自: https://www.javacodegeeks.com/2012/11/ten-things-you-can-do-with-spring-security.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/370334.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/370334.shtml
英文地址,請注明出處:http://en.pswp.cn/news/370334.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python編寫自定義函數判斷n1-n2范圍內的素數_【每日道代碼題001】- PYTHON基礎復習...

問題001-1&#xff1a;請對輸入三個整數a,b,c,判斷能否以它們為三個邊長構成三角形。若能&#xff0c;輸出YES和面積&#xff0c;否則輸出NOa float(input())b float(input())c float(input())if a > 0 and b > 0 and c > 0: #判斷邊長是否為正if (a b > c) an…

php繪制一個三角形,如何利用css或html5畫出一個三角形?兩種不同的制作三角形方法(代碼實例)...

我們在平時的前端開發的時候&#xff0c;有時候是需要一些小圖形來豐富一下頁面效果&#xff0c;比如&#xff1a;下拉列表的倒三角圖形。那么這樣的一個三角形是如何制作出來的&#xff0c;本章給大家介紹如何利用css或html畫出一個三角形&#xff1f;兩種不同的制作三角形方法…

課時53.video標簽(掌握)

這節課來學習一下html5中新增的標簽&#xff0c;我們先來看一下&#xff0c;html5中新增了哪些標簽&#xff1f; 打開W3school的網頁&#xff0c;點擊參考手冊中的HTML/HTML5標簽&#xff0c;有一個按字母順序排列的標簽&#xff0c;但凡標簽后面帶有5標記的&#xff0c;都是h…

Date函數基礎知識整理

Date類型&#xff1a;1.Date.parse()接收一個表示日期的字符串參數&#xff0c;然后再根據這個字符串返回響應的日期的毫秒數&#xff1b;如&#xff1a;創建一個日期&#xff1a; 1 <script> 2 // var someDatenew Date(May 25,2004); 3 // console.log(someDate);//Tue…

Google Guava –與Monitor同步

Google Guava項目是每個Java開發人員都應該熟悉的庫的集合。 Guava庫涵蓋I / O&#xff0c;集合&#xff0c;字符串操作和并發性。 在這篇文章中&#xff0c;我將介紹Monitor類。 Monitor是一種同步構造&#xff0c;可以在使用ReentrantLock的任何地方使用。 在任何時候&#x…

yaf 重寫index.php,php框架Yaf路由重寫實例代碼

通常為了友好的URL格式&#xff0c;會進行站點URL的重寫&#xff0c;可以在webserver(Nginx)的配置中進行rewrite&#xff0c;也可在在程序端進行&#xff0c;本文主要和大家介紹php框架Yaf路由重寫&#xff0c;給大家做個參考&#xff0c;希望能幫助到大家。以下使用Yaf框架進…

python類初始化導入庫_Python中optparser庫用法實例詳解

本文研究的主要是Python中optparser庫的相關內容&#xff0c;具體如下。一直以來對optparser不是特別的理解&#xff0c;今天就狠下心&#xff0c;靜下心研究了一下這個庫。當然了&#xff0c;不敢說理解的很到位&#xff0c;但是足以應付正常的使用了。廢話不多說&#xff0c;…

SQL--Chapter8--Working with Triggers and Transactions

Objectives:1.Implement triggers 2.Implement transactions 轉載于:https://www.cnblogs.com/Catherinezhilin/p/7979644.html

Canvas制作的下雨動畫

簡介 在codepen上看到一個Canvas做的下雨效果動畫&#xff0c;感覺蠻有意思的。就研究了下&#xff0c;這里來分享下&#xff0c;實現技巧。效果可以見下面的鏈接。 霓虹雨: http://codepen.io/natewiley/full/NNgqVJ/ 效果截圖&#xff1a; Canvas動畫基礎 大家都知道&…

在Eclipse中有效使用JUnit

最近&#xff0c;我被卷入了討論1和一些受感染的同伴2&#xff0c;他們關于我們如何在Eclipse IDE中使用JUnit 。 令人驚訝的是&#xff0c;對話帶來了并非所有人都知道的一些“技巧”。 這使我有了寫這篇文章的想法&#xff0c;總結了我們的演講。 誰知道–也許有人也有新事物…

jquery文件上傳控件 Uploadify

基于jquery的文件上傳控件&#xff0c;支持ajax無刷新上傳&#xff0c;多個文件同時上傳&#xff0c;上傳進行進度顯示&#xff0c;刪除已上傳文件。 要求使用jquery1.4或以上版本&#xff0c;flash player 9.0.24以上。 有兩個版本&#xff0c;一個用flash,一個是html5。html5…

imagick php 縮放,php使用imagick模塊實現圖片縮放、裁剪、壓縮示例

PHP 使用Imagick模塊 縮放&#xff0c;裁剪&#xff0c;壓縮圖片 包括gif圖片縮放 裁剪代碼如下:/*** 圖片裁剪* 裁剪規則&#xff1a;* 1. 高度為空或為零 按寬度縮放 高度自適應* 2. 寬度為空或為零 按高度縮放 寬度自適應* 3. 寬度&#xff0c;高度到不為空或為…

php實現第三方郵箱登錄_PHP實現用戶異地登錄提醒功能的方法

有時候你的網站賬號被盜或你在別處登錄操作后臺時&#xff0c;右下角會彈出提示信息&#xff0c;提醒你的賬號異地登錄&#xff0c;或者會被強制下線。對于這種安全性要求比較高的web網站&#xff0c;很多后臺管理都會做這種功能提醒。甄別自己的賬號是否被盜或者是否有另一個人…

課時47.datalist標簽(了解)

1.datalist標簽 作用&#xff1a;給輸入框綁定待選項 2.datalist格式&#xff1a; <datalist> <option>待選項內容</option> </datalist> 3.如何給輸入框綁定待選列表&#xff1f; 搞一個輸入框搞一個datalist列表給datalist列表標簽添加一個id給…

pandas.read_csv參數詳解

讀取CSV&#xff08;逗號分割&#xff09;文件到DataFrame也支持文件的部分導入和選擇迭代更多幫助參見&#xff1a;http://pandas.pydata.org/pandas-docs/stable/io.html參數&#xff1a;filepath_or_buffer : str&#xff0c;pathlib。str, pathlib.Path, py._path.local.Lo…

Gradle – Maven的觀點

正如我博客的讀者所知道的&#xff0c; 我有點像Maven迷 。 我從2007年8月左右開始使用Maven&#xff0c;從沒有回過頭。 但是&#xff0c;就像其他所有情況一樣&#xff0c;“變化是唯一不變的”。 現在這個領域還有其他參與者&#xff0c;Gradle看起來是最有前途的。 我決定試…

postgis安裝_從零開始,構建電子地圖網站:0_2_數據處理postgis

軟件安裝完&#xff0c;開始數據處理。從China Historical GIS下載一份數據。一、數據下載數據來源&#xff1a;China Historical GIS&#xff1a;https://sites.fas.harvard.edu/~chgis/data/chgis/v6/先下載一份時間序列數據&#xff1a;Download CHGIS V6 TIME SERIES Datah…

sar圖像去噪matlab,一種基于總曲率的SAR圖像變分去噪方法與流程

本發明屬于數字圖像處理技術領域&#xff0c;具體涉及一種基于總曲率的SAR圖像變分去噪方法。背景技術&#xff1a;&#xff1a;相干斑噪聲是合成孔徑雷達(Synthetic Aperture Radar&#xff0c;簡稱SAR)圖像的重要特征&#xff0c;嚴重影響SAR圖像的可解譯性。相干斑噪聲通常作…

Linux下用netstat查看網絡狀態、端口狀態

在linux一般使用netstat 來查看系統端口使用情況步。 netstat命令是一個監控TCP/IP網絡的非常有用的工具&#xff0c;它可以顯示路由表、實際的網絡連接以及每一個網絡接口設備的 netstat命令的功能是顯示網絡連接、路由表和網絡接口信息&#xff0c;可以讓用戶得知目…

課時2.瀏覽器和服務器(了解)

1.什么是瀏覽器&#xff1f; 瀏覽器就是由安裝在我們電腦上的一款軟件&#xff0c;QQ&#xff0c;百度影音等一樣&#xff0c;都是安裝在電腦上的一款軟件 那這些軟件之間由什么區別呢&#xff1f; 它們的區別就是它們的功能不太一樣&#xff0c;QQ是用來聊天的&#xff0c;…