本文介绍了sec:authorize 和 sec:authentication 注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下视图代码的 Spring + Thymeleaf 项目.

<头><title>联系人</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><身体><div id="内容"><h1>欢迎来到本站!</h1><p th:if="${loginError}">用户或密码错误</p><form th:action="@{/j_spring_security_check}" method="post"><label for="j_username">电子邮件地址</label>:<input type="text" id="j_username" name="j_username"/><br/><label for="j_password">密码</label>:<input type="password" id="j_password" name="j_password"/><br/><input type="submit" value="登录"/></表单>

<div sec:authorize="isAuthenticated()">用户:<span sec:authentication="name">miquel</span>

</html>

sec:authorize 和 sec:authentication 属性无法按预期工作 - div 始终显示,即使没有用户登录,并且跨度始终显示为miquel".

遵循我的控制器类中的相关片段.

@RequestMapping(value = "/welcome.html")公共字符串惠康(){身份验证 auth = SecurityContextHolder.getContext().getAuthentication();System.out.println("用户名:" + auth.getName());回家";}

println 语句按预期工作 - 如果没有用户登录,则打印anonymousUser",否则打印用户名.

我做错了什么?

解决方案

在将我的应用程序与 Thymeleaf & 密切比较后Spring Security 演示应用程序,我发现了错误的来源.

显然,为了让 Thymeleaf 处理 sec:authorizesec:authentication 属性,您需要注册 SpringSecurityDialect 作为附加模板引擎 bean 的方言.

<property name="templateResolver" ref="templateResolver"/><属性名称="additionalDialects"><设置><bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/></set></属性></bean>

这令人惊讶,因为相关的 Thymeleaf 文档页面 上没有提及这一事实.我希望这可以帮助将来遇到同样问题的其他人.

I have a Spring + Thymeleaf project with the following view code.

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
    <title>Contacts</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="content">
    <h1>Welcome to the site!</h1>
    <p th:if="${loginError}">Wrong user or password</p>
    <form th:action="@{/j_spring_security_check}" method="post">
        <label for="j_username">Email address</label>:
        <input type="text" id="j_username" name="j_username"/> <br/>
        <label for="j_password">Password</label>:
        <input type="password" id="j_password" name="j_password"/> <br/>
        <input type="submit" value="Log in"/>
    </form>
</div>

<div sec:authorize="isAuthenticated()">
    User: <span sec:authentication="name">miquel</span>
</div>
</body>
</html>

The sec:authorize and sec:authentication attributes don't work as expected - the div is always shown, even if no user is logged in, and the span always reads "miquel".

Follows a relevant snippet from my controller class.

@RequestMapping(value = "/welcome.html") 
public String wellcome() { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    System.out.println("username: " + auth.getName()); 

    return "home"; 
}

The println statement works as expected - if no user is logged in, it prints "anonymousUser", otherwise the username.

What am I doing wrong?

解决方案

After comparing my application closely to the Thymeleaf & Spring Security demo applicaiton, I discovered the source of the error.

Apparently, in order for Thymeleaf to process the sec:authorize and sec:authentication attributes, you need to register SpringSecurityDialect as an additional dialect of the template engine bean.

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <property name="additionalDialects">
        <set>
            <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect" />
        </set>
    </property>
</bean>

This is surprising as there is no mention of that fact on the related Thymeleaf documentation page. I hope this helps others who will face the same issue in future.

这篇关于sec:authorize 和 sec:authentication 注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:23