41. 安全命名空间

本附录提供了对安全性名称空间中可用元素的引用以及它们创建的基础 Bean 的信息(假定您了解各个类以及它们如何协同工作-您可以在项目 Javadoc 和本文档的其他地方找到更多信息.)。如果您以前没有使用过命名空间,请阅读有关命名空间配置的introductory chapter,因为这是对此处信息的补充。建议在编辑基于模式的配置时使用高质量的 XML 编辑器,因为这将提供有关哪些元素和属性可用的上下文信息,以及解释其用途的 Comments。名称空间以RELAX NG Compact 格式编写,然后转换为 XSD 模式。如果您熟悉此格式,则不妨直接检查schema file

41.1 Web 应用程序安全性

41.1.1 <debug>

启用 Spring Security 调试基础结构。这将提供人类可读的(多行)调试信息,以监视进入安全过滤器的请求。这可能包括敏感信息,例如请求参数或 Headers,并且仅应在开发环境中使用。

41.1.2 <http>

如果在应用程序中使用<http>元素,则会创建一个名为“ springSecurityFilterChain”的FilterChainProxy bean,并且该元素中的配置用于在FilterChainProxy中构建过滤器链。从 Spring Security 3.1 开始,其他http元素可用于添加额外的过滤器链[22]。某些核心过滤器始终在过滤器链中创建,而其他核心过滤器将根据存在的属性和子元素添加到堆栈中。标准过滤器的位置是固定的(请参见名称空间介绍中的过滤器 Order 表),当用户必须在FilterChainProxy bean 中显式配置过滤器链时,消除了以前版本框架中常见的错误源。当然,如果需要完全控制配置,您仍然可以这样做。

所有需要引用AuthenticationManager的过滤器都将自动注入由名称空间配置创建的内部实例(有关AuthenticationManager的更多信息,请参见introductory chapter)。

每个<http>名称空间块始终创建SecurityContextPersistenceFilterExceptionTranslationFilterFilterSecurityInterceptor。这些是固定的,不能用替代方法替代。

<http> Attributes

<http>元素上的属性控制核心过滤器上的某些属性。

<http>的子元素

41.1.3 <access-denied-handler>

该元素允许您使用error-page属性为ExceptionTranslationFilter使用的默认AccessDeniedHandler设置errorPage属性,或者使用ref属性提供自己的实现。关于ExceptionTranslationFilter的部分将对此进行详细讨论。

<access-denied-handler>的父元素

<access-denied-handler> Attributes

41.1.4 <cors>

该元素允许配置CorsFilter。如果未指定CorsFilterCorsConfigurationSource并且 Spring MVC 在 Classpath 中,则将HandlerMappingIntrospector用作CorsConfigurationSource

<cors> Attributes

<cors>元素上的属性控制 headers 元素。

<cors>的父元素

41.1.5 <headers>

此元素允许配置要与响应一起发送的其他(安全)Headers。它可以轻松配置多个 Headers,还可以通过header元素设置自定义 Headers。其他信息,可以在参考的Security Headers部分中找到。

<headers> Attributes

<headers>元素上的属性控制 headers 元素。

<headers>的父元素

<headers>的子元素

41.1.6 <cache-control>

添加Cache-ControlPragmaExpiresHeaders,以确保浏览器不缓存您的安全页面。

<cache-control> Attributes

<cache-control>的父元素

41.1.7 <hsts>

启用后,会将Strict-Transport-SecurityHeaders 添加到任何安全请求的响应中。这允许服务器指示浏览器自动将 HTTPS 用于将来的请求。

<hsts> Attributes

<hsts>的父元素

41.1.8 <hpkp>

启用后,会将HTTP 的公钥固定扩展Headers 添加到任何安全请求的响应中。这允许 HTTPS 网站使用错误签发或欺诈性证书来抵制攻击者的冒名顶替。

<hpkp> Attributes

<hpkp>的父元素

41.1.9 <pins>

引脚列表

<pins>的子元素

41.1.10 <pin>

使用 base64 编码的 SPKI 指纹作为值并使用密码哈希算法作为属性来指定引脚

