私はauthenticationProviderでautowiredオブジェクトを使用しようとしていますが、エラーが発生します。クラス内でサービスオブジェクトを使用しなければ問題はありませんが、サービスオブジェクトが必要です。AuthenticationProviderでAutowiredを使用しているエラー
customAuthenticationproviderクラス:セキュリティのため
@Configuration
class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean(destroyMethod = "close")
public Service service() {
return new Service("JPADatabase");
}
}
初期化:豆の
@Configuration
@EnableWebSecurity
@ComponentScan("view.controller")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider authenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}}
作成:カスタムプロバイダを使用している
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private Service service;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if (service.LoginCorrect(name, password)) {
// use the credentials
// and authenticate against the third-party system
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority(service.getPerson(name).getRole().role()));
return new UsernamePasswordAuthenticationToken(
name, password, grantedAuths);
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}}
はクラス
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplicationConfig.class, WebSecurityConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{DispatcherServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
エラーメッセージ:
警告:コンテキストの初期化中に発生した例外 - キャンセルリフレッシュしよう:org.springframework.beans.factory.UnsatisfiedDependencyException:エラー「webSecurityConfig」名前を持つBeanを作成するには:不満依存関係がフィールドを通して表現 " authenticationProvider ';ネストされた例外はorg.springframework.beans.factory.BeanCreationExceptionです:ファイル 'D:¥Users¥neuts¥Documents¥NetBeansProjects¥project-ip-vincentneuts¥spring-mvc-kopie¥target'に定義された 'customAuthenticationProvider' r0621919 \ WEB-INF \ classes \ view \ controller \ CustomAuthenticationProvider.class]:Beanのインスタンス化に失敗しました。ネストされた例外はorg.springframework.beans.BeanInstantiationExceptionです:[view.controller.CustomAuthenticationProvider]のインスタンス化に失敗しました:コンストラクタが例外をスローしました。ネストされた例外はjava.lang.RuntimeExceptionある:互換性のないソースコード - シンボル シンボルを見つけることができません:クラスAutowiredは 場所:クラスview.controller.CustomAuthenticationProvider 重度:コンテキストの初期化 org.springframework.beans.factory.UnsatisfiedDependencyExceptionを失敗:エラー作成豆を'webSecurityConfig'という名前では:フィールド 'authenticationProvider'で表現されている満足度の低い依存関係。ネストされた例外はorg.springframework.beans.factory.BeanCreationExceptionです:ファイル 'D:¥Users¥neuts¥Documents¥NetBeansProjects¥project-ip-vincentneuts¥spring-mvc-kopie¥target'に定義された 'customAuthenticationProvider' r0621919 \ WEB-INF \ classes \ view \ controller \ CustomAuthenticationProvider.class]:Beanのインスタンス化に失敗しました。ネストされた例外はorg.springframework.beans.BeanInstantiationExceptionです:[view.controller.CustomAuthenticationProvider]のインスタンス化に失敗しました:コンストラクタが例外をスローしました。ネストされた例外はjava.lang.RuntimeExceptionです:互換性のないソースコード - シンボル シンボル見つけることができません:クラスAutowired 場所:クラスview.controller.CustomAuthenticationProvider
EDIT:ITのみ、最初の実行で動作します。 (Glassfishサーバーを再起動した後)
私のnetbeansセッションの最初の実行時にのみ、プロジェクトが実行されていることが分かりました。 – VincentN
更新。私はglassfishサーバーを再起動しただけでも動作します。どのように私はこの問題を解決することができます任意のアイデア? – VincentN