有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在添加spring安全性后无法访问主页

我正在为网站添加spring安全性 我的目标是显示所有图像,回家。html和索引。向所有用户发送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();
}

我的索引。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>

my Image


共 (1) 个答案

  1. # 1 楼答案

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