我試圖在我們的應用程序的tomcat服務器中啓用HPKP證書鎖定。我添加了一個過濾器來包含HSTS和HPKP指令。按照預期,我在響應頭中獲得了HSTS和HPKP指令。用於啓用HPKP的Tomcat篩選器
但即使在我更改服務器中的證書或攔截請求併發送假證書後,瀏覽器也不會停止請求。瀏覽器具有HPKP功能,因爲當請求被攔截並顯示假證書時,它會阻止其他啓用了HPKP的網站(如Facebook)。
只是想知道是否有什麼錯誤的方式我生成公鑰的散列,因此瀏覽器不鎖住鑰匙?任何反饋如果任何人已啓用HPKP的tomcat?
過濾器在web.xml加入
<filter>
<filter-name>HttpsSecFilter</filter-name>
<filter-class>com.x.x.x.x.x.HttpsSecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpsSecFilter</filter-name>
<url-pattern>..[url-pattern]..</url-pattern>
</filter-mapping>
過濾代碼(修改爲僅包括爲了簡潔相關部分)
public class HttpsSecurityFilter implements Filter {
private boolean _isHTTSEnabled = false;
private long _httsMaxAge = -1;
private boolean _isHPKPEnabled = false;
private long _hpkpMaxAge = -1;
private String _hpkpCertHashesString = null;
private boolean _includeSubdomains = false;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (_isHTTSEnabled && response instanceof HttpServletResponse) {
((HttpServletResponse) response).setHeader("Strict-Transport-Security",
"max-age=" + _httsMaxAge);
}
if (_isHPKPEnabled && response instanceof HttpServletResponse) {
((HttpServletResponse) response).setHeader("Public-Key-Pins",
_hpkpCertHashesString);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
//Read from application configuration file
_isHTTSEnabled = true;
_httsMaxAge = 5184000;
_isHPKPEnabled = true;
_hpkpMaxAge = 5184000;
_includeSubdomains = false;
if (_isHTTSEnabled) {
try {
KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream is = new FileInputStream(APP_HOME + "/conf/keystore");
keyStore.load(is, keysotrePassword);
is.close();
Certificate cert = keyStore.getCertificate(myappcetificatealias);
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(cert.getPublicKey().getEncoded());
_hpkpCertHashesString = "pin-sha256=\"" + java.util.Base64.getEncoder()
.encodeToString(digest) + "\"; max-age=" +
_hpkpMaxAge;
} catch (Exception e) {
;
}
}
}
}
從響應的頭指令
Public-Key-Pins:pin-sha256="JdZ3itf02UwsCav0X26wcSQLGfo="; max-age=5184000
Strict-Transport-Security:max-age=5184000
他問tomcat不是apache2 –