一
您可以在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
接口的类对密码进行编码和验证。 这只有两种方法: encodePassword
和isPasswordValid
。 它的许多实现包括:
- 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