【问题标题】:Spring boot + spring boot security start errorSpring Boot + Spring Boot 安全启动错误
【发布时间】:2020-08-25 04:49:00
【问题描述】:

我正在使用 spring boot 和 spring security 和 jsp 做一个 MVC 项目。我只是在训练我的弹簧,我有同样的项目在没有弹簧靴的情况下运行。目前我搬到了springboot,当我尝试开始时,我得到:

2020-05-09 17:28:38.521 信息 21308 --- [restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring 嵌入式 WebApplicationContext 2020-05-09 17:28:38.527 信息 21308 --- [restartedMain] os.web.context.ContextLoader:根 WebApplicationContext:初始化在 6813 毫秒内完成 2020-05-09 17:28:38.753 WARN 21308 --- [restartedMain] ConfigServletWebServerApplicationContext:遇到异常 在上下文初始化期间 - 取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“inMemoryDatabaseShutdownExecutor”的 bean 时出错 在类路径资源中定义 [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: 通过方法表达的不满足的依赖关系 'inMemoryDatabaseShutdownExecutor' 参数 0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径资源中定义的名称为“dataSource”的bean [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.zaxxer.hikari.HikariDataSource]:工厂方法 'dataSource' 抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: 无法确定合适的驱动程序类 2020-05-09 17:28:38.769 信息 21308 --- [restartedMain] o.apache.catalina.core.StandardService :停止服务 [Tomcat] 2020-05-09 17:28:38.826 信息 21308 --- [restartedMain] ConditionEvaluationReportLoggingListener:

***************************应用程序启动失败


说明:

未能配置数据源:未指定“url”属性并且 无法配置嵌入式数据源。

原因:无法确定合适的驱动程序类

我不知道发生了什么。

application.properties

# JDBC properties
#
app.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC
app.datasource.username=springstudent
app.datasource.password=springstudent

# Spring Data JPA properties
spring.data.jpa.repository.packages=com.crm.dao
spring.data.jpa.entity.packages-to-scan=com.crm.beans

#
# SECURITY JDBC properties
#
security.datasource.jdbc-url=jdbc:mysql://localhost:3306/spring_security_demo_bcrypt?useSSL=false&serverTimezone=UTC
security.datasource.username=springstudent
security.datasource.password=springstudent
security.datasource.driver-class-name= com.mysql.jdbc.Driver

配置:

    @Configuration
    @EnableWebSecurity
    public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

        // add a reference to our security data source

        @Autowired
        @Qualifier("securityDataSource")
        private DataSource securityDataSource;


        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {

            auth.jdbcAuthentication().dataSource(securityDataSource);

        }

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/resources/**");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            System.out.println("aplicando configuracion");
            http.authorizeRequests()
            .antMatchers("/employees/showForm*").hasAnyRole("MANAGER", "ADMIN")
            .antMatchers("/employees/save*").hasAnyRole("MANAGER", "ADMIN")
            .antMatchers("/employees/delete").hasRole("ADMIN")
            .antMatchers("/employees/**").hasRole("EMPLOYEE")
            .antMatchers("/resources/**").permitAll()
            .antMatchers("/showMyLoginPage").permitAll()
            .and()
            .formLogin()
                .loginPage("/showMyLoginPage")
                .loginProcessingUrl("/authenticateTheUser")
                .permitAll()
            .and()
            .logout().permitAll()
            .and()
            .exceptionHandling().accessDeniedPage("/access-denied");

        }


    }

配置:

    @Configuration
@EnableJpaRepositories(basePackages={"${spring.data.jpa.repository.packages}"})
public class DemoDataSourceConfig {

    @Primary
    @Bean
    @ConfigurationProperties(prefix="app.datasource")
    public DataSource appDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="spring.data.jpa.entity")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, DataSource appDataSource) {
        return builder
                .dataSource(appDataSource)
                .build();
    }

    @Bean
    @ConfigurationProperties(prefix="security.datasource")
    public DataSource securityDataSource() {
        return DataSourceBuilder.create().build();
    }
}

提前感谢您的帮助。

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    在您的属性文件中...

    app.datasource.* 更改为spring.datasource.*,如下所示..

    # JDBC properties
    #
    spring.datasource.url=jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC
    spring.datasource.username=springstudent
    spring.datasource.password=springstudent
    

    配置数据源失败:未指定“url”属性,无法配置嵌入式数据源。

    此错误消息提示当 Soring 查找数据库 URL 属性时......(它们有一个固定的属性名称......像spring.datasource.*)或者其他......如果您使用任何嵌入式数据库,如 DB2,那么它确实如此不需要任何url/username/password,只要依赖就足够了。

    在您的情况下,它不是嵌入式数据库。所以 spring 寻找的属性名称必须正确提供。

    【讨论】:

    • 你好,这样做,错误消失了,但是自定义配置(DemoSecurityConfig)没有应用。
    • 您好,您发布的错误仅与数据库连接问题有关。请提供更多详细信息......或者更好地在 Stackoverflow(社区推荐)上提出单独的问题,因为这将帮助不同的用户。
    猜你喜欢
    • 2018-10-09
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2020-06-11
    • 2017-03-10
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多