2013-08-19 76 views
16

我有一個Spring + Thymeleaf項目,具有以下視圖代碼。秒:授權和sec:認證註釋不起作用

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

的秒:授權和秒:如預期認證屬性不工作 - 格始終顯示,即使沒有用戶登錄,並且跨度總是讀「米克爾」。

遵循我的控制器類中的相關片段。

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

      return "home"; 
    } 

的println語句,按預期工作 - 如果沒有用戶登錄,它打印「anonymousUser」,否則用戶名。

我在做什麼錯?

+0

可能的解決方案:http://stackoverflow.com/questions/32904857/secauthorize-returning-true-for-both-isauthenticated-and -isanonymous-in-thy/40492335#40492335 – bpgriner

回答

20

在比較我的應用程序和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上沒有提及這一事實。我希望這有助於未來面臨同樣問題的其他人。

+2

男人,謝謝! –

+1

另外不要忘記像我一樣 –

5

對於Java配置的版本,它的工作對我來說太加上春季安全方言:

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

yup包括'thymeleaf-extras-springsecurity3' maven依賴項!增加方言也爲我工作。 –

0

另外,你不妨驗證事件後清除模板緩存,所以你的模板是重新用新的認證數據進行處理。或者,使用ServletContextTemplateResolver.setNonCacheablePatterns()將對登錄會話敏感的模板設置爲非緩存(這是我所做的)。

13

在春天引導我不得不添加下面的依賴關係:

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

這對我而言沒有配置SpringTemplateEngine bean。我正在使用Spring引導1.4.1.RELEASE和Spring依賴管理插件0.5.1.RELEASE – Yasin