You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

引入SecurityFilterChain配置后Spring Boot应用DataSource配置失败问题排查求助

引入SecurityFilterChain配置后Spring Boot应用DataSource配置失败问题排查求助

各位好,我最近在给Spring Boot应用添加社交登录功能时遇到了一个棘手的问题,想请大家帮忙排查下:

之前我的应用只配置了@EnableJpaRepositories(...)和@EntityScan(...),一切运行正常——能正常连接数据库、自动创建数据表,数据的增删改查也都没问题。

后来我照着示例添加了OAuth2登录的安全配置类,代码如下:

@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;
}

结果添加完这个配置后,应用启动直接抛出了数据库连接错误,具体报错信息如下:

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

我的application.yaml配置内容如下:

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 ...

我实在搞不懂问题出在哪——明明之前数据库配置是完全正常的,怎么加了安全配置就突然识别不了数据源了?有没有大佬能帮我看看我哪里配置出错了?

备注:内容来源于stack exchange,提问作者maximus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 08:23:13