我使用Metro堆栈在java中创建了2个SOAP Web服务 . 为了防止意外的请求,只有在请求者拥有客户端证书的情况下才能进行这些请求 . 为此,web.xml看起来像下面的代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>PadronExterno</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>WebServicePort</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>TomcatStartupServlet</servlet-name>
<servlet-class>com.company.TomcatStartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebServicePort</servlet-name>
<url-pattern>/theWebService</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name>Constraint1</display-name>
<web-resource-collection>
<web-resource-name>theWebService</web-resource-name>
<description></description>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<description></description>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>CLIENT-CERT</auth-method>
</login-config>
</web-app>
最近我开发了一个全新的肥皂ws,但我想尝试Spring Boot . 我开始使用Spring Initializr . Web服务完全编码,完成,但它缺少SSL身份验证/授权的一部分 .
编辑:
我想出了一个可能的解决方案,但我遗漏了一些东西 . 到目前为止,这是我能够写的:
@Configuration
@EnableWebSecurity
public class SecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private Configuracion config;
@Override
protected void configure(HttpSecurity http) throws Exception {
// Add support for HSTS
http
.headers()
.httpStrictTransportSecurity()
.includeSubDomains(true)
.maxAgeInSeconds(31536000);
// Disable HTTP
http.httpBasic().disable();
Integer httpPuerto = config.getHttpPuerto();
Integer httpsPuerto = config.getHttpsPuerto();
http
.portMapper()
.http(httpPuerto)
.mapsTo(httpsPuerto);
http.requiresChannel().anyRequest().requiresSecure();
}
}
不幸的是,当我运行到我的应用服务器(启用了SSL / TLS的Tomcat 8.5)时,您可以在不拥有客户端证书的情况下运行它 . 根据要求,这里是我的Tomcat的连接器配置:
<Connector
connectionTimeout="20000"
port="9090"
protocol="HTTP/1.1"
redirectPort="9443"/>
<Connector
SSLEnabled="true"
keystorePass="***d"
keystoreType="JKS"
maxThreads="200"
port="9443"
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
scheme="https"
secure="true"
sslImplementationName="org.apache.tomcat.util.net.jsse.JSSEImplementation"
sslProtocol="TLSv1.2"
clientAuth="want"
keystoreFile="D:\apache\Tomcat8.5\certs\tomcat.jks"
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443"/>
ciphers="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA" />
我错过了 WebSecurityConfigurerAdapter
中的内容吗?谢谢
1 回答
你首先说你有一个没有Spring的工作示例,但是看看Tomcat配置似乎缺少一些部分(例如具有客户端证书颁发机构的truststorefile)
按照此处提供的步骤进行操作Mutual authentication with Tomcat 7
例如,要生成服务器和客户端证书:
然后配置server.xml:
在浏览器中配置了Tomcat和客户端证书后,它应该要求它,但应用程序将因HTTP403错误而失败,因为您的spring配置需要HTTPS,但它没有指定它需要客户端认证(正如您在Web中所做的那样) .xml没有 spring ) .
你需要在SecurityConfigurerAdapter中指定:
它应该最终奏效 .
如果要将其作为Tomcat外部的独立spring-boot执行,则还应在application.properties中进行配置
编辑:Tomcat中的信任库应仅包含客户端使用的根证书,而不包含实际的客户端证书