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
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:
git checkout remotes/origin/developer-exercise-4 -f
mvn installto run a Maven install in solace-chat-common.
Now the connection details are set, you can create and send the REST POST request to the PubSub+ instance as a Publish/Subscribe message:
//Rest Request goes here RestTemplate restTemplate = new RestTemplate();
RestTemplate makes HTTP REST calls and simplifies the interaction with HTTP servers.
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.
restTemplate.postForObject(solaceRESTHost + “/LOGIN/MESSAGE/REQUEST”,request, String.class);
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.
return new ResponseEntity(HttpStatus.OK);
ResponseEntity creates an HTTP response.
Now you have created and sent the REST POST request, you can test whether the request is actually delivered to the Solace instance.
mvn spring-boot:runto start the web application.
localhost:8081to bring up the web application.
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.