isAuthenticated和isAnonymous同时返回false

时间:2016-06-02 12:23:18

标签: spring spring-security spring-boot thymeleaf

在我目前的春季项目中,我看到了这个HTML代码:

<th:block sec:authorize="isAuthenticated()">
  <h2 sec:authentication="name"></h2>
</th:block>
<th:block sec:authorize="isAnonymous()">
  <p> <a th:href="@{/loginPage}">Login</a> </p>
  <p>
    <ul>
      <li> <a th:href="@{/homeFacebook}">Connect to Facebook</a> </li>
      <li> <a th:href="@{/homeTwitter}">Connect to Twitter</a> </li>
    </ul>
  </p>
</th:block>

当我运行应用程序时,页面中没有显示任何内容,因为条件sec:authorize="isAuthenticated()"sec:authorize="isAnonymous()"都返回false;

我的配置:

在我的Application.java类

  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine  =  new SpringTemplateEngine();
    engine.addDialect( new SpringSecurityDialect() );
    return engine;
  }

在我的pom.xml文件中

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring3</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

我有其他类似配置的项目工作正常,但在这一个我坚持这个问题。有人可以看到这里有什么问题吗?

更新

我将pom.xml更改为:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring3</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>LATEST</version>
    </dependency>

现在不显示任何内容,而是显示两个块。 (如果我从pom.xml文件中的依赖项列表中删除thymeleaf-spring3,该页面将不显示任何块,如前所述)

更新2

使用此代码,页面不显示任何内容:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>

用那个显示两个块:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
        <version>LATEST</version>
    </dependency>

但未处理sec:authentication

0 个答案:

没有答案