当前位置: 首页 > 面试题库 >

Java Spring Security:多个HTTP配置不起作用

封昊天
2023-03-14
问题内容

我正在尝试使用Spring Security,并且有一个用例,其中我想保护不同的登录页面和不同的URL集。

这是我的配置:

@Configuration
@Order(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .antMatchers("/admin/login").permitAll()
                .antMatchers("/admin/**").access("hasRole('BASE_USER')")
                .and()
            .formLogin()
                .loginPage("/admin/login").permitAll()
                .defaultSuccessUrl("/admin/home")
                .failureUrl("/admin/login?error=true").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
            .csrf()                    
                .and()
            .exceptionHandling().accessDeniedPage("/Access_Denied");            
    }
}


@Configuration
@Order(2)
public static class ConsumerSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/consumer/login").permitAll()
                .antMatchers("/consumer/**").access("hasRole('BASE_USER')")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/consumer/login").permitAll()
                .defaultSuccessUrl("/consumer/home")
                .failureUrl("/consumer/login?error=true").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")
                .and().csrf()                
                .and()
            .exceptionHandling().accessDeniedPage("/Access_Denied");
    }
}

这些类是MultipleHttpSecurityConfig具有注释的另一个类的内部类@EnableWebSecurity

的安全性admin/**工作正常,但所有consumer/**页面均未得到保护,登录页面未发生任何重定向。我搜索了其他答案,但没有一个有效。


问题答案:

看一下Spring Security Reference:

@EnableWebSecurity
public class MultiHttpSecurityConfig {
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) { 1
      auth
          .inMemoryAuthentication()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
  }

  @Configuration
  @Order(1)                                                        2
  public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
      protected void configure(HttpSecurity http) throws Exception {
          http
              .antMatcher("/api/**")                               3
              .authorizeRequests()
                  .anyRequest().hasRole("ADMIN")
                  .and()
              .httpBasic();
      }
  }    

  @Configuration                                                   4
  public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
                  .anyRequest().authenticated()
                  .and()
              .formLogin();
      }
  }
}

1正常配置身份验证

2创建一个WebSecurityConfigurerAdapter包含的实例,@Order以指定WebSecurityConfigurerAdapter应首先考虑的对象。

3 http.antMatcher指出这HttpSecurity仅适用于以开头的URL/api/

4创建的另一个实例WebSecurityConfigurerAdapter。如果URL不以/api/该配置开头,则将使用此配置。此配置被认为是之后的,ApiWebSecurityConfigurationAdapter因为它的@Order值是after 1(没有@Order默认值是last)。

您的第二个配置未使用,因为您的第一个配置匹配/**(未antMatcher配置)。而且您的第一个配置仅限制/admin/**,默认情况下允许所有其他URL。



 类似资料:
  • 问题内容: 我正在尝试使用Spring Security,并且有一个用例,其中我想保护不同的登录页面和不同的URL集。 这是我的配置: 这些类是具有注释的另一个类的内部类。 的安全性工作正常,但所有页面均未得到保护,登录页面未发生任何重定向。我搜索了其他答案,但没有一个有效。 问题答案: 看一下: 1正常配置身份验证 2创建一个包含的实例,以指定应首先考虑的对象。 3 r指出这仅适用于以开头的 4

  • 我正在尝试使用Spring Security,我有一个用例,我想要保护不同的登录页面和不同的URL集。 下面是我的配置:

  • 这是我第一次使用Spring-Data-jpa和Querydsl。 我正在尝试配置数据模型的自动生成。 我执行了以下步骤: 我在pom中添加了以下依赖项。xml 然后我添加了以下插件 但我总是在pom中遇到同样的错误。xml: 生命周期配置未涵盖插件执行:com。迈塞马。maven:maven apt插件:1.0.2:流程(执行:默认,阶段:生成源代码) 并且未生成和填充target/genera

  • 我在向我的GsonBuilder注册多个TypeAdapter时遇到问题。似乎只有一个会开火,而第二个永远不会被考虑。如果我单独使用每一个,它似乎工作得很好。但我需要他们一起工作,而且似乎我做错了什么。此外,我目前正在使用GSON v2。2.4. Zip对象简单表单: 拉链系列: 简单形式: JsonResponseSerializer: 测试示例: 输出: 预期输出:(注意ZipSerializ

  • 我在may Spring应用程序中添加了安全问题,所以我想通过调试Spring。 Log4j正在工作,因为我的服务正在将调试信息记录到控制台。但是Spring没有记录任何东西到我的控制台 这是我的 在我如此放置。结果: 我尝试在我的网站上添加这个。正如在这个问题中所说的xml 我在其他应用程序中使用了此调试,但我真的没有看到原因。。。谢谢你的帮助

  • 问题内容: 下面的代码有任何错误吗?以下代码无法提供多个目录服务。当我访问localhost:9090 / ide时,服务器将返回404错误。 当我这样更改代码时, 它会按我预期的那样工作。 问题答案: 使用中的问题之一是请求路径用于构建文件名,因此,如果要从根目录以外的任何地方提供服务,则需要剥离到该处理程序的路由前缀。 标准库为此提供了一个有用的工具,但该工具只能在s上使用,而不能在s 上使用