title: Spring, Spring Security and Thymeleaf, Is user Logged In Or Not
body: <p>I am implementing a Spring MVC Web Application. I have added Spring Security to my application. I wanted to add a navigation bar, where Login and Logout buttons appear and disappear accordingly to whether the user is logged in or not. I searched the web and I saw thatthis can be done by using Thymeleaf. Therefore, I added it to my project and tried to experiment around it.
  Even though I tried the following, nothing work.</p>
  
  <pre><code>&lt;div th:if="${name!='eleni'}"&gt;
     &lt;div&gt;Welcome, ${name}&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div th:if="${name=='eleni'}"&gt;
     &lt;div&gt;Welcome, ${name}&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Both of the "Welcome, ..." would appear. The parameter ${name} works correctly, if I'm logged in as 'eleni' it displays it, if not it's left blank.
    Afterwards, I tried using sec:authorize:</p>
  
  <pre><code>&lt;div sec:authorize="!hasRole('USER')" &gt; Not a User &lt;/div&gt;
  &lt;div sec:authorize="hasRole('USER')" &gt;  User &lt;/div&gt;
  </code></pre>
  
  <p>Again both of these would show up.</p>
  
  <p>I'm posting some of the relevant code.
  In my navigation.jspf I've included:</p>
  
  <pre><code>&lt;html xmlns:th="http://www.thymeleaf.org"
   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"&gt;
  </code></pre>
  
  <p>In my SecurityConfiguration.java:</p>
  
  <pre><code>@Autowired
  public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
          throws Exception {
      auth.inMemoryAuthentication().withUser("eleni").password("1")
              .roles("USER");
  }
  </code></pre>
  
  <p>And finally my pom.xml:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.todo&lt;/groupId&gt;
  &lt;artifactId&gt;todo-springmvc&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax&lt;/groupId&gt;
          &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
          &lt;version&gt;6.0&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;4.3.9.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web --&gt;
      &lt;dependency&gt;
           &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
           &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
           &lt;version&gt;4.2.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --&gt;
  &lt;!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
          &lt;artifactId&gt;thymeleaf-spring4&lt;/artifactId&gt;
           &lt;version&gt;3.0.10.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
           &lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt;
           &lt;artifactId&gt;thymeleaf-extras-springsecurity4&lt;/artifactId&gt;
           &lt;version&gt;3.0.4.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
          &lt;artifactId&gt;json-path&lt;/artifactId&gt;
          &lt;version&gt;2.2.0&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;4.2.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  &lt;!-- https://mvnrepository.com/artifact/javax.servlet/jstl --&gt;
      &lt;dependency&gt;
           &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
           &lt;artifactId&gt;jstl&lt;/artifactId&gt;
           &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;pluginManagement&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;verbose&gt;true&lt;/verbose&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                      &lt;showWarnings&gt;true&lt;/showWarnings&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.tomcat.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat7-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;path&gt;/&lt;/path&gt;
                      &lt;contextReloadable&gt;true&lt;/contextReloadable&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/pluginManagement&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>I genuinely do not know what I am doing wrong, I feel pretty dumb at this point. I feel like a logical explanation would be some sort of incompatibility of thymeleaf with the rest of the dependencies. I am sorry for the wall of text, yet I need a solution or some advice!
     Thank you a lot in advance!</p>