首页 > 解决方案 > 为什么添加弹簧安全后无法访问主页

问题描述

我正在向网站添加 Spring Security 我的目标是向所有用户显示所有图像、home.html 和 index.html(无需登录)。

我已允许对图像、主页和索引页面的所有请求,但匿名用户仍然无法在不登录的情况下进入主页

我的安全配置 Java

protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/admin").hasRole("ADMIN")
            .antMatchers("/", "home", "index").permitAll()
            .antMatchers("/styles.css", "/css/**", "/js/**", "/fonts/**", "/images/**").permitAll()
            .anyRequest().hasRole("USER")
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?login_error=1")
            .permitAll()
            .and()
            .logout()
            .logoutSuccessUrl("/")
            .permitAll();
}

我的 index.html 页面

<!doctype html>
<html lang="en"       
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

    <body>

                    <a href="login" class="btn btn-outline-danger btn-lg">login page </a>
                    <a href="home" class="btn btn-outline-danger btn-lg">home page</a>


    </body>
</html>

我的形象

标签: javahtmlspring-bootspring-security

解决方案


您是否尝试过这样的重构: .antMatchers("/", "home", "index").permitAll()-> .antMatchers("/", "/home", "/index").permitAll()


推荐阅读