<pin> Attributes

<pin>的父元素

41.1.11 <content-security-policy>

启用后,将内容安全 Policy(CSP)Headers 添加到响应中。 CSP 是 Web 应用程序可以用来减轻内容注入漏洞(例如跨站点脚本(XSS))的机制。

<content-security-policy> Attributes

<content-security-policy>的父元素

41.1.12 <referrer-policy>

启用后,将Referrer PolicyHeaders 添加到响应中。

<referrer-policy> Attributes

<referrer-policy>的父元素

41.1.13 <frame-options>

启用后,会将X-Frame-Options header添加到响应中,这使较新的浏览器可以进行一些安全检查并防止clickjacking攻击。

<frame-options> Attributes

换句话说,如果指定 DENY,则从其他站点加载时,不仅尝试在框架中加载页面失败,而且从同一站点加载时,尝试也会失败。另一方面,如果您指定 SAMEORIGIN,则仍可以在框架中使用页面,只要该页面中包含该页面的站点与服务该页面的站点相同。

<frame-options>的父元素

41.1.14 <xss-protection>

X-XSS-Protection header添加到响应中,以帮助防范反映的/ Type-1 跨站点脚本(XSS)攻击。这绝不是对 XSS 攻击的全面保护!

<xss-protection> Attributes

<xss-protection>的父元素

41.1.15 <content-type-options>

将带有 nosniff 值的 X-Content-Type-OptionsHeaders 添加到响应中。此disables MIME-sniffing用于 IE8 和 Chrome 扩展程序。

<content-type-options> Attributes

<content-type-options>的父元素

将其他 Headers 添加到响应中,需要同时指定名称和值。

<header-attributes> Attributes

<header>的父元素

41.1.17 <anonymous>

AnonymousAuthenticationFilter添加到堆栈中,并将AnonymousAuthenticationProvider添加到堆栈中。如果使用IS_AUTHENTICATED_ANONYMOUSLY属性,则为必需。

<anonymous>的父元素

<anonymous> Attributes

41.1.18 <csrf>

此元素将为应用程序添加跨站请求伪造(CSRF)保护。它还将默认的 RequestCache 更新为仅在成功身份验证后重播“ GET”请求。其他信息可以在参考的跨站请求伪造(CSRF)部分中找到。

<csrf>的父元素

<csrf> Attributes

41.1.19 <custom-filter>

该元素用于向过滤器链添加过滤器。它不会创建任何其他 bean,而是用于选择类型javax.servlet.Filter的 bean,该 bean 已在应用程序上下文中定义,并将其添加到 Spring Security 维护的过滤器链中的特定位置。完整的详细信息可以在namespace chapter中找到。

<custom-filter>的父元素

<custom-filter> Attributes

41.1.20 <expression-handler>

定义启用基于表达式的访问控制时将使用的SecurityExpressionHandler实例。如果未提供,则将使用默认实现(不支持 ACL)。

<expression-handler>的父元素

<expression-handler> Attributes

41.1.21 <form-login>

用于将UsernamePasswordAuthenticationFilter添加到过滤器堆栈,并将LoginUrlAuthenticationEntryPoint添加到应用程序上下文以按需提供身份验证。这将始终优先于其他由名称空间创建的入口点。如果未提供任何属性,则将在 URL“/login” [23]上自动生成一个登录页面。可以使用<form-login> Attributes来定制行为。

<form-login>的父元素

<form-login> Attributes

41.1.22 <http-basic>

BasicAuthenticationFilterBasicAuthenticationEntryPoint添加到配置中。如果未启用基于表单的登录,则后者将仅用作配置入口点。

<http-basic>的父元素

<http-basic> Attributes

41.1.23<http-firewall>元素

这是一个顶级元素,可用于将HttpFirewall的自定义实现注入由名称空间创建的FilterChainProxy。默认实现应适用于大多数应用程序。

<http-firewall> Attributes

41.1.24 <intercept-url>

