Basic Authentication in Mule 4

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

No alt text provided for this image

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

?

No alt text provided for this image

Step 4: Add Basic Security filter component and specify reals as “mule” in the flow after HTTP Listener.

No alt text provided for this image

Step 5: Add Authorization filter component with Required Authorities as ROLE_ADMIN and a logger in the last of the flow.

No alt text provided for this image

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.

No alt text provided for this image

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.

回复

要查看或添加评论,请登录

Silverline in India的更多文章

社区洞察

其他会员也浏览了