首页 > 解决方案 > How do I solve the error: Failed to load keystore type [PKCS12 ]

问题描述

I want to cobfigure the use of https at a secured endpoint in Spring Boot I geenerate a certificate in PKCS12 format and place the generated certificate in under the resource foldere When I run the gradle build command I recive follwing error

2020-12-15 22:03:11.093  INFO 14592 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@60c8a093, org.springframework.security.web.context.SecurityContextPersistenceFilter@2f2bff16, org.springframework.security.web.header.HeaderWriterFilter@599e4d41, org.springframework.security.web.csrf.CsrfFilter@36681447, org.springframework.security.web.authentication.logout.LogoutFilter@7efb53af, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@333c8791, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@588f63c, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@44cffc25, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1457fde, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@fc807c1, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@7ecec90d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2a369e14, org.springframework.security.web.session.SessionManagementFilter@10f7c76, org.springframework.security.web.access.ExceptionTranslationFilter@70887727, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@237f7970]
2020-12-15 22:03:11.388 ERROR 14592 --- [           main] org.apache.tomcat.util.net.SSLUtilBase   : Failed to load keystore type [PKCS12 ] with path [file:/C:/Users/stein.PC01/Development/Tutorial/BasicAutentication/build/resources/main/certificate.p12%20] due to [PKCS12  not found]

java.security.KeyStoreException: PKCS12  not found
        at java.base/java.security.KeyStore.getInstance(KeyStore.java:871) ~[na:na]
        at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:184) ~[tomcat-embed-core-9.0.35.jar:9.0.35]

The proerty file looks like this

server.ssl.key-store-type=PKCS12 
server.ssl.key-store=classpath:certificate.p12 
server.ssl.key-store-password=XXXXXXX

The main program

package com.laurentiuspilca.ssia;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

}

The controller look likes this

package com.laurentiuspilca.ssia.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello!";
    }
}

The SSL generation command are openssl req -newkey rsa:2048 -x509 -keyout key.pem -out cert.pem -days 365 openssl pkcs12 -export -in cert.pem -inkey key.pem -out certificate.p12 -name "certificate"

The gradle file look like this:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.4.1'
}

repositories {
    mavenLocal()
    maven {
        url = uri('https://repo.maven.apache.org/maven2/')
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security:2.3.0.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.3.0.RELEASE'
    testImplementation 'org.springframework.security:spring-security-test:5.3.2.RELEASE'
    testImplementation 'io.rest-assured:spring-mock-mvc:4.3.1'
    testImplementation  'io.rest-assured:rest-assured-common:4.3.1'
}

group = 'com.laurentiuspilca'
version = '0.0.1-SNAPSHOT'
description = 'Hello World with user and password'
java.sourceCompatibility = JavaVersion.VERSION_1_8


tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

标签: javasslspring-securityhttpspkcs#12

解决方案


我不确定,但问题在classpath:certificate.p12. 错误是resources/main/certificate.p12%20],%20代表空白字符。你能检查有没有空白?还是缺少证书文件resources/main/certificate.p12


推荐阅读