[英]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 信任庫:
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),我懇請幫助。
PS 提交 GET 以外的任何 http 方法(即使是控制器不支持的方法):拋出相同的 403 - Forbidden。
PPS 使用 GET 調用不存在的資源會返回正確的 404 響應。 那么它與某處的HTTP方法設置有關嗎?
找出原因:我禁用了 csrf,現在它可以工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.