DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Revolutionize Your MuleSoft Deployments With GitOps
  • MuleSoft Anypoint Platform Audit Logs to Splunk
  • Mule 4 Custom Policy Example

Trending

  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Mulesoft Basic Authentication With HTTPS

Mulesoft Basic Authentication With HTTPS

With Mulesoft's growing popularity, it's increasingly important to secure your Mule projects. Learn how to use Basic Authentication and HTTPS to secure your API.

By 
Mahesh Patro user avatar
Mahesh Patro
·
May. 16, 17 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
33.8K Views

Join the DZone community and get the full member experience.

Join For Free

I have read multiple articles to find out how I can secure my API. There are multiple ways you can secure newly created APIs. One of them is if you deploy on CloudHub and you declare the policy, it easily secures. But its a paid service. If you want to secure with extra few lines of code in your flow then this article will show you how to secure your API with Basic Authentication over HTTPS. 

As you know, Mule is built on top of the Spring framework, so you can use spring authentication in your-app.xml. For example, I have created a project customer; and after creating my api.raml file, and generating the API, the project would look like this: 

Image1: API with no security

Once our API is created, it's time to implement Basic Authentication. 

1. Define in RAML

In the api.raml file's header section, use the following: 

securitySchemes:
    - basic:
        type: Basic Authentication


Then, inget: method define your file as:

 securedBy: [basic] 

The api.raml file should look like this:

Image title

This enables the console window to ask for a username and password, but it's still not secure. 

Now in our api.xml file, add the following lines of code to enable basic authentication:

<spring:beans>
   <ss:authentication-manager alias="authenticationManager">
     <ss:authentication-provider>
       <ss:user-service id="userService">
          <ss:user name="admin" password="admin" authorities="ROLE_ADMIN" />
          <ss:user name="user1" password="work4proj" authorities="ROLE_DEV" />
        </ss:user-service>
    </ss:authentication-provider>
  </ss:authentication-manager>
</spring:beans>
<mule-ss:security-manager name="muleSecurityManager" doc:name="Spring Security Provider">
  <mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
</mule-ss:security-manager>


Note: Please add security name spaces in the namespace declaration. 

xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security"
xmlns:ss="http://www.springframework.org/schema/security"

http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd"


Your api.xml file should look like this:

Image title

Note that this is an in-memory provider. You could easily change to an LDAP or a DAO provider. 

Now apply security into the flow by adding the following line of code: 

<mule-ss:http-security-filter realm="mule-realm" securityProviders="memory-provider"/>

Image title

Here is the console window from IE and an actual API call from Postman.

Image title


Image title


Try executing your code with the wrong password first, in order to verify it will not allow you to execute an incorrect username/password combination. 

Note: if you try to execute with the correct password first, then you may need to clean the cache as there is no logout in basic authentication. 

Once you have secured your Mulesoft project with Basic Authentication, it is time to secure it with HTTPS. For that, you will need to follow a three-step process:

1. Create keystore.jks using following command line tool (jdk bin must be in path variable)

keytool -genkeypair -keystore keystore.jks   -dname "CN=localhost, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown"  -keypass password  -storepass password  -keyalg RSA  -sigalg SHA1withRSA  -keysize 2048  -alias mule  -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999


2. Copy the newly created keystore.jks to a resource folder in your project. 

Image title

3. Open the HTTP connector properties window, select the protocol as HTTPS, and in the TLS/SSL tab enter Key Store Configuration as shown below:

Image title

Image title

Please note that the Key Password and password have to match with the password you mentioned while creating keystore.jks in Step 1. 

Now, revisit your console window with https://localhost:8082/customer/console

Image title

Click Continue as the keystore you created is not yet trusted (IE/Chrome will throw up a warning).

Cloud Deployment Note:

If you are deploying in the cloud, then you would need to change your port entry in api.properties from http.port to https.port.

HTTPS authentication MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Revolutionize Your MuleSoft Deployments With GitOps
  • MuleSoft Anypoint Platform Audit Logs to Splunk
  • Mule 4 Custom Policy Example

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook