Reputation: 8321
I have following Mule flow :-
<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<mule-ss:security-manager>
<mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
</mule-ss:security-manager>
<spring:beans>
<ss:authentication-manager alias="authenticationManager">
<ss:authentication-provider>
<ss:user-service id="userService">
<ss:user name="username" password="password" authorities="ROLE_ADMIN" />
</ss:user-service>
</ss:authentication-provider>
</ss:authentication-manager>
</spring:beans>
<flow name="testFlow1" doc:name="testFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" doc:name="HTTP">
<mule-ss:http-security-filter realm="realm" />
</http:inbound-endpoint>
<jersey:resources doc:name="REST">
<component class="MyRest" />
</jersey:resources>
<logger message="Done !!!!!" level="INFO" doc:name="Logger"/>
<!-- http://username:password@localhost:8083/myrest or http://localhost:8083/myrest and in this case we need to provide username and password from browser-->
<catch-exception-strategy>
<choice >
<when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.authentication.BadCredentialsException']">
<logger level="INFO" message="Authentication failed exception: #[exception.getCauseException().getClass().getName()]"/>
<set-property propertyName="http.status" value="401"/>
<http:response-builder status="401" doc:name="Status code = 401" doc:description="Authentication failed"/>
</when>
<when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.access.AccessDeniedException']">
<logger level="INFO" message="Access denied exception: #[exception.getCauseException().getClass().getName()]"/>
<set-property propertyName="http.status" value="405"/>
<http:response-builder status="405" doc:name="Status code = 405" doc:description="Authorization exception - Access denied"/>
</when>
<otherwise>
<logger message="An exception has been caught: #[exception.getCauseException().getClass().getName()]" level="ERROR" doc:name="Exception Thrown"/>
<set-payload value="Error detected while processing" doc:name="Prepare response for client"/>
<set-property propertyName="http.status" value="500"/>
<http:response-builder status="500" doc:name="Status code = 500" doc:description="Exception - Sending a 500 Http Status code as Response"/>
</otherwise>
</choice>
</catch-exception-strategy>
</flow>
I have taken the following as reference :- https://github.com/daveEason/mule-example-spring-security/blob/master/src/main/app/mule-config.xml
Now the issue whenever I hit the url :- http://localhost:8083/myrest
... I get the following exception :-
ERROR 2014-10-20 23:06:52,223 [[test].connector.http.mule.default.receiver.03] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String (org.mule.api.security.UnauthorisedException)
org.mule.transport.http.filters.HttpBasicAuthenticationFilter:156 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/security/UnauthorisedException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.security.UnauthorisedException: Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
at org.mule.transport.http.filters.HttpBasicAuthenticationFilter.authenticateInbound(HttpBasicAuthenticationFilter.java:156)
at org.mule.security.AbstractEndpointSecurityFilter.authenticate(AbstractEndpointSecurityFilter.java:54)
at org.mule.security.AbstractAuthenticationFilter.doFilter(AbstractAuthenticationFilter.java:52)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
ERROR 2014-10-20 23:06:52,225 [[test].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: An exception has been caught: org.mule.api.security.UnauthorisedException
ERROR 2014-10-20 23:06:52,520 [[test].connector.http.mule.default.receiver.03] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String (org.mule.api.security.UnauthorisedException)
org.mule.transport.http.filters.HttpBasicAuthenticationFilter:156 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/security/UnauthorisedException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.security.UnauthorisedException: Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
at org.mule.transport.http.filters.HttpBasicAuthenticationFilter.authenticateInbound(HttpBasicAuthenticationFilter.java:156)
at org.mule.security.AbstractEndpointSecurityFilter.authenticate(AbstractEndpointSecurityFilter.java:54)
at org.mule.security.AbstractAuthenticationFilter.doFilter(AbstractAuthenticationFilter.java:52)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
ERROR 2014-10-20 23:06:52,522 [[test].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: An exception has been caught: org.mule.api.security.UnauthorisedException
I wonder why it's generating such exception ..
But if I remove the exception block from the code and hit http://localhost:8083/myrest
it works fine and ask the username and password as expected and generate expected result ..
But I wonder why it is generating exception when I am using exception block in the code .. Actually my intention of using exception block is to get and control different http status in different situation
Upvotes: 1
Views: 4059
Reputation: 33413
There are two things at play here:
Exceptions thrown by the Jersey resources
Such exceptions must be dealt with by one or several custom JAX-RS exception handlers, registered to jersey:resources
via one or several jersey:exception-mapper
elements.
Exceptions thrown by mule-ss:http-security-filter
These exceptions are thrown by Mule outside of the scope of JAX-RS, so they must be handled by a catch-exception-strategy
element, as you did in your question.
Having this combination of error handling should provide you with what you need.
Upvotes: 3
Reputation: 573
to retrieve the HTTP code you can use a filter and throw an exception in the flow. Here an example:
<message-filter throwOnUnaccepted="true">
<message-property-filter pattern="message.inboundProperties['http.status'] >= 400" />
</message-filter>
Upvotes: 1