【问题标题】:Unable to understand the behavior of Spring security无法理解 Spring 安全的行为
【发布时间】:2019-11-08 21:26:20
【问题描述】:

我正在使用带有执行器依赖项的 spring boot 2.1.4。我想为执行器和我的应用程序配置单独的身份验证和授权机制。我阅读了Multiple HttpSecurity 并将我的 WebSecurityAdapter 配置如下:

@Configuration
public class ProvisioningServiceSecurityConfiguration {

  @Value("${actuator.user.name}")
  private String actuatorUserName;

  @Value("${actuator.password}")
  private String actuatorPassword;

  @Value("${actuator.role}")
  private String actuatorRole;

  @Bean
  public UserDetailsService userDetailsService() throws Exception {
    // ensure the passwords are encoded properly
    UserBuilder users = User.withDefaultPasswordEncoder();
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(users.username("user").password("password").roles("ADMIN").build());
    manager.createUser(
        users.username(actuatorUserName).password(actuatorPassword).roles(actuatorRole).build());
    return manager;
  }

  @Configuration
  @Order(1)
  public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception {
      http
          .antMatcher("/locations/**")
          .antMatcher("/organizations/**")
          .antMatcher("/productTypes/**")
          .authorizeRequests()
          .anyRequest().hasRole("ADMIN")
          .and()
          .httpBasic();
    }
  }

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

    protected void configure(HttpSecurity http) throws Exception {
      http
          .antMatcher("/manage/**")
          .authorizeRequests()
          .anyRequest().hasRole("ACTUATOR_ADMIN")
          .and()
          .httpBasic();
    }
  }

  /*@Configuration
  public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

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

}

注意:我暂时禁用了表单登录

当我运行 curl 请求时

curl -XGET  http://localhost:9797/provisioningService/organizations/all

我可以看到输出。就好像春天的安全从未存在过。当我启用表单登录时,我得到了弹簧登录屏幕。我观察到的另一种行为是,如果我将 /locations 的用户名和密码与执行器用户名和密码互换,我仍然会得到有效的响应。

我知道表单登录更多是一种后备,但我想禁用表单登录(可能我们可能会转移到 cas)并仅使用基于 spring security httpBasic 的身份验证和授权。我无法理解我所犯的错误。

我的要求终于:

1) 仅当用户名密码为“user”和“password”时,才能访问对 /organizations 或 /locations 等的请求

2) 仅当用户名和密码以及角色与执行器用户名和密码匹配时,才能访问作为执行器 API 的 /manage 请求。

3) 任何其他API都可以permitAll/form login

我该如何实现这一目标?

【问题讨论】:

  • 您可以先阅读如何定义授权请求:docs.spring.io/spring-security/site/docs/5.2.0.M3/reference/…
  • @EnableWebSecurity 添加到您的ProvisioningServiceSecurityConfiguration 中,这样Spring Boot 的默认设置不适用(它们现在适用,您可以对其进行扩展)。这也意味着您必须配置 AuthenticationManager 而不是使用 Spring Boot。

标签: java spring spring-boot spring-security


【解决方案1】:

1) Spring Security 有通过Authorities过滤来控制访问的功能(在Authentication之后),但是没有通过登录所需信息过滤的功能。您需要业务逻辑来验证您是否在登录时尝试使用相应的 ID 和密码进行登录。

2) 如上所述,不提供ID和密码的访问控制。 我建议只为您请求的两个帐户创建授权。

3).antMatcher("/form").permitAll()

【讨论】:

  • 你的解释是错误的。第一个 antMatcher 元素限制了此配置适用的 URL。对于其他不适用的请求,可能是您的意思是解释但没有遇到。
  • @M.Deinum 你是对的。那么antMatcher第一次被限制,并没有具体说明怎么限制,但是如果你知道的话,能不能告诉我?
猜你喜欢
  • 2012-12-03
  • 2015-09-12
  • 2022-01-16
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 2015-12-07
相关资源
最近更新 更多