Spring 安全 X509 - 403 禁止访问

Spring Security X509 - 403 Forbidden

我正在尝试通过 X509 - 客户端认证对我的 spring boot rest API 实施单个客户端的身份验证。我已经创建了一个私钥:

openssl genrsa -des3 -out client.key 1024

为了创建此密钥,我设置了一些通用密码,如 changeit,此密码用于所有后续文件创建和请求。

接下来,我创建了一个证书签名请求:

openssl req -new -key client.key -out client.csr

并设置通用名称localhost(据我所知,其他字段无关紧要?)

接下来,使用 csr 创建证书:

openssl x509 -req -days 3650 -in client.csr -signkey client.key -out client.crt

接下来,创建 Java Truststore:

keytool -import -file client.crt -alias clientCA -keystore truststore.jks

最后,将密钥库创建为 .p12 文件:

openssl pkcs12 -export -in client.crt -inkey client.key -certfile client.crt -name "clientcert" -out keystore.p12

在 Spring 引导(2.0.2 版本)方面我有安全配置 class:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.x509();
}

/*
 * localhost is the CN of the client certificate
 */
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("localhost")
            .password("none")
            .roles("USER");
  }
}

和 application.properties 文件:

server.port=8443
logging.level.root=info

server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=changeit
server.ssl.trust-store=classpath:truststore.jks
server.ssl.trust-store-password=changeit
server.ssl.client-auth=need
security.headers.hsts=NONE

正在提交获取请求,例如

curl -vkE ./keystore.p12:changeit --cert-type P12  "https://localhost:8443/customers/test"

给了我一个有效的响应(故意更改或省略正确的证书会引发 SSL 错误),这很好。

现在:如果我尝试以与请求正文相同的方式提交 POST,例如

curl -kiE ./keystore.p12:changeit --cert-type P12 -X POST -H "Content-Type: application/json" --data @req-body.json  "https://localhost:8443/customers"

我收到以下回复:

HTTP/1.1 403
Set-Cookie: JSESSIONID=106214250C497F20C5F1AA02E554B316; Path=/; Secure; HttpOnly
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 16 Jul 2018 13:49:43 GMT

{"timestamp":"2018-07-16T13:49:43.335+0000","status":403,"error":"Forbidden","message":"Forbidden","path":"/customers"}%

我不知道为什么会发生这种情况(403),并请求帮助。

P.S。提交 GET 以外的任何 http 方法(即使是控制器不支持的方法):抛出相同的 403 - Forbidden。

P.P.S。使用 GET returns 调用一个不存在的资源一个很好的正确 404 响应。所以跟某处的HTTP方法设置有关?

找出原因:我禁用了 csrf,现在它可以工作了。