2017-01-02 85 views
2

對不起,我的英語不好..彈簧靴,彈簧安全鏈接數據庫

我是一個初學者與春天。一位同事建議我使用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中的代碼部分嗎?



預先感謝您的幫助! :)

+0

您正在創建一個數據源的bean工作,這是如果你正在使用多個數據源爲前工作非常有幫助。, Mysql和Oracle一起。如果我正確理解你的問題,你只用一個數據源,所以你可以在沒有** MyConfig.java **類的情況下實現這一點。當你在application.properties中指定了數據源參數時,springboot會創建一個bean供你玩耍。你所需要做的就是自動調用JdbcTemplate對象。 – harshavmb

+0

你完全理解我的問題。我想在我的application.properties中定義我的數據源參數,但現在,我該如何使用它?我沒有找到沒有** MyConfig.java **在網上的實現的例子.. –

+0

試試這個http://stackoverflow.com/questions/27697190/spring-boot-autoconfiguration-with-jdbc-template-autowiring-datasource -issue – harshavmb

回答

0

在春季啓動時,您必須定義一個數據源的兩種方式,要麼你把它定義上的應用。屬性,或者以編程方式執行。除非你需要一些非常特殊的配置,或者你有多個數據源,否則你應該在你的application.properties中定義你的數據源配置,這樣springboot就會爲你自動配置所有的東西。

在您提供的示例中,您定義了兩次數據源,您應該刪除MvcConfig.java。

我建議你閱讀這篇文章,瞭解如何使用SQL數據庫和春季啓動Working with SQL databases

+0

非常感謝您的幫助。但是,如果我刪除** MvcConfig.java **中的代碼,我的應用程序將如何理解** WebSecurityConfig.java **中添加的代碼? –

+0

Springboot應該使用您在application.properties中提供的配置來實例化數據源。您的WebSecurityConfig應該照原樣運行。 – IgnacioL

+0

我剛剛檢查了你的application.properties配置。確保你提供了Spring需要配置數據源的所有信息。至少,您需要定義以下屬性:spring.datasource.url,spring.datasource.username, spring.datasource.password,** spring.datasource.driver-class-name ** – IgnacioL

0
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