Results 1 to 7 of 7

Thread: Return 403 instead of 401 from Basic Auth

  1. #1

    Default Return 403 instead of 401 from Basic Auth

    I am using a custom AuthenticationProvider within a BasicProcessingFilter to implement basic HTTP auth.

    However, in case of failure I would like it to return a 403 (forbidden) instead of a 401 (i.e. I don't want the browser dialog to pop up).

    Where can I configure the HTTP error code to be returned?

    Code:
    <bean id="vasmAuthenticationProvider" class="com.rp.security.VasmAuthenticationProvider">
    		<security:custom-authentication-provider />
    	</bean>
    
    	<bean id="basicProcessingFilter" class="org.springframework.security.ui.basicauth.BasicProcessingFilter">
    		<property name="authenticationManager">
    			<ref bean="_authenticationManager" />
    		</property>
    		<property name="authenticationEntryPoint">
    			<ref bean="authenticationEntryPoint" />
    		</property>
    		<security:custom-filter position="PRE_AUTH_FILTER" />
    	</bean>
    	<bean id="authenticationEntryPoint" class="org.springframework.security.ui.basicauth.BasicProcessingFilterEntryPoint">
    		<property name="realmName" value="RP" />
    	</bean>
    
    	<!-- The authentication Manager that forwards the handling to the provider manager -->
    	<bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager">
    		<property name="providers">
    			<list>
    				<ref bean="vasmAuthenticationProvider" />
    			</list>
    		</property>
    	</bean>

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You will have to create your own (or extend) BasicAuthenticationEntryPoint. That is sending the 401 to the client.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    I presume you mean BasicProcessingFilterEntryPoint?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I was checking the spring-security 3.0 sources, it got renamed in that release . But you are correct.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    OK, so I presume I would have to override the commence() method.

    I did this:
    Code:
    public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
    			throws IOException, ServletException {
    		super.commence(request, response, authException);
    		HttpServletResponse h = (HttpServletResponse) response;
    		h.setStatus(403);
    }
    But when I call it via CURL with a wrong user/password it still shows 401:

    Code:
    <body><h2>HTTP ERROR 401</h2>
    Any suggestions?

    P.S. My new entry point is getting called, I checked that.

  6. #6

    Default

    if I try sendError(403) I get an IllegalStateException since response has already been committed....

  7. #7

    Default

    Got it, should not have called super. Looked at the original source code and replaced my commence() with:

    Code:
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, authException.getMessage());
    Works like a charm, thanks for pointing me to the right direction.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •