0

I have String Standard app (no Boot) and use @EnableJdbcHttpSession to store session i database.

However session was store in database without PRINCIPAL_NAME column that is NULL, also on my index.jsp page ${sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal} is empty. When i haven't @EnableJdbcHttpSession annotation my principal user is correctly written.

In my pom.xml have:

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.3.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-core</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>

Also I have datasource class with @EnableJdbcHttpSession:

@EnableTransactionManagement
@EnableJdbcHttpSession
public class Datasource{

@Autowired
private AppEnvironment env;

@Bean(name = "dataSource")
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getDriverClassName());
    dataSource.setUrl(env.getDatabaseUrl());
    dataSource.setUsername(env.getDatabaseUserName());
    dataSource.setPassword(env.getDatabasePassword());
    dataSource.setValidationQuery(env.getDatabaseValidationQuery());
    dataSource.setTestOnBorrow(env.isTomcatTestOnBorrow());
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
    jpaAdapter.setShowSql(Boolean.valueOf(env.isJpaShowSql()));
    jpaAdapter.setDatabase(Database.SQL_SERVER);

    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setPersistenceProvider(new HibernatePersistenceProvider());
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "com.humanbizz.web.entities" });
    em.setPersistenceUnitName("persistenceUnit");
    em.setJpaVendorAdapter(jpaAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", env.getHibernateDialect());
    properties.setProperty("hibernate.default_catalog", env.getDefaultCatalog());
    properties.setProperty("hibernate.hbm2ddl.auto", env.getHbm2ddlAuto());

    properties.setProperty("hibernate.id.new_generator_mappings", String.valueOf(env.isIdNewGeneratorMappings()));
    properties.setProperty("hibernate.default_schema", env.getDefaultSchema());
    properties.setProperty("hibernate.connection.useUnicode", String.valueOf(env.isConnectionUseUnicode()));
    properties.setProperty("hibernate.connection.charSet", env.getConnectionCharSet());
    return properties;
}


}

and separatly three initializer class:

@Order(1)
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { Datasource.class, WebSecurity.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebMvc.class };
    }        

    @Override
    protected String[] getServletMappings() {
         return new String[]{"/"};
    }

}

and

@Order(2)
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {

}

and

@Order(3)
public class SpringSessionInicializer extends AbstractHttpSessionApplicationInitializer {

}

Somewhere JdbcSession override HttpSession but I don't know where and how to fix that, if someone have idea how I can solve this problem is welcome me, thanks

Todor27
  • 3
  • 1
  • 7
  • I found the answer to my problem on another issue https://stackoverflow.com/a/31476073/9197647 – Todor27 Apr 19 '18 at 12:38
  • First off, you shouldn't be combining different versions of Spring Session as you do in the first code snippet. Since you're using JDBC backed session store, you only need `spring-session-jdbc` dependency. Also could you provide a [minimal, complete and verifiable sample app](https://stackoverflow.com/help/mcve) that we could use to reproduce the problem? – Vedran Pavic Apr 20 '18 at 12:18

0 Answers0