简体   繁体   English

如果未登录,则在Spring Boot / Thymeleaf应用程序中隐藏按钮

[英]Hide a button in Spring Boot/Thymeleaf application if not logged in

I'm trying to hide a button in the header of my Spring Boot application in the following way with my markup: 我试图用我的标记通过以下方式在Spring Boot应用程序的标题中隐藏一个按钮:

<!-- Is not logged in, so don't show "Log In" -->
<li sec:authorize="!isAuthenticated()">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</li>\

Is this not correct? 这不正确吗? I'm using the Thymeleaf templating engine. 我正在使用Thymeleaf模板引擎。

Add Spring Security Dialect in spring boot app for sec attribute to work, 在spring boot应用程序中添加Spring Security Dialect,以使sec属性起作用,

@Configuration
public class ThymeleafConfig {

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

If you have a Spring Security Dialect, then you can try, 如果您有Spring Security方言,则可以尝试,

<!-- Show login link only for anonymous users -->
<div sec:authorize="isAnonymous()">
    <a href="/login" th:href="@{/login}" class="btn-login">Log In</a>
</div>

You can use simple if's: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#simple-conditionals-if-and-unless 您可以使用简单的if: http//www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#simple-conditionals-if-and-unless

But you have to put the objects, you use in the if's, before into the (Spring) Model 但是您必须将在if中使用的对象放到(Spring)模型中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM