在 Spring 中禁用登录窗口(Http Basic)[重复]
Posted
技术标签:
【中文标题】在 Spring 中禁用登录窗口(Http Basic)[重复]【英文标题】:Disable Sign In Window in Spring (Http Basic) [duplicate] 【发布时间】:2021-12-05 11:08:04 【问题描述】:我正在创建一个带有登录网站的简单应用。 我使用了 HTTP 基本类型的授权,但问题是我不知道如何禁用每次传递错误凭据或在身份验证之前编写安全端点站点时都会显示的弹出窗口。
前端是用纯 JS 编写的,在没有任何模板引擎的情况下启动。只是静态目录中的 js + html 文件。
身份验证页面使用 Fetch Api 发送带有凭据的标头
有人知道如何禁用这个窗口,如下图:
这是我的安全配置类:
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter
@Resource
private UserDetailsService userDetailsService;
@Autowired
private CustomLogoutHandler logoutHandler;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
auth.authenticationProvider(authProvider());
@Override
protected void configure(HttpSecurity http) throws Exception
http.csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.POST, "/demo/users/save").permitAll()
.antMatchers(HttpMethod.POST, "/demo/users/**").permitAll()
.antMatchers(HttpMethod.POST, "/users/*/save").permitAll()
.antMatchers(HttpMethod.DELETE, "/users/**").permitAll()
.antMatchers(HttpMethod.POST, "/users/*/verify").permitAll()
.antMatchers(HttpMethod.GET,"/users/**").permitAll()
.antMatchers(HttpMethod.PUT,"/users/**").permitAll()
.antMatchers("/css/**", "/js/**", "/img/**").permitAll()
.antMatchers("/signup-page.html").permitAll()
.antMatchers("/landing-page.html").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.disable()
.logout()
.logoutUrl("/logout")
.addLogoutHandler(logoutHandler)
.logoutSuccessUrl("/landing-page.html")
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))
.permitAll()
.and()
.httpBasic();
@Bean
public DaoAuthenticationProvider authProvider()
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
@Bean
public PasswordEncoder passwordEncoder()
return new BCryptPasswordEncoder();
【问题讨论】:
另见***.com/questions/34296208/… 【参考方案1】:我之前找不到答案,我找到的每个响应都包含“禁用 httpBasic”,这不是一个令人满意的解决方案。 这是一个主题: Spring Boot security shows Http-Basic-Auth popup after failed login
这些行解决了我的问题:
httpBasic()
.authenticationEntryPoint(new AuthenticationEntryPoint() //<< implementing this interface
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException
//>>> response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\""); <<< (((REMOVED)))
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
);
【讨论】:
以上是关于在 Spring 中禁用登录窗口(Http Basic)[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring 服务器配置中禁用“需要身份验证的弹出窗口”?