2016-08-18 61 views
1

成功登錄後,認證顯然不爲空,但仍然登錄時強制出錯使認證爲空。Thymeleaf認證對象在錯誤頁面上爲空或爲空

  • 春季啓動1.3.1
  • Thymeleaf 2.1.4
  • Thymeleaf-Spring4 2.1.4
  • Thymeleaf-額外-SpringSecurity4

的error.html(自定義錯誤頁手柄錯誤/例外)

...   
<header th:include="fragments/menu :: menu"></header> 
... 

menu.html(所有菜單物品在錯誤期間不顯示)

... 
<li sec:authorize="hasAnyRole('ADMIN', 'MANAGER')"> 
... 
</li> 
... 
<li sec:authorize="isAuthenticated()"><a id="logoff" href="#logoff">Log Off</a></li> 
.. 

此行爲是否預期或我缺少什麼?我期望認證對象不爲空,所以我可以重新顯示安全的URL鏈接。

回答

0

看看在thymeleaf-extras-springsecurity GitHub的網站

您應該SpringSecurity方言添加到您的模板引擎

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> 
     <property name="additionalDialects"> 
     <set> 
      <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/> 
     </set> 
     </property> 
    </bean> 

希望這有助於

1

這是一個已知的問題與春啓動,即使記載於the documentation

NB 。如果您註冊了一個ErrorPage,並且該路徑最終將被過濾器處理(例如,像Jersey和Wicket這樣的非Spring Web框架),那麼過濾器必須被 明確註冊爲ERROR調度程序(默認 FilterRegistrationBean不包括ERROR調度程序類型)。

而且例如:

@Bean 
public FilterRegistrationBean myFilter() { 
    FilterRegistrationBean registration = new FilterRegistrationBean(); 
    registration.setFilter(new MyFilter()); 
    ... 
    registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class)); 
    return registration; 
} 

related issue in Spring Boot tracker見。

但是你可以通過添加以下行來application.properties配置它甚至因爲1.3.1簡單(見#4505):

security.filter-dispatcher-types: ASYNC, FORWARD, INCLUDE, REQUEST, ERROR 
+0

創建FilterRegistrationBean和重寫默認調度類型無助解決問題。但是,[issue#5638](https://github.com/spring-projects/spring-boot/issues/5638)通過禁止默認的BasicErrorController提供了一種解決方法。到目前爲止,這在Spring Boot 1.3.1 + Thymeleaf 2.1.4中運行良好。 – aalmero

相關問題