質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

1回答

7016閲覧

springsecurityでのDBの認証について

heavyuseman

総合スコア42

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

0クリップ

投稿2017/05/26 15:16

編集2017/05/28 04:25

いつもお世話になっております。
springsecurityでのDBの認証情報で質問があります。
springsecurityでのDBの認証情報を管理する機能を実装したのですが、DataSource
の箇所で下記のエラーが出ます。
2017-05-28 10:11:42.697 WARN 796 --- [ main] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-05-28 10:11:42.921 ERROR 796 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Field dataSource in com.tuyano.springboot.springsecurity.SecurityConfig required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

DBはMysqlを使用し、データソースがはApache Commons DBCPを使用しています。
JavaConfigでデータソースを定義しています。
上記のエラーの原因はDBをMysqlに設定しているため、データソースをMysql用のものを
設定していないことが原因なのでしょうか?
※ググったのですが、Mysql用のデータソースについての情報がなかったため
困惑しております。
それとも他の原因がありますでしょうか?
以上です。ご回答宜しくお願い致します。

java

1//Apache Commons DBCPのjavaconfig 2package com.tuyano.springboot.springsecurity; 3 4import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 5 6 7import javax.sql.DataSource; 8 9import org.apache.commons.dbcp.BasicDataSource; 10import org.springframework.beans.factory.annotation.Value; 11import org.springframework.context.annotation.Bean; 12import org.springframework.context.annotation.Configuration; 13import org.springframework.context.annotation.PropertySource; 14import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 15import org.springframework.core.io.ClassPathResource; 16import org.springframework.core.io.FileSystemResource; 17import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 18 19@Configuration 20@PropertySource("application.properties") 21public class DataSourceConfig { 22 23 @Value("${spring.datasource.driverClassName}") 24 private String driverName; 25 @Value("${spring.datasource.url}") 26 private String url; 27 @Value("${spring.datasource.username}") 28 private String userName; 29 @Value("${spring.datasource.password}") 30 private String password; 31 32 @Bean 33 public static PropertySourcesPlaceholderConfigurer propertyConfig() { 34 return new PropertySourcesPlaceholderConfigurer(); 35 } 36 37 @Bean 38 public DataSource dataSource() { 39 BasicDataSource ds = new BasicDataSource(); 40 ds.setDriverClassName(driverName); 41 ds.setUrl(url); 42 ds.setUsername(userName); 43 ds.setPassword(password); 44 return ds; 45 } 46 47} 48

java