此元素用于定义应用程序感兴趣的 URL 模式集,并配置应如何处理它们。它用于构造FilterSecurityInterceptor使用的FilterInvocationSecurityMetadataSource。例如,如果需要通过 HTTPS 访问特定的 URL,它还负责配置ChannelProcessingFilter。当将指定的模式与传入的请求进行匹配时,将按照声明元素的 Sequences 进行匹配。因此,最具体的模式应该放在首位,最一般的模式应该放在最后。

<intercept-url>的父元素

<intercept-url> Attributes

Note

该属性对filter-security-metadata-source无效

如果添加了<port-mappings>配置,则SecureChannelProcessorInsecureChannelProcessor bean 将使用它来确定用于重定向到 HTTP/HTTPS 的端口。

Note

该属性对filter-security-metadata-source无效

Note

该属性对filter-security-metadata-source无效

41.1.25 <jee>

将 J2eePreAuthenticatedProcessingFilter 添加到过滤器链以提供与容器身份验证的集成。

<jee>的父元素

<jee> Attributes

41.1.26 <logout>

LogoutFilter添加到过滤器堆栈。这是使用SecurityContextLogoutHandler配置的。

<logout>的父元素

<logout> Attributes

设置此属性将为SessionManagementFilter注入配置了属性值的SimpleRedirectInvalidSessionStrategy。提交无效的会话 ID 后,将调用该策略,并重定向到配置的 URL。

41.1.27 <openid-login>

<form-login>类似,并且具有相同的属性。 login-processing-url的默认值为“/login/openid”。 OpenIDAuthenticationFilterOpenIDAuthenticationProvider将被注册。后者需要引用UserDetailsService。同样,这可以由id使用user-service-ref属性指定,或者将自动位于应用程序上下文中。

<openid-login>的父元素

<openid-login> Attributes

<openid-login>的子元素

41.1.28 <attribute-exchange>

attribute-exchange元素定义应从身份提供者请求的属性列表。可以在名称空间配置一章的OpenID Support部分中找到一个示例。可以使用多个,在这种情况下,每个属性都必须具有identifier-match属性,该属性包含与提供的 OpenID 标识符匹配的正则表达式。这允许从不同的提供程序(Google,Yahoo 等)获取不同的属性列表。

<attribute-exchange>的父元素

<attribute-exchange> Attributes

<attribute-exchange>的子元素

41.1.29 <openid-attribute>

制作 OpenID AX Fetch Request时使用的属性

<openid-attribute>的父元素

<openid-attribute> Attributes

41.1.30 <port-mappings>

默认情况下,实例PortMapperImpl将添加到配置中,用于重定向到安全和不安全的 URL。可以选择使用此元素来覆盖该类定义的默认 Map。每个子<port-mapping>元素定义一对 HTTP:HTTPS 端口。默认 Map 为 80:443 和 8080:8443. namespace introduction中提供了覆盖这些示例的示例。

<port-mappings>的父元素

<port-mappings>的子元素

41.1.31 <port-mapping>

提供一种在强制重定向时将 http 端口 Map 到 https 端口的方法。

<port-mapping>的父元素

<port-mapping> Attributes

41.1.32 <remember-me>

RememberMeAuthenticationFilter添加到堆栈中。依次使用TokenBasedRememberMeServicesPersistentTokenBasedRememberMeServices或由用户指定的 Bean 来实现RememberMeServices,具体取决于属性设置。

<remember-me>的父元素

<remember-me> Attributes

41.1.33<request-cache>元素

设置RequestCache实例,ExceptionTranslationFilter将在调用AuthenticationEntryPoint之前将其用于存储请求信息。

<request-cache>的父元素

<request-cache> Attributes

41.1.34 <session-management>

与会话 Management 相关的功能是通过在过滤器堆栈中添加SessionManagementFilter来实现的。

<session-management>的父元素

<session-management> Attributes

如果启用了会话固定保护,则为SessionManagementFilter注入适当配置的DefaultSessionAuthenticationStrategy。有关更多详细信息,请参见此类的 Javadoc。

<session-management>的子元素

41.1.35 <concurrency-control>

