当前位置: 首页 > 知识库问答 >
问题:

spring添加新依赖项时无法从String转换为RSAPUblickey/RSAPRivateKey

琴献
2023-03-14

我有一个spring安全的微服务,我有application.yml中的私钥和公钥的内容(请不要评判我)。我还有一个带有这个属性的@ConfigurationProperties文件。该服务工作良好,内部RsaKeyConversionServicePostProcessor分别执行从String到RSAPUblickey/RSAPrivateKey的转换。

问题是当我在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>

提前道谢。

共有1个答案

庾君博
2023-03-14

似乎由于某种原因没有加载属性转换器。我相信这是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继承。

 类似资料:
  • 下面是我的pom.xml文件 http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0

  • npm是否有安装依赖作为对等依赖的选项,如yarn选项,而不是手动添加它例如: 感谢@Broncha,更新问题的更多说明 问题是如何向项目添加对等依赖。那就是 将依赖项添加到package.json中的“dependencies”中, 如何安装将其添加到package.json?中的“对等依赖”的依赖项

  • 我想运行比较xml响应的Junit。为此,我想使用org。xmlunit和我在pom中添加了依赖项,如下所示 然而,当我运行maven安装时,我发现了以下错误。我在eclipse(Mars发行版(4.5.0))中使用嵌入式maven。有人能帮我解决这个错误吗。 [错误]未能在项目springWSSecurityCertOrderSvc上执行目标:无法解决项目com.anvesh.spring.ws

  • 我使用的是eclipse 2018、gradle 5.2.1、buildship 3.0.1。 我的配置看起来像: 我尝试根据building-spring-boot-2-projects-with-gradle创建spring boot 2 是: 但是,在我保存构建之后。gradle,项目和外部依赖关系消失,Spring Bootjar也没有下载。 我错了什么? 如果我使用gradle by s

  • 如何将此WorldEdit依赖项添加到Maven项目中?http://maven.sk89q.com/artifactory/repo/com/sk89q/worldedit/worldedit-bukkit/我需要6.1.1快照。 是否有算法来获取组ID工件ID和版本?

  • 错误:无法初始化主类com.companyname.bank.App,原因是:java.lang.NoClassDefFoundError:org/apache/http/client/ResponseHandler 我在pom.xml文件中添加了依赖项,在/src/lib中也添加了相关的.jar文件之后,这个报告一直出现。真的很困惑,不知道怎么解决。 请帮我一把。谢谢。 以下是我的操作流程: >