Tech Blog

Coding

Authorization In HiveMQ Using Extension

By Yash chandvani2 min read
← Back to blog

In the previous blog, we discussed adding authentication in a HiveMQ extension project. Now to move forward, In this blog, we will learn how to do a custom authorization in HiveMQ using the extension.

First, we need to have a basic project for the HiveMQ extension. If we don’t have one we can create our own basic project as discussed in How to create a custom HiveMQ extension?

To understand why we need authorization in HiveMQ we can take a scenario below:

In our HiveMQ project, the user should publish messages with predefined algorithms. If the messages aren’t encrypted, the broker should discard the packet.

By default, MQTT does not provide any way to authorize data. We can achieve the same by using the custom extension.

The steps to create our own custom extension :

  • First we create a new java class MyPublishAuthorizer and implement PublishAuthorizer
Authorization In HiveMQ Using Extension
  • Add below method, which will call method to decrypt or check that message published by the client is valid or not
public Boolean isValidMsg(String payload) {
		//implement your custom logic or decrypt payload to check message is valid or not?
		
		return false;
	}
  • We have to add the below code inside the authorizePublish method:
try {
final PublishPacket publishPacket = publishAuthorizerInput.getPublishPacket();        
String clientID = publishAuthorizerInput.getClientInformation().getClientId().toString();
        String msg = publishAuthorizerInput.getPublishPacket().getPayload().toString();


        if (isValidMsg(msg)) {
            publishAuthorizerOutput.authorizeSuccessfully();
        } else {
            publishAuthorizerOutput.failAuthorization();
        }
    } catch (Exception ex) {
        System.out.println("Exception");
    }
Authorization In HiveMQ Using Extension
  • Next we have to create a new java class which implements AuthorizerProvider and then add the below code:
return new MyPublishAuthorizer();
Authorization In HiveMQ Using Extension
  • Lastly we have to add the code given below inside the extensionStart method of the main class:
Services.securityRegistry().setAuthorizerProvider(new MyAuthorizerProvider());
Authorization In HiveMQ Using Extension

The broker will check and decrypt each and every message which will reduce the slight performance in real-time based on your processing logic on the message.

In the next blog, we will learn about monitoring HiveMq in Grafana. Stay tuned!

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →