日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security可以做的十件事

發(fā)布時間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security可以做的十件事 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

您可以在Spring XML配置文件中指定您選擇的授權提供者。 您可以通過配置Spring的http://www.springframework.org/schema/security/spring-security-3.1.xsd模式中定義的authentication-manager來實現(xiàn)。 簡化的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>

這意味著,例如,您可以使用任何數(shù)量的身份驗證提供程序,包括基本身份驗證和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文件中配置授權規(guī)則。 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>


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

  • 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>

…其次,您可以針對網(wǎng)址進行授權

<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的內(nèi)部注釋執(zhí)行方法級別的授權

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

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

要啟用@PreAuthorize(...)和@PostAuthorize(...)請將以下內(nèi)容添加到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請將以下內(nèi)容添加到您的Spring配置文件中:

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


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

<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(輕型目錄訪問協(xié)議)服務器對用戶進行身份驗證。 下面顯示的簡化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(輕型目錄訪問協(xié)議)服務器對用戶進行身份驗證。 下面顯示的簡化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,則將啟動嵌入式服務器,監(jiān)聽提供的端口號。 該端口是可選的,默認為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。 例如,如果要確保在發(fā)送密碼之前始終對密碼進行加密,則可以將此簡化的XML添加到配置中:

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

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

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

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

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

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

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

總結

以上是生活随笔為你收集整理的Spring Security可以做的十件事的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。