尝试在空上下文对象上调用方法表达式(java.lang.String)

Jef*_*eff 5 spring-security thymeleaf

我在我的春季应用程序中使用安全性.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的登录控制器:

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

然后我做了菜单,在那里我试着制作退出按钮:

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

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

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

我该如何解决?

小智 9

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

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