Spring Boot Exception评估SpringEL表达式

时间:2018-12-26 11:16:29

标签: java spring spring-boot thymeleaf

  

错误4904--[nio-8080-exec-1] org.thymeleaf.TemplateEngine
  :[THYMELEAF] [http-nio-8080-exec-1]异常处理模板   “索引”:模板解析期间发生错误(模板:“类   路径资源[templates / index.html]“)

     

org.thymeleaf.exceptions.TemplateInputException:发生错误   在模板解析期间(模板:“类路径资源   [templates / 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>

3 个答案:

答案 0 :(得分:0)

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

<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
在thymeleaf页面的html模板中

xmlns:sec

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

答案 1 :(得分:0)

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

  

在控制器上设置参数

import 'package:mysql1/mysql1.dart';

Future main() async {
  // Open a connection (testdb should already exist)
  final connection = await MySqlConnection.connect(new ConnectionSettings(
      host: '10.0.2.2',
      port: 3306,
      user: 'root',
      password: '0123456789',
      db: 'development',
      ));
  var results = await connection.query('select * from tableName');
  for (var row in results) {
    print('${row[0]}');
  }

  // Finally, close the connection
  await connection.close();
}
  

在前端使用相同的内容

request.setAttribute("authorization", authorizationObject);

希望这会有所帮助,谢谢

答案 2 :(得分:0)

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

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>

目前一切正常!