当添加新的依赖项时,Spring无法从String转换为rsappublickey / rsprivvatekey.&l



我有一个使用Spring Security的微服务,我在应用程序中有私钥和公钥的内容。Yml(请不要评判我)。我还有一个带有这些属性的@ConfigurationProperties文件。服务工作得很好,内部rsakeyconverversionservicepostprocessor分别从String转换到rsappublickey/rsprivvatekey。

问题是,当我在pom.xml中添加flyway-core依赖时。Spring无法进行转换。这个微服务不是我的,所以我不能把键从属性移到文件并从那里读取它。

你知道会发生什么事吗?

application.yml

lorem:
ipsum:
dolor:
jwt:
private-key: |
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
public-key: |
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

房地产类

@ConfigurationProperties(prefix = "lorem.ipsum.dolor.jwt")
@Component
class SecurityProperties {
private RSAPrivateKey privateKey;
private RSAPublicKey publicKey;
}

错误日志


***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'lorem.ipsum.dolor.jwt.public-key' to java.security.interfaces.RSAPublicKey:
Property: augcod.security.authentication.jwt.public-key
Value: -----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
Origin: class path resource [application-local.yml] - 34:21
Reason: No converter found capable of converting from type [java.lang.String] to type [java.security.interfaces.RSAPublicKey]
Action:
Update your application's configuration

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>lorem.ipsum.dolor</groupId>
<artifactId>sit-amet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-saml2-service-provider</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.66</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.66</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>7.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- To support Junit 4 tests -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-deps</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<silent>true</silent>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

提前感谢。

似乎由于某种原因没有加载属性转换器。我认为这是Spring Boot自动配置中的错误。

如果你想立即解决它,那么你可以自己实现转换器。只需使用相同的Spring类。

像这样:

package lorem.ipsum.dolor.sitamet;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.converter.RsaKeyConverters;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.security.interfaces.RSAPrivateKey;
@Component
@ConfigurationPropertiesBinding
public class MyPrivateKeyConverter implements Converter<String, RSAPrivateKey> {
@Override
public RSAPrivateKey convert(String from) {
return RsaKeyConverters.pkcs8().convert(new ByteArrayInputStream(from.getBytes()));
}
}
package lorem.ipsum.dolor.sitamet;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.converter.RsaKeyConverters;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.security.interfaces.RSAPublicKey;
@Component
@ConfigurationPropertiesBinding
public class MyPublicKeyConverter implements Converter<String, RSAPublicKey> {
@Override
public RSAPublicKey convert(String from) {
return RsaKeyConverters.x509().convert(new ByteArrayInputStream(from.getBytes()));
}
}

同样,您不需要在pom.xml中指定flyway-core的版本。继承自spring-boot-starter-parent

最新更新