增加了对并发会话控制的支持,从而可以限制用户可以拥有的活动会话的数量。将创建一个ConcurrentSessionFilter,并将一个ConcurrentSessionControlAuthenticationStrategySessionManagementFilter一起使用。如果已声明form-login元素,则策略对象也将注入到创建的身份验证过滤器中。将创建SessionRegistry的实例(除非用户希望使用自定义 bean,否则为SessionRegistryImpl实例)供策略使用。

<concurrency-control>的父元素

<concurrency-control> Attributes

41.1.36 <x509>

添加了对 X.509 身份验证的支持。 X509AuthenticationFilter将添加到堆栈中,并且将创建Http403ForbiddenEntryPoint bean。仅当没有其他身份验证机制在使用时才使用后者(其唯一功能是返回 HTTP 403 错误代码)。还将创建一个PreAuthenticatedAuthenticationProvider,将PreAuthenticatedAuthenticationProvider的用户权限加载委托给UserDetailsService

<x509>的父元素

<x509> Attributes

41.1.37 <filter-chain-map>

用于通过 FilterChainMap 显式配置 FilterChainProxy 实例

<filter-chain-map> Attributes

<filter-chain-map>的子元素

41.1.38 <filter-chain>

用于定义一个特定的 URL 模式和适用于与该模式匹配的 URL 的过滤器列表。当将多个过滤器链元素组合到一个列表中以配置 FilterChainProxy 时,必须将最特定的模式放在列表的顶部,而将最常规的模式放在底部。

<filter-chain>的父元素

<filter-chain> Attributes

41.1.39 <filter-security-metadata-source>

用于显式配置 FilterSecurityMetadataSource bean 与 FilterSecurityInterceptor 一起使用。通常仅在显式配置 FilterChainProxy 而不是使用元素时才需要。使用的拦截 URL 元素应仅包含模式,方法和访问属性。其他任何情况都将导致配置错误。

<filter-security-metadata-source> Attributes

<filter-security-metadata-source>的子元素

41.2 WebSocket 安全

Spring Security 4.0 提供了对消息授权的支持。一个有用的具体示例是在基于 WebSocket 的应用程序中提供授权。

41.2.1 <websocket-message-broker>

websocket-message-broker 元素具有两种不同的模式。如果未指定[email protected],它将执行以下操作:

如果需要其他控制,则可以指定 ID,并将 ChannelSecurityInterceptor 分配给指定的 ID。然后,可以手动完成与 Spring 的消息传递基础结构的所有连接。这比较麻烦,但是可以更好地控制配置。

<websocket-message-broker> Attributes

<websocket-message-broker>的子元素

41.2.2 <intercept-message>

定义消息的授权规则。

<intercept-message>的父元素

<intercept-message> Attributes

41.3 身份验证服务

在 Spring Security 3.0 之前,AuthenticationManager是在内部自动注册的。现在,您必须使用<authentication-manager>元素显式注册一个。这将创建 Spring Security 的ProviderManager类的实例,该实例需要配置一个或多个AuthenticationProvider实例的列表。这些可以使用命名空间提供的语法元素创建,也可以是标准 Bean 定义,并使用authentication-provider元素标记为要添加到列表中。

41.3.1 <authentication-manager>

每个使用名称空间的 Spring Security 应用程序都必须在某处包含此元素。它负责注册为应用程序提供身份验证服务的AuthenticationManager。所有创建AuthenticationProvider实例的元素都应该是该元素的子元素。

<authentication-manager> Attributes

<authentication-manager>的子元素

41.3.2 <authentication-provider>

除非与ref属性一起使用,否则此元素是配置DaoAuthenticationProvider的简写。 DaoAuthenticationProviderUserDetailsService加载用户信息,并将用户名/密码组合与登录时提供的值进行比较。可以通过使用可用的命名空间元素(jdbc-user-service或通过使用user-service-ref属性指向应用程序上下文中其他位置定义的 bean)来定义UserDetailsService实例。您可以在namespace introduction中找到这些变化的示例。

<authentication-provider>的父元素

<authentication-provider> Attributes

