尝试在空上下文对象

时间:2016-05-24 09:34:01

标签: spring-security thymeleaf

我在春季应用中使用安全性。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

这是我的登录控制器:

@Configuration
static class ClientWebConfig extends WebMvcConfigurerAdapter {
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("fragments/login");
    }
}

然后我做了菜单,在那里我尝试了注销按钮:

<li th:if="${#authorization.expression('isAuthenticated()')}">
    <a href="/logout" th:href="@{/logout}">Logout</a>
</li>

我可以正确登录,但是当它加载菜单时会抱怨:

org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 15): Method call: Attempted to call method expression(java.lang.String) on null context object

我该如何解决?

1 个答案:

答案 0 :(得分:7)

尝试在授权对象的末尾添加一个问号,以便在使用它之前检查它是否为空。

$ {#授权?.expression( 'isAuthenticated()')}

相关问题