带有spring security 6的spring Boot 3中的h2数据库问题[重复]

2j4z5cfb  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(70)

此问题在此处已有答案

Spring + H2DB-Web-Console: "This method cannot decide whether these patterns are Spring MVC patterns or not."(3个答案)
2天前关闭。
我收到错误:这是因为在servlet上下文中有多个可Map的servlet:{org.h2.server.web.JakartaWebServlet=[/h2-console/*]
尝试了所有的方法,它不工作,目前有这样的

@Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers(new AntPathRequestMatcher("/h2-console/**"));
    }

    @Bean
    @DependsOn("webSecurityCustomizer")
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .authorizeHttpRequests((requests) -> requests
                        .requestMatchers("/", "/login").permitAll()
                        .requestMatchers("/students", "/students/**").hasRole("ADMIN")
                        .anyRequest().authenticated()
                )
                .formLogin()
                .failureUrl("/login?error=BadCredentials")
                .defaultSuccessUrl("/students", true)
                .and()
                .logout()
                .logoutUrl("/logout")
                .clearAuthentication(true)
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID")
                .logoutSuccessUrl("/")
                .and()
                .authenticationProvider(authenticationProvider());

        return http.build();
    }

字符串
我可以让它工作的唯一方法,如果我简单地把:http.authorizeHttpRequests().anyRequest().permitAll();

6bc51xsx

6bc51xsx1#

看起来你试图使用@DependsOn(“webSecurityCustomizer”)来确保webSecurityCustomizer bean在SecurityFilterChain之前初始化。ignoring()方法在WebSecurityCustomizer的上下文中不能直接在Web上使用,所以你无法让它运行。
相反,您可以在安全过滤器链中简单地定义.ignoring()上下文,我们在HttpSecurity示例上使用它,它看起来像这样。

public SecurityFilterChain securityFilterChain(HttpSecurity http) throws 
    Exception {
    http.csrf().disable().requestMatchers(new AntPathRequestMatcher("/h2-console/**")).ignoring()
   
    // rest of the logic
}

字符串

相关问题