Basic Authentication in Mule 4
Basic Authentication is an authentication mechanism by which we can secure our APIs. It is built upon HTTP protocol. The client needs to send an Authorization HTTP header as a combination of username and password with HTTP request. Mule app will verify those headers and the client is able to access the application. If it fails to verify it will give 401 - Unauthorized error.?
Steps to implement Basic Authentication
Step 1: Create a project and add spring module
Step 2: Add beans.xml file in src/main/resources folder.?
<beans xmlns="https://www.springframework.org/schema/beans"
??xmlns:context="https://www.springframework.org/schema/context"
??xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
??xmlns:ss="https://www.springframework.org/schema/security"
??xsi:schemaLocation="https://www.springframework.org/schema/beans
????https://www.springframework.org/schema/beans/spring-beans.xsd
????https://www.springframework.org/schema/context
????https://www.springframework.org/schema/context/spring-context.xsd
????https://www.springframework.org/schema/security
https://www.springframework.org/schema/security/spring-security.xsd">
??<ss:authentication-manager alias="authenticationManager">
????<ss:authentication-provider> ?
?????<ss:user-service id="userService"> ?
???????<ss:user name="silverline" password="123456789" authorities="ROLE_ADMIN" />
? </ss:user-service>
????</ss:authentication-provider>
??</ss:authentication-manager>
</beans>
Step 3: Add Spring config and Spring Security manager as Global elements.?
<spring:config name="springConfig" files="beans.xml" /
<spring:security-manager doc:name="Spring Security manager" doc:id="58abd80e-40ed-4025-96e6-3d087e6c2e0b" >
<spring:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
</spring:security-manager>>
?
Step 4: Add Basic Security filter component and specify reals as “mule” in the flow after HTTP Listener.
Step 5: Add Authorization filter component with Required Authorities as ROLE_ADMIN and a logger in the last of the flow.
Step 6: Add beans.xml files path in mule.artifact file
{
"minMuleVersion": "4.3.0",
"classLoaderModelLoaderDescriptor": {
????????"id": "mule",
????????"attributes": {
????????????"exportedResources": [
????????????????"beans.xml"??
???????????????????????]
????????}
????}
}
Step 7: Deploy the app and hit the application using postman. Set Authorization to Basic Auth and provide username and password as required.
As required authority is ROLE_ADMIN, only requests with the admin’s credentials will be passed further.?
Change username/password to incorrect combination and requests will fail with HTTP: BASIC_AUTHENTICATION error.
Useful tutorial. I have used it successfully wwith Spring 1.3.6. Have you tried this procedure with Spring 1.3.9? Because it seems that doesn't work.