1package com.tuyano.springboot.springsecurity; 2 3import org.springframework.context.annotation.Bean; 4import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 9 10import javax.sql.DataSource; 11 12import org.springframework.beans.factory.annotation.Autowired; 13import org.springframework.beans.factory.annotation.Qualifier; 14 15 16 17@EnableWebSecurity 18public class SecurityConfig extends WebSecurityConfigurerAdapter{ 19 @Qualifier("authDataSource") 20 @Autowired 21 private DataSource dataSource; 22 @Override 23 protected void configure(HttpSecurity http) throws Exception{ 24 http 25 .authorizeRequests() 26 .antMatchers("/First.html").permitAll() 27 .and() 28 .formLogin() 29 .loginPage("/First.html") 30 .loginProcessingUrl("/pricessLogin") 31 .defaultSuccessUrl("/Member.html") 32 .failureUrl("/user.html") 33 .usernameParameter("name") 34 .passwordParameter("password") 35 .and() 36 .csrf() 37 .disable(); 38 39 } 40 @Override 41 public void configure(AuthenticationManagerBuilder auth) throws Exception { 42 auth.jdbcAuthentication() 43 .dataSource(dataSource) 44 .usersByUsernameQuery( 45 "select name, password from Newaccount where name = ?") 46 .passwordEncoder(new BCryptPasswordEncoder()); 47 //.authoritiesByUsernameQuery( 48 // "select mail_address, role from accounts where mail_address = ?"); 49 } 50 51 52}

xml

1//Mysql用の設定ファイル 2spring.jpa.database=MYSQL 3spring.datasource.url=jdbc:mysql://localhost/mysql?useSSL=false 4spring.datasource.name=authDataSource 5spring.datasource.username=root 6spring.datasource.password=root 7spring.datasource.sqlScriptEncoding=UTF-8 8spring.datasource.driverClassName=com.mysql.jdbc.Driver 9security.user.role=USER,ADMIN 10security.basic.enabled=false

Gradle

1buildscript { 2 ext { 3 springBootVersion = '1.5.1.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11} 12 13apply plugin: 'java' 14apply plugin: 'eclipse-wtp' 15apply plugin: 'org.springframework.boot' 16apply plugin: 'war' 17 18war { 19 baseName = 'MybootApp' 20 version = '0.0.1-SNAPSHOT' 21} 22 23sourceCompatibility = 1.8 24 25repositories { 26 mavenCentral() 27} 28 29configurations { 30 providedRuntime 31} 32 33dependencies { 34 //SpringSecurityを使うため、追加 35 compile('org.springframework.boot:spring-boot-starter-security') 36 compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4') 37 compile('org.springframework.boot:spring-boot-starter-data-jpa') 38 compile('org.springframework.boot:spring-boot-starter-thymeleaf') 39 compile('org.springframework.boot:spring-boot-starter-web') 40 runtime('mysql:mysql-connector-java') 41 providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 42 testCompile('org.springframework.boot:spring-boot-starter-test') 43 // MySQL 44 //compile ("mysql:mysql-connector-java:$mySQLVersion") 45 //追記したMysqlのデータソース用 46 compile ('mysql:mysql-connector-java') 47 compile ('com.fasterxml.jackson.core:jackson-databind:2.8.8') 48 compile("org.springframework.boot:spring-boot-starter-thymeleaf") 49 compile 'commons-dbcp:commons-dbcp:1.4' 50 51} 52

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

java

1public class SecurityConfig extends WebSecurityConfigurerAdapter{ 2 @Autowired 3 @Qualifier("authDataSource") 4 private DataSource dataSource; 5.....

と、データソース名にauthDataSourceを指定していますので、

properties

1spring.datasource.name=authDataSource

を追加すればよいかと思います。

※ただし、認証用のデータソースと、アプリケーション内で使うデータソースを分けたい場合は、複数のデータソース定義ができるよう、xmlかJavaConfigでやるべきでしょうか。

Mkyong.com | Spring Security form login using database

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

投稿2017/05/27 03:29

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

heavyuseman

2017/05/28 01:50 編集

ご回答ありがとうございます。データソースをjavaconfigに設定したところ新たなエラーが発生しました。 今の所考えられる原因がDBをMysqlを使用しているにも関わらず、データソースをApache Commons DBCPを使用していることが原因なのではないかを考えております。 DBとデータソースの製品が違っていても大丈夫なのでしょうか? データソースもMysql用のものを使用したいのですが、ネットで検索しても情報が出てこないため困惑しております。 質問欄に新たにApache Commons DBCPのjavaconfigのソースを追記しました 以上です。ご回答宜しくお願い致します
A-pZ

2017/05/28 02:21

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html の spring.datasource.type は、基本的にはクラスパスから自動認識されます。そのため、gradleの compile 'commons-dbcp:commons-dbcp:1.4' を設定しているので、Commons-dbcpを使うように動いているかと。mysqlを利用した設定については、Spring公式のページ(https://spring.io/guides/gs/accessing-data-mysql/)から、▼Build with Gradle にて記載されていますよう、dependencies に compile 'mysql:mysql-connector-java' を追加すれば利用されるかと思います。
heavyuseman

2017/05/28 04:25 編集

mysql:mysql-connector-java-5.1.40.jarを追加したのですが、mysql:mysql-connector-java-5.1.40.jarの中にあるどのクラスをimportすれば、データソースに関連するメソッド等を利用できるようになるのか不明です。 ※すみませんmysql:mysql-connector-java-5.1.40.jarに関してググったのですが関連する情報が見つからなかったもので... gradleのソースを編集しました。 以上です。ご回答宜しくお願い致します
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問