评估 SpringEL 表达式的 Spring Boot 异常

     2023-02-19     34

关键词:

【中文标题】评估 SpringEL 表达式的 Spring Boot 异常【英文标题】:Spring Boot Exception evaluating SpringEL expression 【发布时间】: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"> 你能把它贴在帖子里吗?并且请在发布前尝试格式化您的文本 【参考方案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">

【讨论】:

spring使用spring表达式(springel)(代码片段)

  Spring还提供了更灵活的注入方式,那就是Spring表达式,实际上SpringEL远比以上注入方式强大,我们需要学习它。SpringEL拥有很多功能。  使用Bean的id来引用Bean。  •调用指定对象的方法和访问对象的属性。  •进... 查看详情

spring学习(十三)-----spring表达式语言(springel)

...讲述了SpringExpressionLanguage——即Spring3中功能丰富强大的表达式语言,简称SpEL。SpEL是类似于OGNL和JSFEL的表达式语言,能够在运行时构建复杂表达式,存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式,格式... 查看详情

spring04-springel&springjdbc数据访问

...运行时查询和操作对象图的强大的动态语言,语法类似于EL表达式,具有诸如显示方法和基本字符串模板函数等特性.1.准备工作项目:spring-el2.需要导入jar包spring-expression.jarmaven项目pom文件添加:1<dependency>2<groupId>org.spr 查看详情

深入浅出spring原理及实战「原理分析专题」从零开始教你springel表达式使用和功能分析讲解指南(上篇)(代码片段)

SpringEL表达式语言,这种语言jsp中学到的el,但是在整个spring之中其表达式语言要更加的复杂,而且支持度更加的广泛,最重要的是他可以进行方法的调用,对象的实例化,集合操作等等,但是唯一的难点就是:代码太复杂了,表达式太复杂... 查看详情

使用springel表达式进行方法调用

原文链接:http://www.yiidian.com/spring/spring-el-method.htmlSpEL允许开发者用El运行方法函数,并且允许将方法返回值注入到属性中。一、编写Bean类TestMethod类packagecom.yiidian;/****@authorhttp://www.yiidian.com**/publicclassTestMethod{publicDoub 查看详情

springel表达式隔离不同环境的rocketmq(代码片段)

...资料。这里就不在赘述了。今天主要是讲如何使用SpringEL表达式来隔离不同环境的RocketMQ的。因为在非生产环境我们为了优化资源的效果,只部署了一套Ra 查看详情

springel表达式错误记录

原因暂时未知。。。。 查看详情

如何评估作为列值的表达式?

】如何评估作为列值的表达式?【英文标题】:Howtoevaluateexpressionsthatarethecolumnvalues?【发布时间】:2018-04-2410:16:19【问题描述】:我有一个包含数百万行的大数据框,如下所示:ABCEqn1234A+B3289B*C56122A+B*C如何评估Eqn列中的表达式?... 查看详情

如何在 Spring Security hasRole() 中评估变量表达式

】如何在SpringSecurityhasRole()中评估变量表达式【英文标题】:howtoevaluatevariableexpressioninspringsecurityhasRole()【发布时间】:2017-10-0714:00:58【问题描述】:在thyemeleaf的春季安全方面寻求帮助。我想在springsecurityhasRole()函数中将特定角... 查看详情

3.数据校验和springel

1.数据验证数据验证不应该被限定在web层去处理,他应该在任何需要做数据验证的地方做验证;基于以上考虑,Spring设计了一个既方便又可以在所有层使用的Validator接口Spring提供了Validator接口来进行对对象的验证,该接口实现2个... 查看详情

c中的内部短路评估

...不会评估。但是如果我有像if((a&amp;&amp;b)OPc)这样的表达式(其中OP是任意逻辑运算符),如果a=false会评估b吗?谢谢。【问题 查看详情

Spring Security - Thymeleaf - 我可以在 sec:authorize 标签中评估 SPEL 表达式吗?

】SpringSecurity-Thymeleaf-我可以在sec:authorize标签中评估SPEL表达式吗?【英文标题】:SpringSecurity-Thymeleaf-CanIevaluateSPELExpressionsinsidesec:authorizetags?【发布时间】:2016-03-1811:25:16【问题描述】:我正在编写一个小的SpringMVC-以Thymeleaf作为... 查看详情

springel表达式隔离不同环境的rocketmq(代码片段)

...资料。这里就不在赘述了。今天主要是讲如何使用SpringEL表达式来隔离不同环境的RocketMQ的。因为在非生产环境我们为了优化资源的效果,只部署了一套RabbitMQ环境。但是非生产环境有多套环境:dev(开发环境)、test(测试环... 查看详情

在 Python 中动态评估简单的布尔逻辑

...1-01-2821:11:57【问题描述】:我有一些动态生成的布尔逻辑表达式,例如:(A或B)和(C或D)A或(A和B)一个空-计算结果为真占位符被替换为布尔值。我应该,将此信息转换为Python表达式,例如Trueor(TrueorFalse)和eval吗?创建一个... 查看详情

pseq可以用seq来定义吗?(代码片段)

...则允许函数应用程序首先计算参数,因为这样做不会改变表达式的语义。因此,优化编译器可以首先开始评估b,因为它知道它最终将需要它。 查看详情

从 Pandas 中的公式动态评估表达式

】从Pandas中的公式动态评估表达式【英文标题】:DynamicallyevaluateanexpressionfromaformulainPandas【发布时间】:2019-05-1517:45:36【问题描述】:我想使用pd.eval对一个或多个数据帧列执行算术运算。具体来说,我想移植以下评估公式的代... 查看详情

Spring boot 无法评估表达式方法抛出 'org.hibernate.LazyInitializationException' 异常。使用 getter,ManyToMany 关系

】Springboot无法评估表达式方法抛出\\\'org.hibernate.LazyInitializationException\\\'异常。使用getter,ManyToMany关系【英文标题】:SpringbootUnabletoevaluatetheexpressionMethodthrew\'org.hibernate.LazyInitializationException\'exception.usinggetter,M 查看详情

评估条款开头的表达式(代码片段)

有没有办法在findall/3条款中评估这个总和?findall((A+C,[M,H|_]),(b_to_b(H,M,C),+member(M,[H|T])),R).在这里我得到像(1+3,List)这样的值,我正在寻找一些捷径,以便我得到价值4而不是(1+3)我明白问题是什么,但快捷方式会很好,否则我必须重... 查看详情