【问题标题】:Spring Boot Exception evaluating SpringEL expression评估 SpringEL 表达式的 Spring Boot 异常
【发布时间】:2019-05-24 16:09:15
【问题描述】:

错误 4904 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine
: [THYMELEAF][http-nio-8080-exec-1] 异常处理模板 “index”:模板解析时出错(模板:“class 路径资源 [templates/index.html]")

org.thymeleaf.exceptions.TemplateInputException:发生错误 在模板解析期间(模板:“类路径资源 [模板/index.html]") 在 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE] 在 org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 在 org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE] 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) [spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]


原因:org.attoparser.ParseException:异常评估 SpringEL 表达式:“#authorization.expression('isAuthenticated()') 和 #authorization.expression('hasAuthority(''USER'')')" (模板: “片段/导航栏” - 第 8 行,第 15 列)


Pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

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

当我尝试检查权限时,问题来自这里

<html lang="en" xmlns="w3.org/1999/xhtml" xmlns:th="thymeleaf.org"> 
<th:block th:fragment="navbar">
    <th:block if:sec:authorize="isAnonymous()">
        <th:block th:replace="fragments/navbar-guest"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''USER'')')}">
        <th:block th:replace="fragments/navbar-user"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''ADMIN'')')}">
        <th:block th:replace="fragments/navbar-admin"></th:block>
    </th:block>
    <th:block th:if="${#authorization.expression('isAuthenticated()') and #authorization.expression('hasAuthority(''MODERATOR'')')}">
        <th:block th:replace="fragments/navbar-user"></th:block>
    </th:block>
</th:block>

【问题讨论】:

  • 可以添加一个sn-p的索引模板吗?
  • w3.org/1999/xhtml" xmlns:th="thymeleaf.org">
  • 你能把它贴在帖子里吗?并且请在发布前尝试格式化您的文本

标签: java spring spring-boot thymeleaf


【解决方案1】:

我添加了拦截器,每个请求都会发送角色

modelAndView.addObject("authorization", SecurityContextHolder.getContext().getAuthentication().getAuthorities().stream().findFirst().get().toString());

在 HTML 中,我检查了 <th:block th:if="${authorization=='USER'}"> <th:block th:replace="fragments/navbar-user"></th:block> </th:block>

现在一切正常!

【讨论】:

    【解决方案2】:

    如果您通过如下模型的授权-

    在控制器上设置参数

    request.setAttribute("authorization", authorizationObject);
    

    在前端使用相同的,如下所示

    <th:block th:if="${authorization.isAuthenticated() && authorization.hasAuthority('USER')}">
            <th:block th:replace="fragments/navbar-user"></th:block>
    </th:block>
    

    希望对你有帮助,谢谢

    【讨论】:

    • 我如何获得授权对象
    • 它帮助我添加了 Object 的拦截器
    【解决方案3】:

    确保您在 pom.xml 中 thymeleaf-extras-springsecurity4

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

    xmlns:sec 在 html 模板中 在 thymeleaf 页面中。

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      相关资源
      最近更新 更多