Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring thymeleaf中的身份验证对象为null或空_Spring_Maven_Spring Security_Spring Boot_Thymeleaf - Fatal编程技术网

Spring thymeleaf中的身份验证对象为null或空

Spring thymeleaf中的身份验证对象为null或空,spring,maven,spring-security,spring-boot,thymeleaf,Spring,Maven,Spring Security,Spring Boot,Thymeleaf,我使用的是Spring boot、Spring Security和Thymeleaf。目前,在访问页面时,身份验证和授权工作正常,但我无法使thymeleaf授权工作。流程非常简单:我尝试访问admin.html页面,我需要一个“admin”角色。Spring Security正确地拦截请求,首先将我转发到登录页面,当以管理员身份登录时,它允许我继续 现在我想根据角色在管理页面上“隐藏”一些内容。因此,在我的admin.html页面中,我有以下内容: 管理页面 秘密内容 你好,管理员! 此页

我使用的是Spring boot、Spring Security和Thymeleaf。目前,在访问页面时,身份验证和授权工作正常,但我无法使thymeleaf授权工作。流程非常简单:我尝试访问admin.html页面,我需要一个“admin”角色。Spring Security正确地拦截请求,首先将我转发到登录页面,当以管理员身份登录时,它允许我继续

现在我想根据角色在管理页面上“隐藏”一些内容。因此,在我的admin.html页面中,我有以下内容:


管理页面
秘密内容
你好,管理员!

此页面允许您执行管理任务


通过更改pom.xml中的以下依赖项进行修复:

 <!-- Thymeleaf Dialect -->
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

org.thymeleaf.extras
thymeleaf-extras-springsecurity4
2.1.2.1发布
而不是3.0.0.BETA01。现在进行以下工作:


此内容仅显示给管理员。

您可能忘记用
@controller
注释标记任何控制器。它发生在我的案例中,修复它有助于修复错误。这可能是故障排除步骤之一

发生这种情况的原因是,当您在运行时不使用
@controller
标记控制器并尝试从模板语言(在我的例子中是Thymeleaf)进行引用时,它会在上下文中向下延伸并返回丢失身份验证对象,因此会出现如下错误:

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#authorization.expression('!isAuthenticated()')" (template: "fragments/layout" - line 64, col 8)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 47 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#authorization.expression('!isAuthenticated()')" (template: "fragments/layout" - line 64, col 8)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
...
...
...
...
... 49 more
Caused by: java.lang.IllegalArgumentException: Authentication object cannot be null
    at org.springframework.security.access.expression.SecurityExpressionRoot.<init>(SecurityExpressionRoot.java:61)
原因:org.attoparser.ParseException:异常评估SpringEL表达式:“#authorization.expression(“!isAuthenticated()”)”(模板:“片段/布局”-第64行,第8列)
位于org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
位于org.attoparser.MarkupParser.parse(MarkupParser.java:257)
位于org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 47多
原因:org.thymeleaf.exceptions.TemplateProcessingException:异常评估SpringEL表达式:“#authorization.expression(“!isAuthenticated()”)”(模板:“片段/布局”-第64行,第8列)
位于org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
...
...
...
...
... 49多
原因:java.lang.IllegalArgumentException:身份验证对象不能为null
位于org.springframework.security.access.expression.SecurityExpressionRoot.(SecurityExpressionRoot.java:61)
请让我知道它是否有效