【发布时间】:2016-09-03 10:54:06
【问题描述】:
我有一个包含 5 个 REST API 的 Web 应用程序。所有 API 都是启用了 SSL 的 HTTPS。 这是 server.xml 中的连接器标记:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="conf/jks/ketStore.jks" keystorePass="keystore" keystoreType="jks"
truststoreFile="conf/jks/trustStore.jks" truststorePass="truststore" truststoreType="jks"
clientAuth="true" sslProtocol="TLSv1.2"/>
现在我只需要使用单向 SSL 通过 HTTPs 公开其中一个 API。其他 4 个 API 应该只能通过带有 2-way SSL 证书的 HTTPS 访问。
使用 Spring boot 和 Spring 4 Security 解决此问题的最佳方法是什么。
更新
我在这方面取得了一些进展。我已经设置了clientAuth="want" 并且能够访问所需的 API 而无需在客户端提供证书。但是我不确定是否会为其他 API 强制执行 2-way 并编写自定义过滤器来检查 SSL 握手。有没有办法在 Spring Security 中做到这一点。
我有以下MultiHttpSecurityConfig 类:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MultiHttpSecurityConfig {
private static final Logger LOG = LoggerFactory
.getLogger(MultiHttpSecurityConfig.class);
@Configuration
@Order(1)
public static class SecureApiConfigurationAdapter extends
WebSecurityConfigurerAdapter {
@Autowired
private HttpAuthEntryPoint httpAuthEntryPoint;
@Autowired
private X509UserDetSer x509UserUserDetSer;
protected void configure(
final HttpSecurity http)
throws Exception {
LOG.debug("/SSL2waysecureAPI/");
http.csrf().disable()
.antMatcher("/SSL2waysecureAPI/**")
.x509()
.subjectPrincipalRegex("CN=(.*?),")
// .subjectPrincipalRegex(".*")
.authenticationUserDetailsService(x509UserUserDetSer)
.and().exceptionHandling()
.authenticationEntryPoint(httpAuthEntryPoint)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
Tomcat中新的connector标签如下:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="conf/jks/ketStore.jks" keystorePass="keystore" keystoreType="jks"
truststoreFile="conf/jks/trustStore.jks" truststorePass="truststore" truststoreType="jks"
clientAuth="want" sslProtocol="TLSv1.2"/>
【问题讨论】:
-
我认为这只是stackoverflow.com/questions/6441523/… - 在连接器中需要客户端身份验证应该确保那些需要http的端点也需要客户端身份验证
-
一个简单的方法是让前端服务器(Apache、Nginx、Haproxy 等)为您处理这些问题。您的 Tomcat 将无需 SSL,并且您的前端服务器将在需要时处理 SSL。
-
当我尝试调用 /SSL2waysecureAPI/** 而不指定客户端证书时,我的控制器被调用。我认为应该检查是否使用过滤器或其他东西来防止在没有客户端证书的情况下访问安全 URL?
-
也许问题是,您没有配置角色限制并且您的请求使用匿名用户/角色?
-
Manu,你成功了吗?
标签: java spring ssl spring-security spring-boot