Spring Security X509-禁止的403

时间:2018-07-16 13:29:16

标签: java spring security ssl-certificate x509

我正在尝试通过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 boot(2.0.2版)方面,我具有安全配置类:

@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-禁止。

P.P.S。使用GET调用不存在的资源会返回一个不错的404响应。因此,它与某处的HTTP方法设置有关吗?

1 个答案:

答案 0 :(得分:1)

找出了原因:经常被忽略的平庸性,我禁用了csrf,现在它起作用了。