【问题标题】:sec:authorize and sec:authentication annotations don't worksec:authorize 和 sec:authentication 注释不起作用
【发布时间】:2013-08-21 00:25:14
【问题描述】:

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

<!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>

sec:authorize 和 sec:authentication 属性无法按预期工作 - 始终显示 div,即使没有用户登录也是如此,并且 span 始终显示为“miquel”。

遵循我的控制器类中的相关 sn-p。

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

    return "home"; 
}

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

我做错了什么?

【问题讨论】:

标签: spring-mvc spring-security thymeleaf


【解决方案1】:

在将我的应用程序与 Thymeleaf 和 Spring Security 演示应用程序进行了仔细比较后,我发现了错误的根源。

显然,为了让 Thymeleaf 处理 sec:authorizesec:authentication 属性,您需要将 SpringSecurityDialect 注册为模板引擎 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>

这很令人惊讶,因为the related Thymeleaf documentation page 上没有提及这一事实。我希望这可以帮助其他将来面临同样问题的人。

【讨论】:

  • 也不要忘记像我一样包含 thymeleaf-extras-springsecurity3 maven 依赖项
【解决方案2】:

在 Spring Boot 中,我只需要添加以下依赖项:

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

【讨论】:

  • 这对我有用,无需配置 SpringTemplateEngine bean。我正在使用 Spring boot 1.4.1.RELEASE 和 Spring 依赖管理插件 0.5.1.RELEASE
【解决方案3】:

对于 java config 版本,通过添加 spring 安全方言,它也对我有用:

 @Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new TilesDialect());
    templateEngine.addDialect(new SpringSecurityDialect());
    return templateEngine;
}

【讨论】:

    【解决方案4】:

    此外,您可能希望在身份验证事件后清除模板缓存,以便使用新的身份验证数据重新处理您的模板。或者,使用ServletContextTemplateResolver.setNonCacheablePatterns() 将对登录会话敏感的模板设置为非缓存(这就是我所做的)。

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 2015-11-16
      • 2021-06-20
      • 1970-01-01
      • 2021-10-11
      • 2014-08-14
      • 1970-01-01
      • 2019-03-12
      • 2014-05-26
      相关资源
      最近更新 更多