限制消费者应用程序仅访问后端应用程序的一个 API 端点,并限制其他端点的访问

问题描述 投票:0回答:1

我的后端应用程序有两个使用 springboot 3.x 和 java 17 开发的消费者应用程序。我的前端(FE)消费者应用程序可以访问所有 API 端点,而对于其他应用程序,我只想授予对 1 个 API 端点(/usr /abc/1) 基于身份验证。

这是我在后端应用程序中添加 securityconfig.java 文件的代码。但是,它的工作方式是,如果我按以下方式保持 bean 的顺序,它就可以访问所有端点的应用程序。但是,如果我交换订单,那么它会限制我的 FE 应用程序对我不想要的所有端点的访问,但适用于第二个应用程序,我只能访问一个端点(/usr/abc/1),而不是其他端点。

有人可以建议这出了什么问题吗?

@Bean
@Order(1)
SecurityFilterChain abcFilter1(HttpSecurity http) throws Exception {
     CorsConfiguration cc = new CorsConfiguration();
     cc.setAllowedHeaders(List.of("Authorization", "Cache-Control", "Content-Type"));
     cc.setAllowedOrigins(List.of("*"));
     cc.setAllowedMethods(List.of("GET", "POST","OPTIONS")); 
     cc.setAllowedOrigins(Arrays.asList("http://localhost:3000","example.com/“));
     cc.setAllowCredentials(true);
     cc.setExposedHeaders(List.of("Authorization"));

     http.cors(Customizer.withDefaults()).csrf().disable();
     http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
     http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer())
        .and()
        .authorizeHttpRequests()
        .requestMatchers("/", "/health").permitAll()  
        .anyRequest().authenticated();

return http.build();

}

@豆豆 @订单(2) public SecurityFilterChain abcFilter2(HttpSecurity http) 抛出异常 {

http.cors(Customizer.withDefaults()).csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer())
        .and()
        .authorizeHttpRequests()
        .requestMatchers("/usr/abc/1").authenticated()
        .requestMatchers( "/","/health").permitAll()  
        .anyRequest().denyAll();

return http.build();

}

我想根据身份验证仅向 1 个 API 端点 (/usr/abc/1) 授予对第二个消费者应用程序的访问权限,同时对所有端点进行完全访问(基于身份验证)。

spring-boot api endpoint restriction
1个回答
0
投票

由于您不通过

SecurityFilterChain
限制对
HttpSecurity#securityMatchers
的访问,因此两个过滤器链都对所有端点负责。话虽如此,队列中的第一个获胜。所以你要做的是:

  1. 限制对过滤器链的访问
  2. 给过滤器下订单
  3. (可选):当您想将最后一个过滤器设置为默认值时,可以省略对最后一个过滤器的
    securityMatchers
    限制,这样任何之前未由过滤器处理的请求都会在此处结束

这将是您的配置

SecurityFilterChain

@Bean
@Order(10)
public SecurityFilterChain abcFilter1(HttpSecurity http) throws Exception {

    http.cors(Customizer.withDefaults()).csrf().disable();
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);

    http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer()).and()

        .securityMatchers(matchers -> matchers.requestMatchers("/usr/abc/1"))

        .authorizeHttpRequests()
        .anyRequest().authenticated();

    return http.build();
}

@Bean
@Order(20)
public SecurityFilterChain abcFilter2(HttpSecurity http) throws Exception {

     CorsConfiguration cc = new CorsConfiguration();
     cc.setAllowedHeaders(List.of("Authorization", "Cache-Control", "Content-Type"));
     cc.setAllowedOrigins(List.of("*"));
     cc.setAllowedMethods(List.of("GET", "POST","OPTIONS")); 
     cc.setAllowedOrigins(Arrays.asList("http://localhost:3000","example.com/"));
     cc.setAllowCredentials(true);
     cc.setExposedHeaders(List.of("Authorization"));

     http.cors(Customizer.withDefaults()).csrf().disable();
     http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);

     http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer()).and()

        .authorizeHttpRequests()
        .requestMatchers("/", "/health").permitAll()  
        .anyRequest().authenticated();

    return http.build();
}
© www.soinside.com 2019 - 2024. All rights reserved.