Переопределить конфигурацию Spring Security (HttpSecurity) при переопределении в зависимости Maven - PullRequest
0 голосов
/ 29 июня 2019

У меня есть Maven Spring Boot 2 с проектом Spring Security. Одна из зависимостей 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();
    }
}

Я надеялся, что где-то может быть один установщик для этих двух значений.

Спасибо

1 Ответ

0 голосов
/ 29 июня 2019

Вам нужно установить порядок переопределения одного на более высокий, чем переопределенный.

@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 установлен для переопределенного класса.

Или вы можете установить определенный номер для переопределения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...