sec:authorize= "isAuthenticated()" 和 sec:authorize= "isAnonymous()" 在错误页面上不起作用



我正在用Thymeleaf + spring-boot创建小项目。

现在我遇到了 sec:authorize="isAuthenticated((" 和 sec:authorize="isAnonymous((" 为错误页面返回 false 的问题。因此,这两个部分的内容都是隐藏的。

我的项目依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.1</version>
    </dependency>
</dependencies>

我的网页:

<div sec:authorize="isAuthenticated()">
                <span class="navbar-text text-success"> <i class="fas fa-user fa-lg"></i>
                    <span sec:authentication="name"></span> </span>
                <a class="header-btn"  th:href="@{/logout}">
                    <i class="fas fa-sign-out-alt fa-lg"></i> Sign Out
                </a>
            </div>
            <div sec:authorize="isAnonymous()">
                <span class="navbar-text text-success"><i class="fas fa-user-secret fa-lg"></i> Anonymous</span>
                <a class="header-btn" th:href="@{/login}">
                    <i class="fas fa-sign-in-alt fa-lg"></i> Sign In
                </a>
            </div>

在我的情况下,像 403 或 404 这样的错误页面都隐藏了。除了让它开始正常工作之外,还有什么需要改变的呢?

就我而言,我在application.properties中添加了以下内容:

security.filter-dispatcher-types=ASYNC, FORWARD, INCLUDE, REQUEST, ERROR

。并重新启动了应用程序。安全上下文现在在我的错误页面上可用。

最新更新