对不起,我的英语不好..弹簧靴,弹簧安全链接数据库
我是一个初学者与春天。一位同事建议我使用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