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
Newsletter
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

  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • Member Spotlight: Shai Almong
  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 16, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
34.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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