评估SpringEL表达式的异常:“#authorization.expression('isAuthenticated()')”

时间:2016-05-21 11:59:46

标签: spring spring-security

我在thymleaf有一个菜单,里面有一个登录 - 注销按钮。 这是此菜单的完整页面:

<!DOCTYPE html>
<html>
  <head>

  </head>
  <body>
    <div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">RentIt Company</a>
        </div>
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav navbar-right">
            <li th:if="${#authorization.expression('!isAuthenticated()')}">
              <a href="/signin" th:href="@{/signin}">Sign in</a>
            </li>
            <li th:if="${#authorization.expression('isAuthenticated()')}">
              <a href="/logout" th:href="@{/logout}">Logout</a>
            </li>
          </ul>
        </div>
      </div>
    </div>


  </body>
</html>

问题是,当我跑步时,它抱怨:

There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')"

似乎它无法识别isAuthenticated方法,所以我该如何解决?

2 个答案:

答案 0 :(得分:1)

您必须在项目中添加依赖项,请参阅build.gradle中的代码段(我使用gradle,如果不同则适应您的构建系统):​​

dependencies {
    // your dependencies ...
    compile "org.thymeleaf:thymeleaf-spring4:3.0.1.RELEASE"
    compile "org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.1.RELEASE"
}

还需要在配置中为Thymeleaf添加Spring Security扩展,如下所示:

@ComponentScan(basePackageClasses = HomeController.class)
public class ServletContextConfig extends WebMvcConfigurerAdapter {
    @Bean
    public TemplateEngine templateEngine(ApplicationContext applicationContext) {
        final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setEnableSpringELCompiler(true);
        templateEngine.addDialect(new SpringSecurityDialect(););
        templateEngine.setTemplateResolver(defaultTemplateResolver(applicationContext));
        return templateEngine;
    }
    // other @Bean configurations ...
}

答案 1 :(得分:0)

更改org.thymeleaf.extras的版本如下

<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-springsecurity4</artifactId>
  <version>3.0.1.RELEASE</version>

它将解决您的问题