提前为我的糟糕英语道歉。
我是春天的初学者。一位同事建议我使用Spring开始。就目前而言我喜欢这个。
首先,我希望创建一个与mySQL数据库链接的身份验证/登录模块。
我正在研究IntelliJ和phpMyAdmin。
就这项工作而言,有三个部分:
目前,对于身份验证,我有以下文件:
WebSecurityConfig.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我喜欢这个解决方案,因为它是记住我的解决方案与播放框架和解决方案在我的企业。我希望保留这个文件: application.properties。一个配置文件看起来很棒。
为了链接所有这些,我在这个网站上找到了一个解决方案。我必须在我的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中的代码部分吗?
谢谢你的帮助!)
发布于 2017-01-02 16:24:09
在spring引导中,您有两种定义数据源的方法,一种是在application.properties上定义数据源,另一种是通过编程方式定义数据源。除非您需要一些非常特定的配置,或者您有多个数据源,否则您应该在application.properties上定义数据源配置,这样springboot就可以为您自动配置一切。
在提供了两次定义数据源的示例中,应该删除MvcConfig.java。
我建议您阅读本文,介绍如何使用sql数据库和spring引导使用SQL数据库。
发布于 2017-12-23 23:59:00
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:.jsphttps://stackoverflow.com/questions/41429844
复制相似问题