提前抱歉我的英文不好..
我是Spring的初学者。一位同事建议我使用Spring启动。目前我喜欢那样。
首先,我想创建一个与mySQL数据库链接的身份验证/登录模块。
我正在研究IntelliJ和phpMyAdmin。
这项工作有3个部分: - 认证系统 - 好的 - 数据库链接和基本操作 - 好的 - 身份验证和数据库之间的链接 - 不行。
目前,对于身份验证,我有这个文件:
Web security config.Java
package hello;
//imports
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
要将我的应用程序与数据库连接,我有这个文件:
application.properties
spring.datasource.url = jdbc:mysql://localhost/simulateur
spring.datasource.username = root
spring.datasource.password =
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
我喜欢这个解决方案,因为它记得我使用Play Framework的解决方案和我的企业解决方案。我希望保留这个文件:application.properties。一个配置文件似乎很棒。
为了连接所有这些,我在这个website上找到了解决方案。我必须在我的WebSecurityConfig.java中添加它:
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username=?");
}
并将此文件添加到文件MvcConfig.java中,其中包含具有正确参数的路由和其他功能:
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://localhost/simulateur");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("");
return driverManagerDataSource;
}
我的问题是我有重新定义数据库连接的印象。我想使用我的文件:application.properties。您是否有想法使用此文件并且不使用MvcConfig.java中的部分代码?
预先感谢您的帮助 ! :)
在spring boot中,您有两种定义数据源的方法,您可以在application.properties上定义它,也可以以编程方式执行。除非您需要一些非常具体的配置,或者您有多个数据源,否则您应该在application.properties上定义数据源配置,以便springboot为您自动配置所有内容。
在您提供的示例中,您要定义两次数据源,您应该删除MvcConfig.java。
我建议您阅读本文,了解如何使用sql数据库和spring boot Working with SQL databases
server.port = 8084
#create and drop tables and sequences, loads import.sql
spring.jpa.hibernate.ddl-auto=update
# MySQL settings
#spring.datasource.url=jdbc:Mysql://localhost:3306/simplehr
spring.datasource.url=jdbc:mysql://localhost:3306/myProject
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# HikariCP settings
spring.datasource.hikari.*
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5
# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} -
%msg%n
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.=error
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp