Thymeleaf with Spring Security - 如何检查用户是否登录? [英] Thymeleaf with Spring Security - how to check if user is logged in or not?

查看:88
本文介绍了Thymeleaf with Spring Security - 如何检查用户是否登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Thymeleaf 和 Spring Security 的 Spring Boot.我有一个带有登录链接的简单视图.当用户登录时,我想将登录链接更改为注销链接.

我试过了:

<a th:href="@{/logout}">注销</a>

<div sec:authorize="#{isAnonymous()}"><a th:href="@{/login}">登录</a>

但它不起作用 - 它显示两个链接.

解决了.我必须注册 Thymeleaf 方言.为了做到这一点,我创建了一个新的配置类,它创建了 SpringSecurityDialect bean:

@Configuration公共类 ThymeleafConfig {@豆角,扁豆公共 SpringSecurityDialect springSecurityDialect(){返回新的 SpringSecurityDialect();}}

解决方案

根据 thymeleaf docs 没有需要拼写表达.这不是 th: 属性.

所以你可以试试:

<div sec:authorize="isAnonymous()">

I'm using Spring Boot with Thymeleaf and Spring Security. I've got a simple view with a login link. When the user logs in, I'd like to change login link to logout link.

I tried:

<div sec:authorize="#{isAuthenticated()}">
  <a th:href="@{/logout}">Log out</a>
</div>
<div sec:authorize="#{isAnonymous()}">
  <a th:href="@{/login}">Log in</a>
</div>

but it's not working - it displays both links.

EDIT: I solved it. I had to register Thymeleaf dialect. In order to do this, I created a new config class, that creates SpringSecurityDialect bean:

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringSecurityDialect springSecurityDialect(){
        return new SpringSecurityDialect();
    }
}

解决方案

According to thymeleaf docs no spel expression is required. This is not a th: attribute.

So you may try :

<div sec:authorize="isAuthenticated()">

<div sec:authorize="isAnonymous()">

这篇关于Thymeleaf with Spring Security - 如何检查用户是否登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