【问题标题】:Attempted to call method expression(java.lang.String) on null context object试图在空上下文对象上调用方法表达式(java.lang.String)
【发布时间】:2016-09-21 10:25:16
【问题描述】:

我在我的 spring 应用程序中使用安全性。

<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

我该如何解决?

【问题讨论】:

  • 你的依赖列表中有 Spring Security 的 thymeleaf 扩展吗?

标签: spring-security thymeleaf


【解决方案1】:

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

${#authorization?.expression('isAuthenticated()')}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 2017-03-02
    • 1970-01-01
    • 2023-01-16
    • 2015-11-13
    • 1970-01-01
    • 2018-08-25
    • 2017-09-01
    相关资源
    最近更新 更多