Thymeleaf没有解析“sec:authorize”属性

时间:2013-09-28 13:13:51

标签: spring-mvc spring-security thymeleaf

我正在使用 thymeleaf-spring3 2.0.18 Spring 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> 

2 个答案:

答案 0 :(得分:11)

通过查看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>

答案 1 :(得分:0)

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

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