title: Spring Session with JDBC | Principal is null
body: <p>I use Spring Session with JDBC and Mysql.
  The Spring Security Login is successfully but the principal name is null.</p>
  
  <p>Does anyone have an idea, whats the mistake?</p>
  
  <p>Files:</p>
  
  <p>application.yaml:</p>
  
  <pre><code>  session:
      store-type: jdbc
      jdbc:
        initialize-schema: always
        table-name: SPRING_SESSION
  </code></pre>
  
  <p>Configuration:</p>
  
  <pre class="lang-java prettyprint-override"><code>@EnableGlobalMethodSecurity(prePostEnabled = true)
  @EnableWebSecurity
  @EnableJdbcHttpSession
  @EnableJpaRepositories(basePackageClasses = UsersRepository.class)
  @Configuration
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private CustomUserDetailsService userDetailsService;
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  
          auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
      }
  
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
  
          http.csrf().disable();
          http.authorizeRequests()
                  .antMatchers("/*").authenticated().anyRequest().permitAll()
                  .antMatchers("/sources/*").anonymous().anyRequest().permitAll()
                  .antMatchers("/public/*").anonymous().anyRequest().permitAll()
                  .and()
                  .formLogin().
                  loginPage("/login").
                  loginProcessingUrl("/app-login").
                  usernameParameter("app_username").
                  passwordParameter("app_password").
                  permitAll()
                  .and()
                  .exceptionHandling().
                  accessDeniedPage("/error403");
      }
  
      @Bean
      public BCryptPasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  }
  </code></pre>
  
  <p>Here is a picture of the database table: 
  <a href="https://i.stack.imgur.com/UmKvK.png" rel="nofollow noreferrer">link</a></p>
  
  <p>The pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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;sandbox&lt;/groupId&gt;
  &lt;artifactId&gt;TestApp&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.0.5.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
          &lt;artifactId&gt;spring-session-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
          &lt;artifactId&gt;spring-session&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
          &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jstl&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;properties&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
  &lt;/properties&gt;
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;executable&gt;true&lt;/executable&gt;
              &lt;/configuration&gt;
  
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>UPDATE 03/18/19</strong></p>
  
  <p>I can't change the Security strategy. <a href="https://i.stack.imgur.com/KniR2.jpg" rel="nofollow noreferrer">link</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I add the following Bean to my Security configuration, the principal_name in the database is no longer null. Now there is the username of the logged in user.
  BUT after each site reload, a new session is created, so the user can't say logged in.</p>
  
  <pre><code>@Bean
  public HttpSessionIdResolver httpSessionIdResolver(){
      return new HeaderHttpSessionIdResolver("X-Auth-Token");
  }
  </code></pre>