简体   繁体   English

无法了解Spring安全性的行为

[英]Unable to understand the behavior of Spring security

I am using spring boot 2.1.4 with dependencies of actuator. 我正在使用带有启动器依赖项的spring boot 2.1.4。 I wanted to configure separate authentication and authorization mechanisms for actuator and my application. 我想为执行器和我的应用程序配置单独的身份验证和授权机制。 I read the Multiple HttpSecurity and configured my WebSecurityAdapter as follows: 我阅读了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();
    }
  }*/

}

Note: I have disabled form Login temporarily 注意:我已暂时禁用表单登录

When I run a curl request 当我执行curl请求时

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

I am able to see the output. 我能够看到输出。 Its as though the spring security never existed. 好像弹簧安全性从未存在过。 When I enable form login, I get the spring login screen. 启用表单登录后,将显示spring登录屏幕。 The other behavior that I observed is if I interchange the username and password of /locations with the actuator username and password, I still get a valid response back. 我观察到的另一个行为是,如果将/ locations的用户名和密码与执行器的用户名和密码互换,我仍然会收到有效的响应。

I understand the form login is more of a fallback but I want to disable the form login (probably we may move to cas) and use authentication and authorization only based on the spring security httpBasic. 我知道表单登录更多是一种后备方式,但是我想禁用表单登录(可能我们可能会改用cas),并且仅基于Spring Security httpBasic使用身份验证和授权。 I am not able to understand the mistake I am making. 我无法理解我正在犯的错误。

My requirement is finally : 我的要求终于是:

1) a request to /organizations or /locations etc should be accessible only if the username password is "user" and "password" 1)仅当用户名密码为“ user”和“ password”时,才可以访问对/ organizations或/ locations等的请求

2) a request to /manage which is the actuator api should be accessible only if the username and password and role matches with the actuator username and password. 2)仅当用户名和密码以及角色与执行器用户名和密码相匹配时,才可以访问作为执行器api的对/ manage的请求。

3) Any other API can be permitAll / form login 3)任何其他API都可以被允许所有/表单登录

How do i go about achieving this? 我该如何实现这一目标?

1) Spring Security has a function to control access by filtering by Authorities(after Authentication), but there is no function to filter by the information required for login. 1)Spring Security具有通过权限(在身份验证之后)进行过滤来控制访问的功能,但是没有功能可以根据登录所需的信息进行过滤。 You need business logic to verify that you are attempting to log in with the corresponding ID and password during login. 您需要业务逻辑来验证您是否尝试在登录期间使用相应的ID和密码登录。

2) As mentioned above, access control with ID and password is not provided. 2)如上所述,未提供具有ID和密码的访问控制。 I recommend creating Authorities for only the two accounts you requested. 我建议仅为您请求的两个帐户创建“单位”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM