2017-01-02 157 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