使用WebPush Api部署两次类似的战争,导致InvalidKeySpecException:密钥规范无法识别

时间:2019-01-03 11:40:22

标签: java spring-boot tomcat bouncycastle web-push

我正在一个项目中,该项目需要将WebPush通知发送到桌面。当前我正在使用Martijn Dwars库。 现在,我能够使其正常运行,并且通知已无缝发送。但是,在我的服务器(Amazon EC2 Intance)上,我需要部署两次战争(生产和开发作为POC开发的一部分)。

现在发生的发展大战正在顺利进行,但是与prod(名称和日志路径已更改)相同的dev大战,在发送通知时引发了以下错误:< / p>

 ApplicationException: Unable to push notification
...
    Caused by: java.security.spec.InvalidKeySpecException: key spec not recognised
        at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
        at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
        at java.security.KeyFactory.generatePublic(Unknown Source)
        at nl.martijndwars.webpush.Utils.loadPublicKey(Utils.java:55)
        at nl.martijndwars.webpush.Notification.<init>(Notification.java:62)
        at com.services.NotificationService.sendDesktopNotification(NotificationService.java:74)
        ... 62 common frames omitted

我怀疑它是由于名为Bouncy Castle的jar的冲突而引起的,而这也是martijndwars.webpush的子依赖项(但我可能是非常错误的:P)

用于发送通知的方法的代码为:

    public void sendDesktopNotification(String bodyText) throws ApplicationException {
        String payload = getNotificationPayloadAsString(bodyText);

//      String endPoint = subscription.getEndPoint();
//      String p256dhKey = subscription.getP256dh();
//      String auth = subscription.getAuth();
        SubscriptionDTO sub = getRegisteredSubscription(demoUserName);

        String endPoint = sub.getEndPoint();
        log.info("Sending to endpoint: \n" + endPoint);
        String p256dhKey = sub.getP256dh();
        String auth = sub.getAuth();
        PushService ps = new PushService();
        Notification nf;
        try {

            nf = new Notification(endPoint,p256dhKey,auth,payload);
            ps.setPublicKey(pubkey);
            ps.setPrivateKey(pvtKey);
            HttpResponse response = ps.send(nf);
            log.info("Notification Response ReasonPhrase : "+ response.getStatusLine().getReasonPhrase());

        } catch (GeneralSecurityException | IOException | JoseException | ExecutionException | InterruptedException | HttpClientErrorException | HttpServerErrorException e) {
            throw new ApplicationException("Unable to push notification",e);
        }
    }

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.salimshamim</groupId>
    <artifactId>ITSD</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>

    <name>ITSD</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.json/json -->



        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>nl.martijndwars</groupId>
            <artifactId>web-push</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
    </dependencies>

    <pluginRepositories>
        <pluginRepository>
            <id>repository.spring.release</id>
            <name>Spring GA Repository</name>
            <url>https://repo.spring.io/plugins-release/</url>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <finalName>itsd-dev</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

我们非常感谢您的帮助!

PS:请让我知道评论中是否需要其他信息:)

而且,我在网络上浏览了类似的问题,找不到任何帮助:P

非常感谢!

0 个答案:

没有答案