【问题标题】:Spring Boot WebSecurityConfigurerAdapter: one configuration not being appliedSpring Boot WebSecurityConfigurerAdapter:未应用一项配置
【发布时间】:2020-06-18 09:24:54
【问题描述】:

我正在尝试实现两种不同的安全配置(适用于管理员和普通用户)。为了实现这一点,我有两个扩展 WebSecurityConfigurerAdapter 的类 - 每个用户类型一个。

我的问题是管理员的配置被应用并按预期工作,重定向到登录页面,但我的其他配置似乎没有任何影响。更改 @Order 注释似乎不会改变任何东西。日志显示,在应用程序部署时,两个过滤器都应用于链。

我的配置类:

@Configuration
@Order(1)
public class AdminConfigurationAdapter extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/images/**", "/css/**", "/js/**", "/webjars/**", "**/favicon.ico");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/admin/login")
            .permitAll()

            .antMatchers("/admin/**")
            .hasRole("ADMIN")

            .and()
            .formLogin()
            .loginPage("/admin/login")
            .loginProcessingUrl("/admin/loginAction")
            //.failureUrl("/loginAdmin?error=loginError")
            .defaultSuccessUrl("/admin/dashboard", true)

            .and()
            .logout()
            .logoutUrl("/admin/logoutAction")
            //.logoutSuccessUrl("/protectedLinks")
            .deleteCookies("JSESSIONID")

            .and()
            .exceptionHandling()
            .accessDeniedPage("/403")

            .and()
            .csrf().disable();
  }

  @Override
  protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN");
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
} 

二等:

@Configuration
@Order(2)
public class GameConfigurationAdapter extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/images/**", "/css/**", "/js/**", "/webjars/**", "**/favicon.ico");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/", "/game/login")
            .permitAll()

            .antMatchers("/game/**")
            .hasRole("USER")

            .and()
            .formLogin()
            .loginPage("/game/login")
            .loginProcessingUrl("/game/login")
            //.failureUrl("/game/login")
            .defaultSuccessUrl("/game/home", true)

            .and()
            .logout()
            .logoutUrl("/game/logout")
            //.logoutSuccessUrl("/")
            .deleteCookies("JSESSIONID")

            .and()
            .exceptionHandling()
            .accessDeniedPage("/403")

            .and()
            .csrf().disable();
  }
}

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    不知何故 @Order 注释影响了我的应用程序。通过颠倒配置顺序,它解决了我的问题,而没有更改任何其他内容。

    我仍然不确定如何以及为什么,但是将 GameConfigurationAdapter @Order 值设置为 1 并将 AdminConfigurationAdapter 设置为 2 我的过滤器得到了正确应用。

    【讨论】:

      【解决方案2】:

      可能是因为在第二个配置中您设置了路由“/”,请求可能会进入那里。

      改变这个:

      .antMatchers("/", "/game/login")
      

      为:

      .antMatchers("/game/login")
      

      【讨论】:

      • 我已经尝试过了,它并没有改变任何东西。此外,我的“/”映射做了一些后台逻辑,并且在大多数情况下将用户重定向到“游戏/登录”。但我仍然能够在登录之前访问“游戏/加入”。
      【解决方案3】:

      我有另一个具有类似配置的应用程序,我使用 antMatcher 而不是 antMatchers.. 并且适用于所有端点。

      http
          .antMatcher("/game/login")
          .authorizeRequests()
          .antMatchers("/game/**").hasRole("USER")
      .and()
          .formLogin()
          ...
      

      唯一不同的是,我有 3 个配置: /admin/login 、 /game/login 、 /others

      1. 多个 HttpSecurity 配置: when-to-use-spring-securitys-antmatcher
      2. 多重安全:Multiple security
      3. AntMatcher vs AntMatchers

      【讨论】:

        猜你喜欢
        • 2022-11-27
        • 2018-02-02
        • 2018-11-07
        • 2020-09-19
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 2014-11-24
        • 2019-10-16
        相关资源
        最近更新 更多