2013-09-28 36 views
3

我正在使用thymeleaf-spring3 2.0.18Spring 3.1.1。我做了一個可以正常工作的登錄頁面,但是當其他Thymeleaf屬性被解析時,「sec:authorize」不是,因爲如果我查看生成視圖的源代碼,我可以看到它們。百里香不解析「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> 
+0

的可能重複的[秒:授權和秒:認證註釋不工作(http://stackoverflow.com/questions/18309864/secauthorize-and -secauthentication-annotations-dont-work) –

+0

注意:如果您使用的是Spring引導,只需添加依賴關係即可。這對我而言沒有配置SpringTemplateEngine bean。我正在使用Spring引導1.4.1.RELEASE和Spring依賴管理插件0.5.1.RELEASE – Yasin

回答

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 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> 
+0

使用Spring Boot 1.3.3 - 項目init使用start.spring.io - sec:授權在將dep手動添加到項目之前也不起作用POM。 – demaniak

0

我們也有類似的問題,我們通過一個解決它dding包掃描在主控制器的上下文配置:

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