5. Java Configuration
在spring 3.1中向Spring框架添加了对Java配置的常规支持。 自Spring Security 3.2以来,一直有Spring Security Java配置支持,使用户能够轻松地配置Spring Security而不使用任何XML。 如果你熟悉第6章,安全命名空间配置,那么你应该发现它和安全Java配置支持之间有几个相似之处。
Spring Security提供了许多以-j结尾的示例应用程序,演示了使用Spring Security Java配置。
5.1 Hello Web Security Java Configuration
第一步是创建我们的Spring Security Java配置。 配置创建一个名为springSecurityFilterChain的Servlet过滤器,它负责应用程序中的所有安全性(保护应用程序URL,验证提交的用户名和密码,重定向到登录表单等)。 你可以在下面找到Spring Security Java配置的最基本的例子:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
configureGlobal方法的名称不重要。但是,重要的是仅在用
@EnableWebSecurity
,@EnableGlobalMethodSecurity
或@EnableGlobalAuthentication
注释的类中配置AuthenticationManagerBuilder。否则会产生不可预测的结果。
真的没有太多的配置,但它做了很多。您可以在下面找到功能的摘要:
- 需要对应用程序中的每个网址进行身份验证
- 为您生成登录表单
- 允许具有Username用户和密码密码的用户使用基于表单的身份验证进行身份验证
- 允许用户注销
- CSRF 攻击 预防
- 会话 保护
-
安全头集成
- 用于 HTTP严格传输安全 的安全请求
- X-Content-Type-Options 集成
- 缓存控制(以后可以通过应用程序覆盖,允许缓存您的静态资源)
- X-XSS-Protection 集成
- X-Frame-Options 集成以帮助防止 Clickjacking
-
与以下Servlet API方法集成
5.1.1 AbstractSecurityWebApplicationInitializer
下一步是使用war注册springSecurityFilterChain
。 这可以在Java配置中使用Spring的WebApplicationInitializer
支持在Servlet 3.0+环境中完成。不奇怪,Spring Security提供了一个基类AbstractSecurityWebApplicationInitializer
,它将确保springSecurityFilterChain
为您注册。 我们使用AbstractSecurityWebApplicationInitializer
的方式取决于我们是否已经使用Spring,或者如果Spring Security是我们应用程序中唯一的Spring组件。
5.1.2 AbstractSecurityWebApplicationInitializer without Existing Spring
如果你不使用Spring或Spring MVC,你需要将WebSecurityConfig传入超类,以确保配置被选中。 你可以找到一个例子:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
SecurityWebApplicationInitializer
将做以下一些事情:
* 为应用程序中的每个URL自动注册springSecurityFilterChain Filter
* 添加一个加载 WebSecurityConfig 的ContextLoaderListener
.
5.1.3 AbstractSecurityWebApplicationInitializer with Spring MVC
如果我们在应用程序的其他地方使用Spring,我们可能已经有一个WebApplicationInitializer
加载我们的Spring配置。 如果我们使用以前的配置,我们会得到一个错误。 相反,我们应该使用现有的ApplicationContext
注册Spring Security。 例如,如果我们使用Spring MVC,我们的SecurityWebApplicationInitializer
将类似于以下内容:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
这将只是为您的应用程序中的每个URL注册springSecurityFilterChain
过滤器。 之后,我们将确保WebSecurityConfig
加载在我们现有的ApplicationInitializer
中。 例如,如果我们使用Spring MVC,它将被添加到getRootConfigClasses()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
5.2 HttpSecurity
到目前为止,我们的WebSecurityConfig
只包含有关如何验证我们的用户的信息。 Spring Security知道我们想要求所有用户进行身份验证? Spring Security知道我们要支持基于表单的身份验证? 其原因是WebSecurityConfigurerAdapter
在configure(HttpSecurity http)
方法中提供了一个默认配置,如下所示:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
上面的默认配置:
- 确保对我们的应用程序的任何请求需要用户进行身份验证
- 允许用户使用基于表单的登录进行身份验证
- 允许用户使用HTTP基本身份验证进行身份验证
您会注意到,此配置与XML命名空间配置非常相似:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
关闭XML标签的Java配置等同于使用和()方法表示,这允许我们继续配置父代。 如果你读的代码,这也有意义。 我想配置授权的请求并配置表单登录和配置HTTP基本认证。 但是,Java配置具有不同的默认URL和参数。 创建自定义登录页面时请记住这一点。 结果是我们的URL更加RESTful。 此外,它不是很明显,我们使用Spring Security有助于防止信息泄漏。 例如:
5.3 Java Configuration and Form Login
您可能想知道当提示登录时登录表单来自哪里,因为我们没有提到任何HTML文件或JSP。 由于Spring Security的默认配置没有为登录页面显式设置URL,Spring Security会根据启用的功能自动生成一个URL,并使用处理提交的登录的URL的标准值,用户将使用的默认目标URL 发送到登录后等等。 虽然自动生成的登录页面方便快速启动和运行,但大多数应用程序想要提供自己的登录页面。 为此,我们可以更新我们的配置,如下所示:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
更改配置以指定login页面
我们必须授予所有用户(即未经身份验证的用户)对我们登录页面的访问权限。 formLogin().permitAll()方法允许向所有用户授予与基于表单的登录相关联的所有URL的访问权限
使用JSP实现的用于当前配置的示例登录页面如下所示:
下面的登录页面代表我们当前的配置。如果某些默认设置不能满足我们的需要,我们可以轻松修改我们的配置。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
尝试对 /login 发送post请求以验证用户
如果查询参数 param.error 存在,则验证失败
如果查询参数 param.logout 存在,则注销成功
用户名必须以 username 为http请求参数
密码必须以 password 为http请求参数
这部分我们必须在第18.4.3节“包括CSRF令牌”中了解更多,请参阅第18章,跨站点请求伪造(CSRF)部分的参考
5.4 Authorize Requests
我们的示例只需要对用户进行身份验证,并且对我们的应用程序中的每个网址都这样做。我们可以通过向我们的http.authorizeRequests()
方法添加多个子节点来指定我们的URL的自定义要求。例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
http.authorizeRequests()
方法有多个子节点,每个匹配器都按它们声明的顺序考虑
我们指定了多个用户可以访问的网址格式。具体来说,如果网址以”/resources/”开头,等于”/signup”或等于”/about”,则任何用户都可以访问请求。
以”/admin/”开头的任何网址将被重新分配给具有角色”ROLE_ADMIN”的用户。你会注意到,由于我们调用了hasRole方法,我们不需要指定”ROLE_”前缀
以”/db/”开头的任何URL都要求用户同时拥有”ROLE_ADMIN”和”ROLE_DBA”。 你会注意到,由于我们使用hasRole表达式,我们不需要指定”ROLE_”前缀
任何尚未匹配的URL只需要对用户进行身份验证
5.5 Handling Logouts
使用WebSecurityConfigurerAdapter
时,会自动应用注销功能。 默认情况下,访问URL /logout
将通过以下方式记录用户:
- 使HTTP session无效
- 清除配置的任何RememberMe身份验证
- 清除
SecurityContextHolder
- 跳转至
/login?logout
与配置登录功能类似,您还可以使用各种选项进一步自定义您的注销要求:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
提供注销支持。这在使用WebSecurityConfigurerAdapter
时自动应用。
触发注销的URL(默认为/logout
)。如果启用CSRF保护(默认),则请求也必须是POST。有关信息,请参阅JavaDoc
发生注销后重定向到的URL。默认值为/login?logout
。有关信息,请参考JavaDoc。
让我们指定一个自定义LogoutSuccessHandler
。如果指定此参数,将忽略logoutSuccessUrl()。有关信息,请参阅JavaDoc。
指定在注销时是否使HttpSession
无效。 默认情况下是这样。 在封底下配置SecurityContextLogoutHandler
。 有关信息,请参阅JavaDoc。
添加LogoutHandler
。默认情况下,SecurityContextLogoutHandler
作为最后一个LogoutHandler
添加。
允许指定在注销成功时要删除的Cookie的名称。这是一个显式添加CookieClearingLogoutHandler
的快捷方式。
当然,注销也可以使用XML命名空间符号进行配置。有关更多详细信息,请参阅
Spring Security XML
命名空间部分中的注销元素的文档。
通常,为了定制注销功能,您可以添加LogoutHandler
和/
或LogoutSuccessHandler
实现。对于许多常见的情况,当使用流利的API时,这些处理程序应用在封面下
5.5.1 LogoutHandler
通常,LogoutHandler
实现指示能够参与注销处理的类。它们将被调用来执行必要的清理。因此,他们不应该抛出异常。提供了各种实现:
- PersistentTokenBasedRememberMeServices
- TokenBasedRememberMeServices
- CookieClearingLogoutHandler
- CsrfLogoutHandler
- SecurityContextLogoutHandler
有关详细信息,请参见第17.4节“Remember-Me接口和实现”。
并非直接提供LogoutHandler
实现,流利的API还提供了快捷方式,提供了相应的LogoutHandler
实现。 例如。 deleteCookies()
允许指定在注销成功时要删除的一个或多个cookie的名称。 与添加CookieClearingLogoutHandler
相比,这是一个快捷方式。
5.5.2 LogoutSuccessHandler
LogoutSuccessHandler在LogoutFilter成功注销后被调用,以处理例如。 重定向或转发到适当的目的地。 注意,该接口几乎与LogoutHandler相同,但可能引发异常。
提供以下实现:
- SimpleUrlLogoutSuccessHandler
- HttpStatusReturningLogoutSuccessHandler
如上所述,您不需要直接指定SimpleUrlLogoutSuccessHandler
。相反,流利的API通过设置logoutSuccessUrl()
提供了一个快捷方式。 这将在下面设置SimpleUrlLogoutSuccessHandler
。提供的URL将在发生注销后重定向到。 默认值为/login?logout
。 HttpStatusReturningLogoutSuccessHandler
在REST API类型场景中很有趣。 成功注销后,此LogoutSuccessHandler
不会重定向到URL,而是允许您提供要返回的纯HTTP状态代码。 如果未配置,默认情况下将返回状态代码200。
5.6 Authentication
到目前为止,我们只看了一下最基本的认证配置。让我们来看看一些稍微更高级的配置认证选项。
5.6.1 In Memory Authentication
我们已经看到了为单个用户配置内存身份验证的示例。以下是配置多个用户的示例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
5.6.2 JDBC Authentication
您可以找到对基于JDBC的身份验证的更新。下面的示例假设您已经在应用程序中定义了一个DataSource
。 jdbc-javaconfig示例提供了使用基于JDBC的身份验证的完整示例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
5.6.3 LDAP Authentication
您可以找到更新以支持基于LDAP的身份验证。 ldap-javaconfig示例提供了使用基于LDAP的身份验证的完整示例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
上面的示例使用以下LDIF和嵌入式Apache DS LDAP实例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
5.6.4 AuthenticationProvider
您可以通过将自定义AuthenticationProvider
作为bean公开来定义自定义认证。例如,以下将自定义认证,假设SpringAuthenticationProvider
实现AuthenticationProvider
:
这仅在未填充
AuthenticationManagerBuilder
时使用
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
5.6.5 UserDetailsService
您可以通过将自定义UserDetailsService
作为bean公开来定义自定义认证。例如,以下将自定义认证,假设SpringDataUserDetailsService
实现UserDetailsService
:
这仅在未填充
AuthenticationManagerBuilder
且未定义AuthenticationProviderBean
时使用。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
您还可以通过将PasswordEncoder
公开为bean来自定义密码的编码方式。例如,如果使用bcrypt,您可以添加一个bean定义,如下所示:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
5.7 Multiple HttpSecurity
我们可以配置多个HttpSecurity
实例,就像我们可以有多个<http>
块。 关键是要多次扩展WebSecurityConfigurationAdapter
。 例如,以下是具有以/api/
开头的URL的不同配置的示例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
配置认证正常
创建包含@Order
的WebSecurityConfigurerAdapter
的实例,以指定应首先考虑哪个WebSecurityConfigurerAdapter
。
http.antMatcher
声明这个HttpSecurity将只适用于以/api/
创建另一个WebSecurityConfigurerAdapter
实例。 如果URL不以/api/
开头,则将使用此配置。 这个配置在ApiWebSecurityConfigurationAdapter
之后被考虑,因为它在1之后有一个@Order
值(没有@Order
默认值为last)。
5.8 Method Security
从2.0版本开始,Spring Security已经大大提高了对您的服务层方法添加安全性的支持。 它提供对JSR-250注释安全性以及框架的原始@Secured
注释的支持。 从3.0,你也可以使用新的基于表达式的注释。 您可以将安全性应用于单个bean,使用intercept-methods
元素装饰bean声明,也可以使用AspectJ样式切入点在整个服务层上保护多个bean。
5.8.1 EnableGlobalMethodSecurity
我们可以使用任何@Configuration
实例上的@EnableGlobalMethodSecurity
注释启用基于注释的安全性。例如,以下将启用Spring Security的@Secured
注释。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
向方法(在类或接口上)添加注释将相应地限制对该方法的访问。 Spring Security的原生注释支持为方法定义了一组属性。 这些将被传递给AccessDecisionManager
,以便做出实际的决定:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
可以启用对JSR-250注释的支持
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
这些是基于标准的,允许应用简单的基于角色的约束,但没有Spring Security的本机注释。要使用新的基于表达式的语法,您将使用
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
和等效的Java代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
5.8.2 GlobalMethodSecurityConfiguration
有时,您可能需要执行比使用@EnableGlobalMethodSecurity
注释允许的操作更复杂的操作。 对于这些实例,您可以扩展GlobalMethodSecurityConfiguration
,确保@EnableGlobalMethodSecurity
注释存在于您的子类上。 例如,如果要提供自定义的MethodSecurityExpressionHandler
,可以使用以下配置:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
有关可以覆盖的方法的其他信息,请参阅GlobalMethodSecurityConfiguration
Javadoc。
5.9 Post Processing Configured Objects
Spring Security的Java配置不公开它配置的每个对象的每个属性。 这简化了大多数用户的配置。 总之,如果每个属性都暴露出来,用户可以使用标准bean配置。 虽然有很好的理由不直接暴露每个属性,用户可能仍需要更高级的配置选项。 为了解决这个问题Spring Security引入了一个ObjectPostProcessor
的概念,它可以用来修改或替换由Java配置创建的许多Object实例。 例如,如果要在FilterSecurityInterceptor
上配置filterSecurityPublishAuthorizationSuccess
属性,您可以使用以下命令: