1

I am working on a project which requires to send a WebPush notification to a desktop.Currently I am using Martijn Dwars library. Now, I was able to make it work, and the notifications are being sent seamlessly. However, on my server (Amazon EC2 Intance), I needed to deploy two wars (Production and development as part of development of POC).

Now what happening is the development war is working fine, however the prod war which is same as dev(Name and log path changed), upon sending the notification its throwing the following error:

 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

I doubt that its due to some conflict of a jar named Bouncy Castle which is also a sub-dependency of martijndwars.webpush (But I could be very wrong :P)

The method's code for sending the notification is :

    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>

Any help is really appreciated!

PS: Please let me know if any additional information is required in the comments :)

And, I have explored similar questions on web, couldn't find anything of help :P

Thanks a lot!

Community
  • 1
  • 1
Salim Shamim
  • 656
  • 10
  • 25
  • 1
    Have the same problem with nearly the exact same java code. Client tested the Application in Octobre last year, everything was working fine and now the same machine with same java versions, configuration, data etc. is getting these errors upon sending push notification. My developer deployment still works fine, though. – mondjunge Jan 30 '19 at 13:12

0 Answers0