【问题标题】:Implement Both MTLS(Two way SSL ) and public Endpoints (No SSL check) in Spring boot Application在 Spring Boot 应用程序中实现 MTLS(双向 SSL)和公共端点(无 SSL 检查)
【发布时间】:2022-01-05 08:09:13
【问题描述】:

我有一个带有一些端点的 Spring Boot 应用程序

/.wellknown 不需要任何身份验证并且对公众开放

/callback 需要 mtls(仅证书认证)。

对于上述要求,我找不到确切的实现。大部分实现

  1. find 在服务器级别启用 mtls - 这意味着所有 APIS 都已启用 mtls。

  2. 使用 X.509 证书检查来检查证书和用户数据(在我的情况下,不涉及用户数据 - 它是唯一的服务器到服务器 mtls)。

在下面找到代码

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    /*
     * Enables x509 client authentication.
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .authorizeRequests()
                    .anyRequest()
                        .authenticated()
                .and()
                    .x509()
                .and()
                    .sessionManagement()
                        .sessionCreationPolicy(SessionCreationPolicy.NEVER)
                .and()
                    .csrf()
                        .disable();
        // @formatter:on
    }
 
    /*
     * Create an in-memory authentication manager. We create 1 user (localhost which
     * is the CN of the client certificate) which has a role of USER.
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("localhost").password("none").roles("USER");
    }
}

请帮助如何实现相同的 -

  1. /callback - 检查客户端是否提供证书并在信任库中可用

  2. /.well-known/ No mtls check response without any check

【问题讨论】:

标签: spring spring-boot spring-mvc spring-security spring-security-rest


【解决方案1】:

首先,我建议您使用最新的方法来配置 Spring Security,因为 WebSecurityConfigurerAdapter 很快就会被弃用。

实现您的方案的方式是使用 2 个SecurityFilterChain,一个用于/.well-known 路径,另一个用于其余路径,使用requestMatchers,如下所示:

@EnableWebSecurity
public class SecurityConfig {

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SecurityFilterChain wellKnownSecurity(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .requestMatchers((requests) -> requests
                    .antMatchers("/.well-known")
                )
                .authorizeHttpRequests((authorize) -> authorize
                        .anyRequest().permitAll()
                );
        // @formatter:on
        return http.build();
    }

    @Bean
    public SecurityFilterChain defaultSecurity(HttpSecurity http) throws Exception {
        http
            .authorizeRequests((authorize) -> authorize
                .anyRequest().authenticated()
            )
            .x509(Customizer.withDefaults())
            .sessionManagement((session) -> session
                 .sessionCreationPolicy(SessionCreationPolicy.NEVER)
            )
            .csrf(CsrfConfigurer::disable);
        // @formatter:on
        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(user);
    }

}

这样,wellKnownSecurity 链将应用于/.well-known 端点,允许所有请求,defaultSecurity 链将应用于所有其他请求,需要x509

【讨论】:

猜你喜欢
  • 2016-09-03
  • 2020-02-22
  • 2016-02-21
  • 2018-08-08
  • 2010-12-06
  • 2020-05-16
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多