如何为每个连接器配置身份验证方式

时间:2019-10-15 00:42:58

标签: spring-boot http authentication https connection

我想执行以下操作:

  1. 使用Spring Boot创建一项服务。
  2. 可以在不同端口上接收多个http(https)访问。

ex)
1. ClientA -----> http://服务器:8080 ----->服务(无基本身份验证)
2. ClientA -----> https://服务器:8443 ----->服务(具有基本身份验证)
3. ClientA -----> https://服务器:9443 ----->服务(无基本身份验证)

  1. 我不想使用在“ application.properties”中指定端口号的方式。

我考虑过使用以下代码,但是我不知道如何将“ WebSecurityConfigurerAdapter”与“ TomcatServletWebServerFactory”关联。

我想知道如何为每个连接器配置身份验证方式。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.inMemoryAuthentication().withUser("userid").password("{noop}passwd").roles("USER");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/DocumentB/**");
        http.authorizeRequests().anyRequest().authenticated();
        http.httpBasic();       // Basic authentication
        http.csrf().disable();
    }
}

public class HttpsConnectorA {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        return tomcat;
    }
    private Connector createSslConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        try {
            connector.setScheme("https");
            connector.setSecure(true);
            connector.setPort(8443);
            protocol.setSSLEnabled(true);
            protocol.setKeystoreType("PKCS12");
            protocol.setKeystoreFile("C:\\ssl\\server.p12");
            protocol.setKeystorePass("pass1");
            protocol.setKeyAlias("tomcat");
            return connector;
        }
        catch (Exception ex) {
            throw new IllegalStateException("can't access keystore: [" + "keystore" + "] or truststore: [" + "keystore" + "]", ex);
        }
    }
}

0 个答案:

没有答案