WebSecurityConfig:添加 属性 需要 Bean?
WebSecurityConfig: adding property requires Bean?
出于某种原因,每当我尝试向此 class 添加 属性 时,应用程序就会中断。无法弄清楚我需要配置哪个构造函数。
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final AppUserService appUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
//without this line it works
private String newProp;
private FacebookConnectionSignup facebookConnectionSignup;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider =
new DaoAuthenticationProvider();
provider.setPasswordEncoder(bCryptPasswordEncoder);
provider.setUserDetailsService(appUserService);
return provider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/v*/registration/**")
.permitAll()
.anyRequest()
.authenticated().and()
.formLogin();
}
@Bean
public ProviderSignInController providerSignInController() {
ConnectionFactoryLocator connectionFactoryLocator =
connectionFactoryLocator();
UsersConnectionRepository usersConnectionRepository =
getUsersConnectionRepository(connectionFactoryLocator);
((InMemoryUsersConnectionRepository) usersConnectionRepository)
.setConnectionSignUp(facebookConnectionSignup);
return new ProviderSignInController(connectionFactoryLocator,
usersConnectionRepository, new FacebookSignInAdapter());
}
private ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory("appId", "appSecret"));
return registry;
}
private UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator
connectionFactoryLocator) {
return new InMemoryUsersConnectionRepository(connectionFactoryLocator);
}
}
启动 ApplicationContext 时出错。要显示条件报告,请重新 运行 您的应用程序并启用 'debug'。
2021-12-17 00:40:17.874 错误 21160 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter :
应用程序启动失败
描述:
com.example.demo.security.config.WebSecurityConfig 中构造函数的参数 2 需要一个 'java.lang.String' 类型的 bean,但找不到。
操作:
考虑在您的配置中定义类型为 'java.lang.String' 的 bean。
进程已完成,退出代码为 1
而不是 AllArgsConstructor
使用 RequiredArgsConstructor
注释并最终完成此 属性:
private final FacebookConnectionSignup facebookConnectionSignup;
出于某种原因,每当我尝试向此 class 添加 属性 时,应用程序就会中断。无法弄清楚我需要配置哪个构造函数。
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final AppUserService appUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
//without this line it works
private String newProp;
private FacebookConnectionSignup facebookConnectionSignup;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider =
new DaoAuthenticationProvider();
provider.setPasswordEncoder(bCryptPasswordEncoder);
provider.setUserDetailsService(appUserService);
return provider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/v*/registration/**")
.permitAll()
.anyRequest()
.authenticated().and()
.formLogin();
}
@Bean
public ProviderSignInController providerSignInController() {
ConnectionFactoryLocator connectionFactoryLocator =
connectionFactoryLocator();
UsersConnectionRepository usersConnectionRepository =
getUsersConnectionRepository(connectionFactoryLocator);
((InMemoryUsersConnectionRepository) usersConnectionRepository)
.setConnectionSignUp(facebookConnectionSignup);
return new ProviderSignInController(connectionFactoryLocator,
usersConnectionRepository, new FacebookSignInAdapter());
}
private ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory("appId", "appSecret"));
return registry;
}
private UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator
connectionFactoryLocator) {
return new InMemoryUsersConnectionRepository(connectionFactoryLocator);
}
}
启动 ApplicationContext 时出错。要显示条件报告,请重新 运行 您的应用程序并启用 'debug'。 2021-12-17 00:40:17.874 错误 21160 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter :
应用程序启动失败
描述:
com.example.demo.security.config.WebSecurityConfig 中构造函数的参数 2 需要一个 'java.lang.String' 类型的 bean,但找不到。
操作:
考虑在您的配置中定义类型为 'java.lang.String' 的 bean。
进程已完成,退出代码为 1
而不是 AllArgsConstructor
使用 RequiredArgsConstructor
注释并最终完成此 属性:
private final FacebookConnectionSignup facebookConnectionSignup;