我想执行以下操作:
ex)
1. ClientA -----> http://服务器:8080 ----->服务(无基本身份验证)
2. ClientA -----> https://服务器:8443 ----->服务(具有基本身份验证)
3. ClientA -----> https://服务器:9443 ----->服务(无基本身份验证)
我考虑过使用以下代码,但是我不知道如何将“ 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);
}
}
}