Spring Boot Exception, оценивающий выражение SpringEL - PullRequest
0 голосов
/ 26 декабря 2018

ОШИБКА 4904 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine
: [THYMELEAF] [http-nio-8080-exec-1] Шаблон обработки исключений «index»:Произошла ошибка при разборе шаблона (template: "ресурс пути к классу [templates / index.html]")

org.thymeleaf.exceptions.TemplateInputException: Произошла ошибка при разборе шаблона (template: "ресурс пути к классу [templates / index.html] ") в org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse (AbstractMarkupTemplateParser.java:241) ~ [thymeleaf-3.0.11.RELEASE.jar: 3.0.11.RELEASE] в org.y.templateparser.markup.[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:j): Диспетчер[spring-webmvc-5.1.3.RELEASE.jar: 5.1.3.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java:942) [spring-webmvc-5.1.3.RELEASE.jar: 5.1.3.RELEASE] at 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 '') ') "(template:" fragments / navbar "- строка 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 голосов
/ 26 декабря 2018

Если вы проходите авторизацию в модели, как показано ниже -

установите параметры на контроллере

request.setAttribute("authorization", authorizationObject);

используйте то же самое на фронтекак показано ниже

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

Надеюсь, это будет полезно, спасибо

0 голосов
/ 26 декабря 2018

Я добавил перехватчик, который при каждом запросе отправляет Роль

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>

Пока все работает!

0 голосов
/ 26 декабря 2018

Убедитесь, что вы thymeleaf-extras-springsecurity4 в pom.xml

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

и xmlns: sec в html-шаблоне на странице тимилиста.

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...