【问题标题】:Thymeleaf is not parsing "sec:authorize" attributeThymeleaf 未解析“sec:authorize”属性
【发布时间】:2013-10-04 17:33:45
【问题描述】:

我正在使用 thymeleaf-spring3 2.0.18Spring 3.1.1。我已经完成了一个可以正常工作的登录页面,但是在解析其他 Thymeleaf 属性时,“sec:authorize”没有,因为如果我查看生成视图的源代码,我可以看到它们。

我是否缺少某些东西,例如依赖项或特定配置?

这是我的 login.html:

<!DOCTYPE html>
<head>
...
</head> 

<body>
<div class="top">
    <div class="container">         
        <ul class="loginbar pull-right">
            <li sec:authorize="isAnonymous()"><a href="/login" class="login-btn">Login</a></li>   
            <li sec:authorize="isAuthenticated()" class="login-btn">Welcome <span sec:authentication="name">Bob</span></li>   
        </ul>
    </div>      
</div><!--/top-->

<!--=== Content Part ===-->
<div class="container">     
    <div class="row-fluid">
        <form name="f" th:action="@{/j_spring_security_check}" method="post" class="log-page">
            <h3>Login</h3>
            <div th:if="${loginError}" th:with="errorMsg=${session['SPRING_SECURITY_LAST_EXCEPTION'].message}" class="alert alert-error">
                Bad user or password.<br/>
                Cause: <span th:text="${errorMsg}">Wrong input!</span>
            </div>    
            <div class="input-prepend">
                <span class="add-on"><i class="icon-user"></i></span>
                <input name="j_username" class="input-xlarge" type="text" placeholder="Username" />
            </div>
            <div class="input-prepend">
                <span class="add-on"><i class="icon-lock"></i></span>
                <input name="j_password" class="input-xlarge" type="password" placeholder="Password" />
            </div>
            <div class="controls form-inline">
                <button class="btn-u pull-right" type="submit">Login</button>
            </div>
        </form>
    </div><!--/row-fluid-->
</div><!--/container-->     
<!--=== End Content Part ===-->

</body>
</html> 

【问题讨论】:

  • 注意:如果你使用的是Spring boot,只需要添加依赖就足够了。这对我有用,无需配置 SpringTemplateEngine bean。我正在使用 Spring boot 1.4.1.RELEASE 和 Spring 依赖管理插件 0.5.1.RELEASE

标签: spring-mvc spring-security thymeleaf


【解决方案1】:

通过查看this example,我意识到我需要向 pom.xml 添加一个依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity3</artifactId>
    <version>2.0.1</version>
    <scope>compile</scope>
</dependency>

并且还需要在templateEngine 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>

【讨论】:

  • 使用 Spring Boot 1.3.3 - 使用 start.spring.io 进行项目初始化 - sec:authorize 在手动将 dep 添加到项目 pom 之前也不起作用。
【解决方案2】:

我们遇到了类似的问题,我们通过在主控制器上下文配置中添加包扫描来解决它:

<context:component-scan base-package="org.thymeleaf.extras.springsecurity3" />

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2021-10-10
    • 2018-06-05
    • 2021-02-23
    • 2019-03-30
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多