Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to call method expression(java.lang.String) on null context object

I am using security in my spring app.

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

and here is my controller for login:

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

then I have made menu and there I tried to make logout button:

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

I can login correctly, but when it loads the menu it complains with:

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

How can I fix it?

like image 862
Jeff Avatar asked May 24 '16 09:05

Jeff


1 Answers

Try by adding a question mark at the end of authorization object to check if it is null before using it.

${#authorization?.expression('isAuthenticated()')}
like image 59
Ezd Avatar answered Oct 16 '22 20:10

Ezd