I have trouble controlling the HTTP response code of my Spring Boot Rest server. The controller advice change the header www-authenticate but I keep getting 404 not found (tested using Postman).
Here is my code (made to generate 401 all the time).
The Configuration class:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
public SecurityConfig()
{
super(false);
}
@Override
protected AuthenticationManager authenticationManager() throws Exception
{
return new ProviderManager(Arrays.asList((AuthenticationProvider) new CustomAuthenticationProvider()));
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint());
http.httpBasic()
.and().authorizeRequests().anyRequest().hasAuthority("USER")
.and().csrf().disable();
}
}
The controller advice to indicate what to do in case of exception (actually trigger according to my console and the WWW-Authenticate value):
@ControllerAdvice
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint
{
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException
{
System.out.println("there");
response.setHeader("WWW-Authenticate", "Unauthorized test");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}
}
The authentication provider that will always throw a BadCredentialsException.
public class CustomAuthenticationProvider implements AuthenticationProvider
{
public CustomAuthenticationProvider()
{
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
throw new BadCredentialsException("Bad credentials exception");
}
@Override
public boolean supports(Class<?> pClass)
{
return (pClass == UsernamePasswordAuthenticationToken.class);
}
}
Any idea what I did wrong?
Stacktrace:
..57,398 [DEBUG](o.s.web.servlet.DispatcherServlet) - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/error]
..57,399 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1d9af731] in DispatcherServlet with name 'dispatcherServlet'
..57,401 [TRACE](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - No handler mapping found for [/error]
..57,401 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping@445058e8] in DispatcherServlet with name 'dispatcherServlet'
..57,404 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@5423a17] in DispatcherServlet with name 'dispatcherServlet'
..57,405 [DEBUG](o.s.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) - Looking up handler method for path /error
..57,412 [DEBUG](o.s.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) - Did not find handler method for [/error]
..57,412 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WelcomePageHandlerMapping@347b370c] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@356fa0d1] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [TRACE](o.s.web.servlet.handler.BeanNameUrlHandlerMapping) - No handler mapping found for [/error]
..57,413 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@31533eb1] in DispatcherServlet with name 'dispatcherServlet'
..57,413 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - Matching patterns for request [/error] are [/**]
..57,414 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - URI Template variables for request [/error] are {}
..57,416 [DEBUG](o.s.web.servlet.handler.SimpleUrlHandlerMapping) - Mapping [/error] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@3c6fb501]]] and 1 interceptor
..57,418 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@7ecb9e17]
..57,418 [TRACE](o.s.web.servlet.DispatcherServlet) - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@4dac40b]
..57,427 [TRACE](o.s.web.servlet.resource.ResourceHttpRequestHandler) - Applying "invalid path" checks to path: error
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Resolving resource for request path "error"
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: ServletContext resource [/]
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: ServletContext resource [/]
..57,429 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [META-INF/resources/]
..57,430 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [META-INF/resources/]
..57,430 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [resources/]
..57,431 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [resources/]
..57,431 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [static/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [static/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - Checking location: class path resource [public/]
..57,432 [TRACE](o.s.web.servlet.resource.PathResourceResolver) - No match for location: class path resource [public/]
..57,432 [TRACE](o.s.web.servlet.resource.ResourceHttpRequestHandler) - No matching resource found - returning 404
..57,432 [DEBUG](o.s.web.servlet.DispatcherServlet) - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
..57,432 [TRACE](o.s.web.servlet.DispatcherServlet) - Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest@268be1ed
..57,432 [DEBUG](o.s.web.servlet.DispatcherServlet) - Successfully completed request
..57,433 [TRACE](o.s.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext) - Publishing event in org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@40ef3420: ServletRequestHandledEvent: url=[/error]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[37ms]; status=[OK]
..57,433 [DEBUG](o.s.beans.factory.support.DefaultListableBeanFactory) - Returning cached instance of singleton bean 'delegatingApplicationListener'