Home > other >  Failed to configure a DataSource error after introducing securityfilterchain configuration
Failed to configure a DataSource error after introducing securityfilterchain configuration

Time:01-01

I set up my spring boot application to use

@EnableJpaRepositories(...)
@EntityScan(...)

And it worked fine, connecting to database, creating tables and retrieving data as required.

Next step i decided to follow an example to introduce social login features and added configuration:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers("/", "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll()
                .loginPage("/login")
                .usernameParameter("email")
                .passwordParameter("pass")
                .successHandler(databaseLoginSuccessHandler)
                .and()
                .oauth2Login()
                .loginPage("/login")
                .userInfoEndpoint()
                .userService(oauth2UserService)
                .and()
                .successHandler(oauthLoginSuccessHandler)
                .and()
                .logout().logoutSuccessUrl("/").permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/403");
        return http.build();
    }

    @Autowired
    private CustomOAuth2UserService oauth2UserService;

    @Autowired
    private OAuthLoginSuccessHandler oauthLoginSuccessHandler;

    @Autowired
    private DatabaseLoginSuccessHandler databaseLoginSuccessHandler;
}

And it stopped working with error:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

The application.yaml provided:

spring:
  datasource:
    driver-class-name=org.postgresql.Driver
    url:jdbc:postgresql://localhost:5432/pennantrace
    username:admin
    password:123456
  jpa:
    hibernate:
      ddl-auto:create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
  security:
    oauth2:
      client:
        registration:
          google:
             etc ...

What am i doing wrong with these configurations?

CodePudding user response:

Your YAML file is not correctly formatted. First you need to separate driver-class-name and its value by a : instead of a =. Second, after each : that represents a map key, you need to put a whitespace. As per YAML 1.2.2 specification:

Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key/value pair.

Third, to avoid further mistakes, you might want to consider to put the value of url into quotes. Your final application.yaml then might look like:

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: "jdbc:postgresql://localhost:5432/pennantrace"
    username: admin
    password: 123456
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
# ...etc
  • Related