如果您已经编写了自己的AuthenticationProvider实现(或者出于某种原因想要将 Spring Security 自己的实现之一配置为传统 Bean,则可以使用以下语法将其添加到ProviderManager的内部列表中:

<security:authentication-manager>
<security:authentication-provider ref="myAuthenticationProvider" />
</security:authentication-manager>
<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider"/>

<authentication-provider>的子元素

41.3.3 <jdbc-user-service>

导致创建基于 JDBC 的 UserDetailsService。

<jdbc-user-service> Attributes

默认是

select username, authority from authorities where username = ?

select
g.id, g.group_name, ga.authority
from
groups g, group_members gm, group_authorities ga
where
gm.username = ? and g.id = ga.group_id and g.id = gm.group_id

select username, password, enabled from users where username = ?

41.3.4 <password-encoder>

可以将验证提供程序配置为使用namespace introduction中所述的密码编码器。这将导致为该 bean 注入适当的PasswordEncoder实例,并可能随附一个随附的SaltSource bean,以提供用于散列的盐值。

<password-encoder>的父元素

<password-encoder> Attributes

<password-encoder>的子元素

41.3.5 <salt-source>

密码加盐策略。可以使用系统范围的常量或 UserDetails 对象的属性。

<salt-source>的父元素

<salt-source> Attributes

41.3.6 <user-service>

从属性文件或“用户”子元素列表创建内存 UserDetailsService。用户名在内部会转换为小写形式,以允许不区分大小写的查找,因此如果需要区分大小写,则不应使用此名称。

<user-service> Attributes

username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]

<user-service>的子元素

41.3.7 <user>

代表应用程序中的用户。

<user>的父元素

<user> Attributes

41.4 方法安全性

41.4.1 <global-method-security>

该元素是添加对 Spring Security bean 上的安全方法的支持的主要方法。可以使用 AspectJ 语法通过使用 Comments(在接口或类级别定义)或将一组切入点定义为子元素来保护方法。

<global-method-security> Attributes

重要的是要注意,AspectJ 遵循 Java 的规则,即不继承接口上的 Comments。这意味着在接口上定义安全性 Comments 的方法将不安全。相反,在使用 AspectJ 时,必须在类上放置 Security 注解。

<global-method-security>的子元素

41.4.2 <after-invocation-provider>

此元素可用于装饰AfterInvocationProvider,以供<global-method-security>名称空间维护的安全拦截器使用。您可以在global-method-security元素中定义零个或多个,每个都具有ref属性,该属性指向应用程序上下文中的AfterInvocationProvider bean 实例。

<after-invocation-provider>的父元素

<after-invocation-provider> Attributes

41.4.3 <pre-post-annotation-handling>

允许完全替换基于默认表达式的机制来处理 Spring Security 的调用前后 Comments(@ PreFilter,@ PreAuthorize,@ PostFilter,@ PostAuthorize)。仅在启用这些 Comments 的情况下适用。

<pre-post-annotation-handling>的父元素

<pre-post-annotation-handling>的子元素

41.4.4 <invocation-attribute-factory>

定义 PrePostInvocationAttributeFactory 实例,该实例用于从带 Comments 的方法中生成调用前后的元数据。

<invocation-attribute-factory>的父元素

<invocation-attribute-factory> Attributes

41.4.5 <post-invocation-advice>

使用 ref 作为\ 元素的PostInvocationAuthorizationAdvice自定义PostInvocationAdviceProvider

<post-invocation-advice>的父元素

<post-invocation-advice> Attributes

41.4.6 <pre-invocation-advice>

使用 ref 作为\ 元素的PreInvocationAuthorizationAdviceVoter自定义PreInvocationAuthorizationAdviceVoter

<pre-invocation-advice>的父元素

<pre-invocation-advice> Attributes

41.4.7 使用以下方法保护方法

<protect-pointcut>而不是使用@SecuredComments 在单个方法或类的基础上定义安全属性,而是可以使用<protect-pointcut>元素跨服务层中的整个方法和接口集定义跨领域安全约束。您可以在namespace introduction中找到示例。

<protect-pointcut>的父元素

<protect-pointcut> Attributes

41.4.8 <intercept-methods>

可以在 bean 定义内使用,以向 bean 添加安全拦截器并为 bean 的方法设置访问配置属性

