前提・実現したいこと
前提:インスタンスを生成できません。
今回解決を望みませんが、SpringStarterProjectでLombokを入れた状態での起動には失敗しています。
Springbootで webアプリを作っています。
src/main/java/SecurityConfig.javaで
@Autowired
private final UserDetailsService userDetailsService;
しても、userDetailsServiceというインスタンスが、UserDetailServiceクラスに認識されていないのか、エラーになってしまいます。
Springbootに表示される指摘内容
The blank final field useDetailsService may not have been initialized.
Initialize final field 'useDetailsService' at declaration.これをクリックすると;の前に=nullが付与され、
private final UserDetailsService userDetailsService = null;に変換されます。(この時@Autowiredは消されます。)
実現したいこと:=nullを使わずに、インスタンスを生成したい。
質問内容
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;でインポートしたものを、private final UserDetailsService userDetailsService;
としてインスタンス生成する方法を教えていただけると幸いです。
よろしくお願いいたします。
発生している問題・エラーメッセージ
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@562398ac of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@562398ac of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider Caused by: java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@562398ac of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider@562398ac': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: A UserDetailsService must be set Caused by: java.lang.IllegalArgumentException: A UserDetailsService must be set
該当のソースコード
Java
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @RequiredArgsConstructor @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired //private final UserDetailsService userDetailsService; //本来は上のインスタンスを使いたいのですが、エラーになってしまうため、以下のコードで対処しています。 private final UserDetailsService userDetailsService = null; //作成したuserDetailsServiceを使用し、DBからユーザを参照できるようにする @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } }
試したこと
上のコードにも書いたが、userDetailService;で終わるとエラーになってしまうので、
private final UserDetailsService userDetailsService = null;
に変更しました。
実行すると、以下のエラーが出力されました。
java.lang.IllegalArgumentException: A UserDetailsService must be set
まだ回答がついていません
会員登録して回答してみよう