This is part 4 of a series of blog posts in which I walk through the creation of a simple chat app with Solace PubSub+. In the first part, I explained how to create a simple chat app that can send and receive messages via a direct topic subscription. In the second I explained how to modify the sample code so the application consumes messages from a queue, and in part 3 I explained how to add login functionalities to send a REST POST request to Solace PubSub+.

In this tutorial, you will learn how to add a simple authentication server. The server will first receive the client login request from the web application, then authenticate the user, and finally send a REST POST response back to the application to either allow or deny the login request.

Specifically, you will

  • Set the connection details
  • Create and send the REST POST request
  • Test the REST POST request

Prerequisite

Level

  • Beginner

Set the Connection Details

The setting of the connection details tells the Web application server where to send the REST POST login request to so that the Solace instance can receive it.

To set the connection details, do the following:

  1. In your code editor, check out the developer-exercise-4 branch or in your command line, enter git checkout remotes/origin/developer-exercise-4 -f
  2. In your code editor, enter mvn install to run a Maven install in solace-chat-common.
  3. Under src > main > resources, open the file application.properties.
    Simple Chat App with Solace
  4. Log in to Solace PubSub+ Cloud.
  5. Click the Connect tab.
    Simple Chat App with Solace - connect
  6. Open the REST section.
    Simple Chat App with Solace - rest section
  7. Copy the username, password, and REST host information from the Connection Details section and paste them to the application.properties file under src > main > java > resources.
    Simple Chat App with Solace - java resources

Create and Send the REST POST Request

Now the connection details are set, you can create and send the REST POST request to the PubSub+ instance as a Publish/Subscribe message:

  1. In your code editor, under src > main > java, open the file SolaceCloudProxy.java.
    Simple Chat App with Solace - SolaceCloudProxy
  2. Enter the following code to create a REST template object (Line 63).
    //Rest Request goes here
    RestTemplate restTemplate = new RestTemplate();
    

    RestTemplate makes HTTP REST calls and simplifies the interaction with HTTP servers.

  3. Enter the following code below the above code to create the HTTP entity with the user object type (Line 64).
    HttpEntity<UserObject> request = new HttpEntity<UserObject>(userObject,httpHeaders);

    The user authentication type contains just a username and password. solace-chat-common has the reference to it.

  4. Enter the following code to add the POST for the object to the REST template (Line 65).
    restTemplate.postForObject(solaceRESTHost + “/LOGIN/MESSAGE/REQUEST”,request, String.class);

    You defined solaceRESTHost at Step 7 of the “Set the Connection Details” section above.
    /LOGIN/MESSAGE/REQUEST is the specific topic that you are sending the REST POST request to.

  5. Enter the following code to return a response entity (Line 66).
    return new ResponseEntity(HttpStatus.OK);

    ResponseEntity creates an HTTP response.

    Test the REST Request

    Now you have created and sent the REST POST request, you can test whether the request is actually delivered to the Solace instance.

    1. In your code editor, enter mvn spring-boot:run to start the web application.
    2. In Solace PubSub+ Cloud, click the Try Me! tab.
      Simple Chat App with Solace - try me
    3. In the Subscriber panel, click the Connect button, enter LOGIN/MESSAGE/REQUEST as the topic, and click the Subscribe button.
      Simple Chat App with Solace - login-mesage
    4. Open a new browser and enter localhost:8081 to bring up the web application.
    5. Enter “user” as the username and password and click the Submit button.
      You should see the Try Me! tab has received the message.
      Simple Chat App with Solace - submit

    Congratulations! You have successfully added an authentication server, sent a REST POST request to the server as a Publish/Subscribe message, and received a response from it. In the next part, you will learn how to send the REST POST request as a Request/Reply message.

    Related

Hong Qiu
Hong Qiu

Hong is the Senior Community Manager at Solace, where he is responsible for building and managing the Solace community and creating developer-focused content. Prior to Solace, he spent over 10 years at Adobe managing the Adobe Developer Connection portals and planning, creating, and sourcing content for them. Before Adobe, he was a senior technical writer at Nortel and a technical writer at Cognos (now IBM).