无法从 Spring Security 6.2.1 禁用默认登录表单

问题描述 投票:0回答:1

我使用 Spring Security 6.2.1 和 Spring Boot 3.2 来处理我的 Web 应用程序的登录。

不幸的是,我无法禁用 Spring Security 的默认登录表单。 我想使用自己的自定义登录表单,但我总是重定向到 spring 默认表单。

这是我的过滤器:

@Bean

public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    try {
        http
        // ...
       .httpBasic(httpBasic -> httpBasic.disable())
       .authorizeHttpRequests((authorizeExchange) -> authorizeExchange
             .requestMatchers("/mylogin").permitAll()               
             .anyRequest().permitAll())
       .csrf((csrf) -> csrf.disable())
       .formLogin(form -> form.loginPage("/login").permitAll());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    return http.build();

}

我输入 http://127.0.0.1:8080/mylogin 我希望被重定向到我的自定义页面而不是默认页面!!!

我按照此页面使用我的自定义登录页面,但我总是看到默认的登录页面。 https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/form.html

这是我的日志 2024-01-03 11:30:35.602 |调试|并行-1 | WebSessionServerRequestCache:83 | |请求已添加到 WebSession:'/mylogin' 2024-01-03 11:30:35.603 |调试|并行-1 |默认服务器重定向策略:54 | |重定向到“/登录” 2024-01-03 11:30:35.638 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 |或者服务器WebExchangeMatcher:57 | |尝试使用 PathMatcherServerWebExchangeMatcher{pattern='/login', method=POST} 进行匹配 2024-01-03 11:30:35.639 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 | PathPatternParserServerWebExchangeMatcher:82 | |请求“GET /login”与“POST /login”不匹配 2024-01-03 11:30:35.639 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 |或服务器WebExchangeMatcher:62 | |未找到匹配项 2024-01-03 11:30:35.639 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 |或者服务器WebExchangeMatcher:57 | |尝试使用 PathMatcherServerWebExchangeMatcher{pattern='/login', method=GET} 进行匹配 2024-01-03 11:30:35.640 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 | PathPatternParserServerWebExchangeMatcher:100 | |检查请求的匹配:'/login';反对“/登录” 2024-01-03 11:30:35.640 |调试| http-nio-8080-exec-4 | http-nio-8080-exec-4 |或服务器WebExchangeMatcher:62 | |匹配

有什么想法吗? 我已经筋疲力尽了,因为我已经测试了大概 20 段代码!!!!!!!!!!

提前致谢

spring authentication security default
1个回答
0
投票

经过大量调试和调查后,我发现了我的问题。 问题来自于我的 pom.xml 中的这种依赖关系。 事实上,我有一个 spring 项目来充当 Web 服务器和 API 网关。 但他们并不一起遵守。 Spring Cloud Starter Gateway 的 Webflux 和我的 Spring Starter Web 之间存在某种冲突。 我删除了它,现在我测试的代码片段也可以工作。 我创建了另一个仅充当 API 网关的项目

<dependency>
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter-gateway</artifactId> 
   <version>4.1.1</version>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.