Java 在spring security中禁用浏览器身份验证对话框

Java 在spring security中禁用浏览器身份验证对话框,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我使用的是SpringSecurity4,由于某种原因,在我完成登录页面的身份验证后,我会看到浏览器身份验证对话框,这会迫使我再次进行身份验证 这是我的安全配置: http.antMatcher("/test") .httpBasic() .and() .authorizeRequests() .antMatchers("/index.html", "/login.html", "/", "/

我使用的是SpringSecurity4,由于某种原因,在我完成登录页面的身份验证后,我会看到浏览器身份验证对话框,这会迫使我再次进行身份验证

这是我的安全配置:

    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);
使用formLogin()而不是httpBasic()。重构您的配置:

http
   .antMatcher("/test")
   .authorizeRequests()
   .antMatchers("/index.html", "/login.html", "/", "/scripts/**",
       "/bower_components/**", "/styles/**", "/views/**",
       "/login", "/api/user/*").permitAll()
   .anyRequest().authenticated()
   .and().formLogin().loginPage("/your_login_page_here").permitAll()
   .and().logout().logoutUrl("/api/logout").and()
   .csrf().csrfTokenRepository(csrfTokenRepository()).and()
   .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);

如果/login.html是您的登录页面,您可能希望将其从permitAll()位置之一删除。

身份验证弹出窗口是由响应标题
WWW Authenticate:Basic
引起的,该标题由设置

使用自定义的
AuthenticationEntryPoint
,该入口点不设置
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());
    }

}
将自定义身份验证入口点添加到安全配置(顺序很重要):


在WebFlux中,仅仅禁用httpBasic是不够的。有一个ExceptionTranslationWebFilter,默认情况下使用HttpBasicServerAuthenticationEntryPoint,导致此类行为。要禁用它,您应该安装另一个ServerAuthenticationEntryPoint,例如:

http.exceptionHandling()
    .authenticationEntryPoint(HttpStatusServerEntryPoint(HttpStatus.FORBIDDEN))
    .

删除
httpBasic()
.Hi。这没有帮助,对话框仍然存在,然后您有其他触发基本或摘要身份验证的东西。还有什么可以触发它?在您的配置中,您只发布了一个片段。或者你的基础设施中的某些东西。。。有些东西发送了一个带有基本(或摘要)标题的401,否则你将无法得到弹出窗口。很好。这对我帮助很大。
http.exceptionHandling()
    .authenticationEntryPoint(HttpStatusServerEntryPoint(HttpStatus.FORBIDDEN))
    .