java - 在 Maven 依赖项中重写时重写 Spring Security 配置(HttpSecurity)

标签 java spring spring-boot spring-security

我有一个带有 Spring Security 的 Maven Spring Boot 2 项目。 maven 依赖项之一扩展了 WebSecurityConfigurerAdapter。例如,

public class MyConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
                .and()
                .csrf()
                .and()
                .formLogin()
                .permitAll()
                .successHandler(myLoginHandler)
                .failureHandler(formAuthFailureHandler)
                .and()
                .logout()
                .permitAll()
                .logoutRequestMatcher(new AntPathRequestMatcher(logoutUrl()))
                .logoutSuccessUrl(logoutSuccessUrl())
                .and()
                .authorizeRequests()
                .antMatchers(publicRoutes())
                .permitAll()
                .antMatchers(HttpMethod.POST).authenticated()
                .antMatchers(HttpMethod.PUT).authenticated()
                .antMatchers(HttpMethod.PATCH).authenticated()
                .antMatchers(HttpMethod.DELETE).denyAll()
                .anyRequest()
                .authenticated();
    }
}

问题是在这个应用程序中,我需要重写 successHandler() 并添加一个注销处理程序,如 logout().addLogoutHandler(myLogoutHandler)

是否可以只更新这些位,或者我需要再次定义整个链?也许是这样的,

public class AnotherConfig extends MyConfig {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
                .and()
                .csrf()
                .and()
                .formLogin()
                .permitAll()
                .successHandler(myLoginHandler)
                .failureHandler(formAuthFailureHandler)
                .and()
                .logout()
                .addLogoutHandler(myLogoutHandler)
                .permitAll()
                .logoutRequestMatcher(new AntPathRequestMatcher(logoutUrl()))
                .logoutSuccessUrl(logoutSuccessUrl())
                .and()
                .authorizeRequests()
                .antMatchers(publicRoutes())
                .permitAll()
                .antMatchers(HttpMethod.POST).authenticated()
                .antMatchers(HttpMethod.PUT).authenticated()
                .antMatchers(HttpMethod.PATCH).authenticated()
                .antMatchers(HttpMethod.DELETE).denyAll()
                .anyRequest()
                .authenticated();
    }
}

我希望在某个地方可能有一个用于这两个值的 setter 。

谢谢

最佳答案

您需要将覆盖的顺序设置为高于被覆盖的顺序。

@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class AnotherConfig extends MyConfig {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
                .and()
                .csrf()
                .and()
                .formLogin()
                .permitAll()
                .successHandler(myLoginHandler)
                .failureHandler(formAuthFailureHandler)
                .and()
                .logout()
                .addLogoutHandler(myLogoutHandler)
                .permitAll()
                .logoutRequestMatcher(new AntPathRequestMatcher(logoutUrl()))
                .logoutSuccessUrl(logoutSuccessUrl())
                .and()
                .authorizeRequests()
                .antMatchers(publicRoutes())
                .permitAll()
                .antMatchers(HttpMethod.POST).authenticated()
                .antMatchers(HttpMethod.PUT).authenticated()
                .antMatchers(HttpMethod.PATCH).authenticated()
                .antMatchers(HttpMethod.DELETE).denyAll()
                .anyRequest()
                .authenticated();
    }
}

为什么Ordered.LOWEST_PRECEDENCE - 1
因为默认顺序是为重写类设置的 Ordered.LOWEST_PRECEDENCE

或者您可以将其设置为覆盖的特定数字。

关于java - 在 Maven 依赖项中重写时重写 Spring Security 配置(HttpSecurity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814278/

相关文章:

java - 运行 Spring Boot 应用程序时出现 ClassNotFoundException

spring - 将客户端证书添加到 Spring Boot 应用程序

java - 在Java中将String日期转换为ISO日期格式“2019-07-01T03:50:00.000Z”

java - 使用 Spring 包装类相对于 Quartz 调度程序库的优势

java - 将文件数据作为 Bzip2 写入 servlet 响应的输出

java - 使用 SimpleXsdSchema 创建 DefaultWsdl11Definition

java - GradlecompileTestJava 在 gradle clean 构建期间失败

java - 可选的映射类型转换为子类对象

java - 取景器不翻转

java - CompletableFuture 执行中出现意外的空值