java - allowedAll requestMatchers 不适用于 "/"模式

标签 java spring spring-boot

我正在制作网站,我必须使用SecurityFilterChain。 顺便说一句,起始页 - localhost:8080/始终是安全的。 当我输入 localhost:8080/时,浏览器总是要求我登录。 如何调整“/”模式上的permitAll?

public class SecurityConfig {

    @Bean
    public SecurityFilterChain web(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(auth -> {
                    auth.requestMatchers("/", "/hello").permitAll();
                    auth.anyRequest().authenticated();
                })
                .formLogin(Customizer.withDefaults())
                .build();
    }
}

当我输入 localhost:8080/时,我希望看到该页面,而不是登录页面。

最佳答案

我找到了答案。 没有用“/”映射的分页。 当我映射“/”页面后,它就工作了。 我认为它应该显示白页,但似乎如果起始页是白页,那么即使有permitAll允许,它也会打印登录页。

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("/hello2")
    public String hello2(){
        return "hello2";
    }
//
//    @GetMapping("/")
//    public String start(){
//        return "startpage";
//    }
}

关于java - allowedAll requestMatchers 不适用于 "/"模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75020008/

相关文章:

spring - 使用 Spring 数据和 QueryDSL 在 MongoDb 查询上进行投影

java - 为什么java字符串方法将null作为字符串返回?

Spring Boot 1.4.3 嵌入式Tomcat NoSuchMethodError StringManager.getManager

java - org.springframework.batch.item.ItemStreamException : Failed to initialize the reader on second api call

java - JAVA中的TEA加密

java - JNI 检测到应用程序错误 : the return type of CallObjectMethodV does not match void android. media.AudioTrack.play()

java - 滚动时保持 ImageView 的最终 View

java - Spring 应用程序似乎没有持久化数据

java - 将带有嵌套字符的字符串转换为java中特定的json格式

java - Spring : Initialization of properties before any bean creation