首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring,与数据库连接的Spring安全性

问Spring,与数据库连接的Spring安全性
EN

Stack Overflow用户
提问于 2017-01-02 15:58:10
回答 2查看 5.6K关注 0票数 4

提前为我的糟糕英语道歉。

我是春天的初学者。一位同事建议我使用Spring开始。就目前而言我喜欢这个。

首先,我希望创建一个与mySQL数据库链接的身份验证/登录模块。

我正在研究IntelliJ和phpMyAdmin。

就这项工作而言,有三个部分:

  • 认证系统-好的
  • 数据库链接和基本操作-好的
  • 身份验证和数据库之间的链接-不确定。

目前,对于身份验证,我有以下文件:

WebSecurityConfig.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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中添加以下内容:

代码语言:javascript
复制
@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文件中添加具有正确参数的路由和其他特性:

代码语言:javascript
复制
@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中的代码部分吗?

谢谢你的帮助!)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-02 16:24:09

在spring引导中,您有两种定义数据源的方法,一种是在application.properties上定义数据源,另一种是通过编程方式定义数据源。除非您需要一些非常特定的配置,或者您有多个数据源,否则您应该在application.properties上定义数据源配置,这样springboot就可以为您自动配置一切。

在提供了两次定义数据源的示例中,应该删除MvcConfig.java。

我建议您阅读本文,介绍如何使用sql数据库和spring引导使用SQL数据库。

票数 0
EN

Stack Overflow用户

发布于 2017-12-23 23:59:00

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41429844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档