首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sec:authorize="isAuthenticated()“和sec:authorize="isAnonymous()”在错误页面上不起作用

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

Stack Overflow用户
提问于 2018-03-02 04:27:16
回答 1查看 1.6K关注 0票数 0

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

现在我坚持使用sec:authorize="isAuthenticated()“和sec:authorize="isAnonymous()”为错误页面返回false的问题。因此,这两个部分的内容都会被隐藏。

我的项目依赖关系:

代码语言:javascript
复制
<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>

我的html页面:

代码语言:javascript
复制
<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这样的错误页面,两个div都隐藏了。除了让它开始正常工作之外,还需要做什么改变?

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 00:22:38

在我的示例中,我将以下内容添加到application.properties

代码语言:javascript
复制
security.filter-dispatcher-types=ASYNC, FORWARD, INCLUDE, REQUEST, ERROR

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49058166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档