2013-08-19 36 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