對不起,我的英語不好..彈簧靴,彈簧安全鏈接數據庫
我是一個初學者與春天。一位同事建議我使用Spring引導開始。我喜歡那一刻。
開始我想創建一個與mySQL數據庫鏈接的認證/登錄模塊。
我正在使用IntelliJ和phpMyAdmin。
對於這項工作有3個部分:
- 認證系統 - 確定
- 數據庫連接和基本操作 - OK
- NOT OK - 認證和數據庫之間的鏈接。
就目前而言,對於驗證我有這個文件:
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");
}
}
要與我有數據庫連接我aplication此文件:
個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框架和我的企業公司解決方案。我希望保留這個文件: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中的代碼部分嗎?
預先感謝您的幫助! :)
您正在創建一個數據源的bean工作,這是如果你正在使用多個數據源爲前工作非常有幫助。, Mysql和Oracle一起。如果我正確理解你的問題,你只用一個數據源,所以你可以在沒有** MyConfig.java **類的情況下實現這一點。當你在application.properties中指定了數據源參數時,springboot會創建一個bean供你玩耍。你所需要做的就是自動調用JdbcTemplate對象。 – harshavmb
你完全理解我的問題。我想在我的application.properties中定義我的數據源參數,但現在,我該如何使用它?我沒有找到沒有** MyConfig.java **在網上的實現的例子.. –
試試這個http://stackoverflow.com/questions/27697190/spring-boot-autoconfiguration-with-jdbc-template-autowiring-datasource -issue – harshavmb