Idea: I want to protect every site of a Spring web MVC with a HTTP basic authentication, but want to redirect via error-page in case of a 40* or 500 server error to /welcome (Of course the user has to be authenticated or he will see the basic authentication dialog).
Problem: Every time I try to access the site, the basic authentication dialog pops up as expected. But when I cancel the dialog aka press cancel I land on the protected welcome page [and see all important/secured information] - no basic authentication dialog!
Sample controller:
@Controller
public class WelcomeController
{
// Welcome is a protected site!
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public ModelAndView welcome()
{
return new ModelAndView("welcome");
}
}
Security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity security) throws Exception
{
// Set the security settings
security.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf();
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
... Snipped ...
<servlet-mapping>
<servlet-name>testapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>400</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/welcome</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/welcome</location>
</error-page>
</web-app>