首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Webflux禁用登录

Spring Webflux禁用登录
EN

Stack Overflow用户
提问于 2020-08-27 18:58:04
回答 1查看 520关注 0票数 3

让我简要地描述一下我现在面临的问题。

我已经为webflux应用程序配置了spring安全,当我尝试访问不需要身份验证的路由时,会收到登录表单提示。路由是/swagger-ui/,它应该在没有任何登录表单或其他东西的情况下打开。

下面是我在SecurityWebFilterChain中拥有的代码

代码语言:javascript
复制
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    //@formatter:off
    return http
            .formLogin().disable()
            .httpBasic().disable()
            .authenticationManager(authenticationManager)
            .securityContextRepository(securityContextRepository)
            .authorizeExchange()
            .pathMatchers(HttpMethod.OPTIONS).permitAll()
            .pathMatchers("/v2/api-docs", "/v3/api-docs", "/configuration/ui", "/swagger-resources",
                    "/configuration/security", "/swagger-ui/", "/swagge‌​r-ui",
                    "/webjars/**", "/swagger-resources/configuration/ui",
                    "/swagger-resources/configuration/security").permitAll()  // Allowed routes for swagger
            .pathMatchers("/api/auth", "/api/auth/**").permitAll() // Allowed routes for auth
            .and()
            .authorizeExchange()
            .anyExchange()
            .authenticated() // All other routes require authentication
            .and()
            .csrf().disable()
            .headers()
            .hsts()
            .includeSubdomains(true)
            .maxAge(Duration.ofSeconds(31536000))
            .and()
            .frameOptions().mode(XFrameOptionsServerHttpHeadersWriter.Mode.SAMEORIGIN)
            .and()
            .build();
    //@formatter:on
    }
}

如果任何人有任何建议,请让我知道,我将不胜感激。这是我在浏览器中得到的图片。

EN

回答 1

Stack Overflow用户

发布于 2020-12-10 07:41:39

这个问题也让我很恼火。问题是,通过将.httpBasic().disable()放在代码中,您希望spring跳过基本身份验证(浏览器窗口),但它没有。

相反,可以尝试为.httpBasic()提供一个ServerAuthenticationEntryPoint

最简单的是HttpStatusServerEntryPoint

例如,在您的代码中将更改为:

代码语言:javascript
复制
***
return http
            .formLogin().disable()
            .httpBasic().authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
            .authenticationManager(authenticationManager)
            ***

通过更改,您的服务器将返回一个401 UNAUTHORIZED HttpStatus,而不是浏览器窗口!干杯!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63614553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档