Я пишу приложение, где пользователь входит в систему, используя facebook.АутентификацияSuccessEvent никогда не запускается
Мой класс безопасности конфигурации/приложения:
@SpringBootApplication
@EnableOAuth2Sso
@ComponentScan(basePackages = { "app" })
public class Application extends WebSecurityConfigurerAdapter {
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
private OAuth2ClientContext oauth2ClientContext;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/",
"/login**",
"/webjars/**",
"/bower_components/**",
"/assets/**",
"/app/**",
"/api/auth/isAuthenticated")
.permitAll()
.anyRequest()
.authenticated()
.and().formLogin().defaultSuccessUrl("/", true).loginPage("/login").permitAll()
.and().logout().logoutSuccessUrl("/").permitAll()
.and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
}
@Bean
@ConfigurationProperties("facebook")
ClientResources facebook() {
return new ClientResources();
}
private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();
filters.add(ssoFilter(facebook(), "/login/facebook"));
filter.setFilters(filters);
return filter;
}
private Filter ssoFilter(ClientResources client, String path) {
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
filter.setRestTemplate(facebookTemplate);
filter.setTokenServices(new UserInfoTokenServices(client.getResource().getUserInfoUri(), client.getClient().getClientId()));
return filter;
}
class ClientResources {
private OAuth2ProtectedResourceDetails client = new AuthorizationCodeResourceDetails();
private ResourceServerProperties resource = new ResourceServerProperties();
public OAuth2ProtectedResourceDetails getClient() {
return client;
}
public ResourceServerProperties getResource() {
return resource;
}
}
Моя проблема заключается в том, что даже я настроил слушателя:
package app;
@Component
public class AuthenticationListener implements ApplicationListener<AuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
System.out.println("Event fired");
}
}
он никогда не стрелял. Я попробовал решение, предлагаемое здесь: Spring boot OAuth successful login listener not triggering, но это тоже не помогает. SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
возвращается после регистрации.
Итак, есть ли обходной путь? Я имею в виду, как я могу выполнить некоторые действия после успешного входа? –
Думаю. Чтобы усугубить ситуацию, я также исправил [проблему в Spring Boot] (https://github.com/spring-projects/spring-boot/issues/5853), связанную с этим. Мое лучшее предположение заключается в том, что вы должны попытаться зарегистрировать 'OAuth2ClientAuthenticationProcessingFilter' самостоятельно. Проверьте изменения в реализации, вы можете, возможно, выполнить резервное копирование в своем собственном расширении. –