Skip to content
Advertisement

Spring – Change schema connection for persistent_logins

I am working on a Spring Boot web application and I am implementing the “Remember me” function.

I defined in my Web Security Configuration this:

http.authorizeRequests().and()
                    .rememberMe().tokenRepository(this.persistentTokenRepository())
                    .tokenValiditySeconds(1 * 24 * 60 * 60); // 24h

and

@Bean
        public PersistentTokenRepository persistentTokenRepository() {
            JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl();
            db.setDataSource(dataSource);
            return db;
        }

The problem is that when I flag the option on the html page, Spring try to add a token in the default schema of my database -> “public”.

Is there any way to change the default schema for that option? Everything else is linked correctly on the right schema through this property:

spring.jpa.properties.hibernate.default_schema=another_schema_name

I tried to make a personal implement of the class JdbcTokenRepositoryImpl but I can’t find a way to change the schema. I looked it up online but I didn’t find nothing..

Thank you

Regards, Mohamad

Advertisement

Answer

You may initialize differently your dataSource variable what you use in your PersistentTokenRepository bean. Most data sources support schema setting. For instance Spring’s org.springframework.jdbc.datasource.DriverManagerDataSource :

@Bean(name = "dataSource")
public DataSource getDataSource() {
  DriverManagerDataSource dataSource = new DriverManagerDataSource();
  // ... tipicly set username, password, driver class name, jdbc Url
  dataSource.setSchema(schema);
  return dataSource;
}

You could control the schema through the mentioned property: (spring.jpa.properties.hibernate.default_schema)

@Value("${spring.jpa.properties.hibernate.default_schema}")
private String schema;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement