Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable browser authentication dialog in spring security

I am using spring security 4, for some reason after I finish authentication with my login page I get browser authentication dialog which force me to authenticate again.

this is my security configuration:

    http.antMatcher("/test")
            .httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/index.html", "/login.html", "/", "/scripts/**",
                    "/bower_components/**", "/styles/**", "/views/**",
                    "/login", "/api/user/*").permitAll().anyRequest()
            .authenticated().and().logout().logoutUrl("/api/logout").and()
            .csrf().csrfTokenRepository(csrfTokenRepository()).and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
like image 1000
user2095956 Avatar asked Jul 15 '15 07:07

user2095956


2 Answers

The authentication popup is caused by the response header WWW-Authenticate: Basic, which is set by BasicAuthenticationEntryPoint.

Use a custom AuthenticationEntryPoint that doesn't set WWW-Authenticate: Basic:

public class NoPopupBasicAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
    }

}

Add the custom authentication entry point to the security configuration (order is important):

http
    .httpBasic()
    .authenticationEntryPoint(new NoPopupBasicAuthenticationEntryPoint())
like image 194
Markus Pscheidt Avatar answered Sep 22 '22 05:09

Markus Pscheidt


In WebFlux it's not enough to just disable httpBasic. There is an ExceptionTranslationWebFilter that uses HttpBasicServerAuthenticationEntryPoint by default, causing such behavior. To disable it you should install another ServerAuthenticationEntryPoint, for example:

http.exceptionHandling()
    .authenticationEntryPoint(HttpStatusServerEntryPoint(HttpStatus.FORBIDDEN))
    .
like image 39
Vlad Kudoyar Avatar answered Sep 19 '22 05:09

Vlad Kudoyar