<intercept-methods> Attributes

<intercept-methods>的子元素

41.4.9 <method-security-metadata-source>

创建一个 MethodSecurityMetadataSource 实例

<method-security-metadata-source> Attributes

<method-security-metadata-source>的子元素

41.4.10 <protect>

定义一个受保护的方法以及适用于该方法的访问控制配置属性。强烈建议您不要将“保护”声明与“全局方法安全性”提供的任何服务混合使用。

<protect>的父元素

<protect> Attributes

41.5 LDAP 命名空间选项

自己的章节中详细介绍了 LDAP。我们将在这里扩展它,并提供一些有关名称空间选项如何 Map 到 Spring Bean 的解释。 LDAP 实现广泛使用 Spring LDAP,因此熟悉该项目的 API 可能会有用。

41.5.1 使用以下命令定义 LDAP 服务器

<ldap-server>元素此元素设置供其他 LDAP Bean 使用的 Spring LDAP ContextSource,以定义 LDAP 服务器的位置以及用于与其连接的其他信息(例如用户名和密码,如果不允许匿名访问)。它还可以用于创建嵌入式服务器以进行测试。 LDAP chapter涵盖了这两个选项的语法详细信息。实际的ContextSource实现是DefaultSpringSecurityContextSource,它扩展了 Spring LDAP 的LdapContextSource类。 manager-dnmanager-password属性分别 Map 到后者的userDnpassword属性。

如果在应用程序上下文中仅定义了一个服务器,则其他 LDAP 名称空间定义的 Bean 将自动使用它。否则,您可以为元素赋予“ id”属性,并使用server-ref属性从其他名称空间 bean 引用该元素。如果要在其他传统的 Spring Bean 中使用它,则实际上是ContextSource实例的 Bean id

<ldap-server> Attributes

41.5.2 <ldap-authentication-provider>

此元素是创建LdapAuthenticationProvider实例的简写。默认情况下,它将使用BindAuthenticator实例和DefaultAuthoritiesPopulator配置。与所有名称空间身份验证提供程序一样,它必须作为authentication-provider元素的子元素包括在内。

<ldap-authentication-provider>的父元素

<ldap-authentication-provider> Attributes

如果需要执行搜索以在目录中找到用户,则可以设置这些属性来控制搜索。 BindAuthenticator将配置为FilterBasedLdapUserSearch,并且属性值直接 Map 到该 bean 的构造函数的前两个参数。如果未设置这些属性,并且没有提供user-dn-pattern作为替代,那么将使用user-search-filter="(uid={0})"user-search-base=""的默认搜索值。

如果需要执行搜索以在目录中找到用户,则可以设置这些属性来控制搜索。 BindAuthenticator将配置为FilterBasedLdapUserSearch,并且属性值直接 Map 到该 bean 的构造函数的前两个参数。如果未设置这些属性,并且没有提供user-dn-pattern作为替代,那么将使用user-search-filter="(uid={0})"user-search-base=""的默认搜索值。

<ldap-authentication-provider>的子元素

41.5.3 <password-compare>

这用作<ldap-provider>的子元素,并将身份验证策略从BindAuthenticator切换到PasswordComparisonAuthenticator

<password-compare>的父元素

<password-compare> Attributes

<password-compare>的子元素

41.5.4 <ldap-user-service>

该元素配置 LDAP UserDetailsService。所使用的类是LdapUserDetailsService,它是FilterBasedLdapUserSearchDefaultLdapAuthoritiesPopulator的组合。它支持的属性与<ldap-provider>中的用法相同。

<ldap-user-service> Attributes


[22]有关如何从web.xml设置 Map 的信息,请参见introductory chapter

[23]实际上,此功能只是为了方便起见而提供的,并非供生产使用(已选择一种查看技术,可用于呈现自定义的登录页面)。 DefaultLoginPageGeneratingFilter类负责呈现登录页面,并将在需要时为普通表单登录和/或 OpenID 提供登录表单。

[24]这不会影响PersistentTokenBasedRememberMeServices的使用,令牌存储在服务器端。

首页