Java 配置
Spring Framework 在 Spring 3.1 中增加了对 Java 配置 的通用支持。Spring Security 3.2 引入了 Java 配置,让用户可以在不使用任何 XML 的情况下配置 Spring Security。
如果你熟悉安全命名空间配置,你应该会发现它与 Spring Security 的 Java 配置有很多相似之处。
Spring Security 提供了许多示例应用程序,以演示 Spring Security Java 配置的使用。
Hello Web Security Java 配置
第一步是创建我们的Spring Security Java配置。该配置创建了一个名为 springSecurityFilterChain
的Servlet过滤器,它负责应用程序中的所有安全工作(保护应用程序的URL、验证提交的用户名和密码、重定向到登录表单等)。以下示例展示了最基本的Spring Security Java配置:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.authentication.builders.*;
import org.springframework.security.config.annotation.web.configuration.*;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build());
return manager;
}
}
此配置并不复杂或广泛,但它做了很多事情:
-
要求对应用程序中的每个 URL 进行身份验证
-
为您生成登录表单
-
允许用户名为
user
且密码为password
的用户通过基于表单的身份验证进行认证 -
允许用户登出
-
CSRF 攻击 防护
-
安全头集成:
-
缓存控制(您可以在应用程序中稍后覆盖以允许缓存静态资源)
-
X-Frame-Options 集成以帮助防止 Clickjacking
-
与以下 Servlet API 方法的集成:
AbstractSecurityWebApplicationInitializer
下一步是将 springSecurityFilterChain
注册到 WAR 文件中。在 Servlet 3.0+ 环境下,你可以通过 Spring 的 WebApplicationInitializer 支持 在 Java 配置中进行注册。不出所料,Spring Security 提供了一个基类(AbstractSecurityWebApplicationInitializer
),以确保 springSecurityFilterChain
为你注册。我们使用 AbstractSecurityWebApplicationInitializer
的方式取决于我们是否已经在使用 Spring,或者 Spring Security 是我们应用程序中唯一的 Spring 组件。
-
AbstractSecurityWebApplicationInitializer without Existing Spring - 如果您还没有使用 Spring,请使用这些说明
-
AbstractSecurityWebApplicationInitializer with Spring MVC - 如果您已经在使用 Spring,请使用这些说明
AbstractSecurityWebApplicationInitializer without Existing Spring
如果你不使用 Spring 或 Spring MVC,则需要将 WebSecurityConfig
传递给超类,以确保配置被正确加载:
import org.springframework.security.web.context.*;
public class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(WebSecurityConfig.class);
}
}
SecurityWebApplicationInitializer
:
-
自动为应用程序中的每个 URL 注册
springSecurityFilterChain
过滤器。 -
添加一个加载 WebSecurityConfig 的
ContextLoaderListener
。
AbstractSecurityWebApplicationInitializer 与 Spring MVC
如果我们在应用程序的其他地方使用了 Spring,我们可能已经有一个 WebApplicationInitializer
在加载我们的 Spring 配置。如果我们使用前面的配置,将会出现错误。相反,我们应该将 Spring Security 注册到现有的 ApplicationContext
中。例如,如果我们使用 Spring MVC,我们的 SecurityWebApplicationInitializer
可能看起来像下面这样:
import org.springframework.security.web.context.*;
public class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {
}
这只会为应用程序中的每个 URL 注册 springSecurityFilterChain
。之后,我们需要确保 WebSecurityConfig
在现有的 ApplicationInitializer
中被加载。例如,如果我们使用 Spring MVC,它会被添加到 getServletConfigClasses()
中:
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebSecurityConfig.class, WebMvcConfig.class };
}
// ... other overrides ...
}
这样做的原因是 Spring Security 需要能够检查一些 Spring MVC 配置,以便正确配置底层请求匹配器,因此它们需要在同一个应用上下文中。将 Spring Security 放在 getRootConfigClasses
中会将其放入一个可能无法找到 Spring MVC 的 HandlerMappingIntrospector
的父应用上下文中。
配置多个 Spring MVC 分发器
如果需要,可以将与 Spring MVC 无关的任何 Spring Security 配置放在一个不同的配置类中,如下所示:
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { NonWebSecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebSecurityConfig.class, WebMvcConfig.class };
}
// ... other overrides ...
}
如果你有多个 AbstractAnnotationConfigDispatcherServletInitializer
实例,并且不想在它们之间重复通用的安全配置,那么这将会很有帮助。
HttpSecurity
到目前为止,我们的 WebSecurityConfig 仅包含有关如何验证用户的信息。Spring Security 是如何知道我们希望要求所有用户都进行身份验证的?Spring Security 又是如何知道我们希望支持基于表单的身份验证的?实际上,有一个配置类(称为 SecurityFilterChain
)在幕后被调用。它被配置为以下默认实现:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults());
return http.build();
}
默认配置(如前面的示例所示):
-
确保对应用程序的任何请求都需要用户进行身份验证
-
允许用户使用基于表单的登录进行身份认证
-
允许用户使用HTTP Basic认证进行身份认证
请注意,此配置与XML命名空间配置相对应:
<http>
<intercept-url pattern="/**" access="authenticated"/>
<form-login />
<http-basic />
</http>
多个 HttpSecurity 实例
要有效地管理应用程序中某些区域需要不同保护的安全性,我们可以使用多个过滤器链以及 securityMatcher
DSL 方法。这种方法允许我们为应用程序的特定部分定义不同的安全配置,从而增强整个应用程序的安全性和控制。
我们可以配置多个 HttpSecurity
实例,就像我们可以在 XML 中拥有多个 <http>
块一样。关键是要注册多个 SecurityFilterChain
@Bean
。下面的示例对以 /api/
开头的 URL 有不同的配置:
@Configuration
@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Bean 1
public UserDetailsService userDetailsService() throws Exception {
UserBuilder users = User.withDefaultPasswordEncoder();
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(users.username("user").password("password").roles("USER").build());
manager.createUser(users.username("admin").password("password").roles("USER","ADMIN").build());
return manager;
}
@Bean
@Order(1) 2
public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/api/**") 3
.authorizeHttpRequests(authorize -> authorize
.anyRequest().hasRole("ADMIN")
)
.httpBasic(Customizer.withDefaults());
return http.build();
}
@Bean 4
public SecurityFilterChain formLoginFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults());
return http.build();
}
}
如常配置身份验证。
创建一个包含
@Order
的SecurityFilterChain
实例,以指定应首先考虑哪个SecurityFilterChain
。http.securityMatcher()
声明此HttpSecurity
仅适用于以/api/
开头的 URL。创建另一个
SecurityFilterChain
实例。如果 URL 不是以/api/
开头,则使用此配置。由于其@Order
值在1
之后(没有@Order
默认为最后),因此此配置在apiFilterChain
之后被考虑。
选择 securityMatcher
或 requestMatchers
一个常见的问题是:
http.securityMatcher()
方法和用于请求授权的requestMatchers()
(即在http.authorizeHttpRequests()
中使用)之间有什么区别?
要回答这个问题,有助于理解每个用于构建 SecurityFilterChain
的 HttpSecurity
实例都包含一个 RequestMatcher
来匹配传入的请求。如果请求与更高优先级的 SecurityFilterChain
(例如 @Order(1)
)不匹配,则可以尝试将该请求与较低优先级的过滤器链(例如没有 @Order
)进行匹配。
匹配多个过滤器链的逻辑由 FilterChainProxy 执行。
默认的 RequestMatcher
匹配 任何请求 以确保 Spring Security 默认保护 所有请求。
指定 securityMatcher
会覆盖此默认设置。
如果没有任何过滤器链与某个特定请求匹配,那么该请求将不受Spring Security的保护。
以下示例演示了一个仅保护以 /secured/
开头的请求的单一过滤器链:
@Configuration
@EnableWebSecurity
public class PartialSecurityConfig {
@Bean
public UserDetailsService userDetailsService() throws Exception {
// ...
}
@Bean
public SecurityFilterChain securedFilterChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/secured/**") 1
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/secured/user").hasRole("USER") 2
.requestMatchers("/secured/admin").hasRole("ADMIN") 3
.anyRequest().authenticated() 4
)
.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults());
return http.build();
}
}
以
/secured/
开头的请求将受到保护,但其他任何请求都不会受到保护。对
/secured/user
的请求需要ROLE_USER
权限。对
/secured/admin
的请求需要ROLE_ADMIN
权限。任何其他请求(如
/secured/other
)只需要一个已认证的用户。
建议提供一个不指定任何 securityMatcher
的 SecurityFilterChain
,以确保整个应用程序都受到保护,如前面的示例所示。
请注意,requestMatchers
方法仅适用于单个授权规则。列出的每个请求还必须与用于创建 SecurityFilterChain
的特定 HttpSecurity
实例的总体 securityMatcher
匹配。在此示例中使用 anyRequest()
会匹配此特定 SecurityFilterChain
中的所有其他请求(这些请求必须以 /secured/
开头)。
有关 requestMatchers
的更多信息,请参见 授权 HttpServletRequests。
SecurityFilterChain
端点
SecurityFilterChain
中的几个过滤器直接提供了端点,例如由 http.formLogin()
设置的 UsernamePasswordAuthenticationFilter
,它提供了 POST /login
端点。在上面的例子中,/login
端点没有被 http.securityMatcher("/secured/**")
匹配,因此该应用程序将没有任何 GET /login
或 POST /login
端点。这样的请求会返回 404 Not Found
。这通常会让用户感到意外。
指定 http.securityMatcher()
影响该 SecurityFilterChain
匹配哪些请求。但是,它不会自动影响过滤器链提供的端点。在这种情况下,您可能需要自定义过滤器链要提供的任何端点的 URL。
以下示例演示了一种配置,该配置保护以 /secured/
开头的请求并拒绝所有其他请求,同时自定义由 SecurityFilterChain
提供的端点:
@Configuration
@EnableWebSecurity
public class SecuredSecurityConfig {
@Bean
public UserDetailsService userDetailsService() throws Exception {
// ...
}
@Bean
@Order(1)
public SecurityFilterChain securedFilterChain(HttpSecurity http) throws Exception {
http
.securityMatcher("/secured/**") 1
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated() 2
)
.formLogin(formLogin -> formLogin 3
.loginPage("/secured/login")
.loginProcessingUrl("/secured/login")
.permitAll()
)
.logout(logout -> logout 4
.logoutUrl("/secured/logout")
.logoutSuccessUrl("/secured/login?logout")
.permitAll()
)
.formLogin(Customizer.withDefaults());
return http.build();
}
@Bean
public SecurityFilterChain defaultFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().denyAll() 5
);
return http.build();
}
}
以
/secured/
开头的请求将受此过滤器链保护。以
/secured/
开头的请求需要经过身份验证的用户。自定义表单登录,使 URL 以前缀
/secured/
开头。自定义注销,使 URL 以前缀
/secured/
开头。所有其他请求将被拒绝。
此示例自定义了登录和注销页面,这将禁用 Spring Security 生成的页面。你必须自行提供 GET /secured/login
和 GET /secured/logout
的自定义端点。请注意,Spring Security 仍然为你提供了 POST /secured/login
和 POST /secured/logout
端点。
真实世界示例
以下示例演示了一个稍微更接近实际的配置,将所有这些元素组合在一起:
@Configuration
@EnableWebSecurity
public class BankingSecurityConfig {
@Bean 1
public UserDetailsService userDetailsService() {
UserBuilder users = User.withDefaultPasswordEncoder();
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(users.username("user1").password("password").roles("USER", "VIEW_BALANCE").build());
manager.createUser(users.username("user2").password("password").roles("USER").build());
manager.createUser(users.username("admin").password("password").roles("ADMIN").build());
return manager;
}
@Bean
@Order(1) 2
public SecurityFilterChain approvalsSecurityFilterChain(HttpSecurity http) throws Exception {
String[] approvalsPaths = { "/accounts/approvals/**", "/loans/approvals/**", "/credit-cards/approvals/**" };
http
.securityMatcher(approvalsPaths)
.authorizeHttpRequests(authorize -> authorize
.anyRequest().hasRole("ADMIN")
)
.httpBasic(Customizer.withDefaults());
return http.build();
}
@Bean
@Order(2) 3
public SecurityFilterChain bankingSecurityFilterChain(HttpSecurity http) throws Exception {
String[] bankingPaths = { "/accounts/**", "/loans/**", "/credit-cards/**", "/balances/**" };
String[] viewBalancePaths = { "/balances/**" };
http
.securityMatcher(bankingPaths)
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(viewBalancePaths).hasRole("VIEW_BALANCE")
.anyRequest().hasRole("USER")
);
return http.build();
}
@Bean 4
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
String[] allowedPaths = { "/", "/user-login", "/user-logout", "/notices", "/contact", "/register" };
http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(allowedPaths).permitAll()
.anyRequest().authenticated()
)
.formLogin(formLogin -> formLogin
.loginPage("/user-login")
.loginProcessingUrl("/user-login")
)
.logout(logout -> logout
.logoutUrl("/user-logout")
.logoutSuccessUrl("/?logout")
);
return http.build();
}
}
首先配置认证设置。
定义一个带有
@Order(1)
的SecurityFilterChain
实例,这意味着该过滤器链将具有最高优先级。此过滤器链仅适用于以/accounts/approvals/
、/loans/approvals/
或/credit-cards/approvals/
开头的请求。对该过滤器链的请求需要ROLE_ADMIN
权限,并允许 HTTP 基本认证。接下来,创建另一个带有
@Order(2)
的SecurityFilterChain
实例,它将被视为第二优先级。此过滤器链仅适用于以/accounts/
、/loans/
、/credit-cards/
或/balances/
开头的请求。请注意,由于此过滤器链是第二优先级,任何包含/approvals/
的请求将匹配前一个过滤器链,并且不会被此过滤器链匹配。对该过滤器链的请求需要ROLE_USER
权限。此过滤器链没有定义任何认证,因为下一个(默认)过滤器链包含了该配置。最后,创建一个没有
@Order
注解的SecurityFilterChain
实例。此配置将处理其他过滤器链未覆盖的请求,并将在最后处理(没有@Order
默认为最后)。与/
、/user-login
、/user-logout
、/notices
、/contact
和/register
匹配的请求允许在无需认证的情况下访问。任何其他请求都需要用户进行认证才能访问未明确允许或受其他过滤器链保护的任何 URL。
自定义 DSL
你可以在Spring Security中提供自己的自定义DSL:
- Java
- Kotlin
public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
private boolean flag;
@Override
public void init(HttpSecurity http) throws Exception {
// any method that adds another configurer
// must be done in the init method
http.csrf().disable();
}
@Override
public void configure(HttpSecurity http) throws Exception {
ApplicationContext context = http.getSharedObject(ApplicationContext.class);
// here we lookup from the ApplicationContext. You can also just create a new instance.
MyFilter myFilter = context.getBean(MyFilter.class);
myFilter.setFlag(flag);
http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class);
}
public MyCustomDsl flag(boolean value) {
this.flag = value;
return this;
}
public static MyCustomDsl customDsl() {
return new MyCustomDsl();
}
}
class MyCustomDsl : AbstractHttpConfigurer<MyCustomDsl, HttpSecurity>() {
var flag: Boolean = false
override fun init(http: HttpSecurity) {
// any method that adds another configurer
// must be done in the init method
http.csrf().disable()
}
override fun configure(http: HttpSecurity) {
val context: ApplicationContext = http.getSharedObject(ApplicationContext::class.java)
// here we lookup from the ApplicationContext. You can also just create a new instance.
val myFilter: MyFilter = context.getBean(MyFilter::class.java)
myFilter.setFlag(flag)
http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter::class.java)
}
companion object {
@JvmStatic
fun customDsl(): MyCustomDsl {
return MyCustomDsl()
}
}
}
这实际上是像 HttpSecurity.authorizeHttpRequests()
这样的方法的实现方式。
然后你可以使用自定义的 DSL:
- Java
- Kotlin
@Configuration
@EnableWebSecurity
public class Config {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.with(MyCustomDsl.customDsl(), (dsl) -> dsl
.flag(true)
)
// ...
return http.build();
}
}
@Configuration
@EnableWebSecurity
class Config {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http
.with(MyCustomDsl.customDsl()) {
flag = true
}
// ...
return http.build()
}
}
代码按以下顺序调用:
-
Config.filterChain
方法中的代码被调用 -
MyCustomDsl.init
方法中的代码被调用 -
MyCustomDsl.configure
方法中的代码被调用
如果你想,可以通过使用 SpringFactories
让 HttpSecurity
默认添加 MyCustomDsl
。例如,你可以在类路径下创建一个名为 META-INF/spring.factories
的资源,其内容如下:
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = sample.MyCustomDsl
您也可以显式禁用默认设置:
- Java
- Kotlin
@Configuration
@EnableWebSecurity
public class Config {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.with(MyCustomDsl.customDsl(), (dsl) -> dsl
.disable()
)
...;
return http.build();
}
}
@Configuration
@EnableWebSecurity
class Config {
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http
.with(MyCustomDsl.customDsl()) {
disable()
}
// ...
return http.build()
}
}
后处理配置对象
Spring Security 的 Java 配置并没有暴露它配置的每个对象的每个属性。这简化了大多数用户的配置。毕竟,如果每个属性都被暴露,用户可以使用标准的 bean 配置。
虽然有充分的理由不直接暴露每个属性,但用户可能仍然需要更高级的配置选项。为了解决这个问题,Spring Security 引入了 ObjectPostProcessor
的概念,可以用来修改或替换 Java 配置创建的许多 Object
实例。例如,要配置 FilterSecurityInterceptor
上的 filterSecurityPublishAuthorizationSuccess
属性,可以使用以下方法:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
public <O extends FilterSecurityInterceptor> O postProcess(
O fsi) {
fsi.setPublishAuthorizationSuccess(true);
return fsi;
}
})
);
return http.build();
}