Spring Security successHandler не вызывается после успешного входа в систему - код возврата 302 - PullRequest
0 голосов
/ 15 января 2019

После успешного входа в систему (пользователь перенаправляется на страницу, определенную в defaultSuccessUrl), возвращается код состояния 302. Кажется, что успех Хэндлера не называется. Я хотел бы получить код состояния 200. Аутентификация отлично работает с использованием пользовательского интерфейса, но она мне нужна и для вызовов REST API. Вот фрагменты кода:

Конфигурация

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
    .csrf().disable()
    .exceptionHandling()
    .authenticationEntryPoint(restAuthenticationEntryPoint)
    .and()
    .authorizeRequests()
    .antMatchers("/console/**").hasRole("ADMIN").anyRequest()
    .authenticated()
    .and()
    .formLogin()
    .loginPage("/login.xhtml")
    .loginProcessingUrl("/login")
    .permitAll()
    .successHandler(mySuccessHandler)
    .failureUrl("/console")
    .failureHandler(new CustomAuthenticationFailureHandler())
    .and()
    .logout()
    .invalidateHttpSession(true)
    .deleteCookies("JSESSIONID")
    .permitAll();

    http.headers().frameOptions().disable();

    http.formLogin().defaultSuccessUrl("/jobs.xhtml", true);

    http.logout().logoutSuccessUrl("/login.xhtml");

}

MvcConfiguration

@Configuration
 public class MvcConfig implements WebMvcConfigurer {

public void addViewControllers(ViewControllerRegistry registry) {

    registry.addViewController("/login").setViewName("login.xhtml");
    registry.addViewController("/").setViewName("login.xhtml");
    registry.addViewController("/jobs").setViewName("jobs.xhtml");

}

successHandler

@Component
public class MySavedRequestAwareAuthenticationSuccessHandler extends 
SimpleUrlAuthenticationSuccessHandler {

private RequestCache requestCache = new HttpSessionRequestCache();

@Override
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws ServletException, IOException {
    final SavedRequest savedRequest = requestCache.getRequest(request, response);

    if (savedRequest == null) {
        clearAuthenticationAttributes(request);
        return;
    }
    final String targetUrlParameter = getTargetUrlParameter();
    if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
        requestCache.removeRequest(request, response);
        clearAuthenticationAttributes(request);
        return;
    }

    clearAuthenticationAttributes(request);
}

public void setRequestCache(final RequestCache requestCache) {
    this.requestCache = requestCache;
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...