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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Mahesh Patro user avatar by
Mahesh Patro
·
May. 16, 17 · Tutorial
Like (14)
Save
Tweet
Share
31.16K 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.

Popular on DZone

  • mTLS Everywere
  • Introduction to Container Orchestration
  • Stop Using Spring Profiles Per Environment
  • Building Microservice in Golang

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: