0

Маршрут /gateways нуждается в аутентификации.
Когда /gateways доступен в браузере я перенаправлены на /login и появляется следующий вид:Как отключить «Authentification required popup» в конфигурации Spring сервера?

enter image description here

Если /gateways доступен из angular2 приложения появляется следующее окно:

enter image description here

Конфигурация моей пружинной защиты:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 


    private static String REALM="Authentication"; 

    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 

     auth.inMemoryAuthentication().withUser("cris").password("123").roles("ADMIN"); 
     auth.inMemoryAuthentication().withUser("felix").password("felix123").roles("USER"); 
    } 

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

     http 

       .httpBasic() 

       .and() 
       .csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 

       .and() 
       .formLogin() 

       .and() 
       .authorizeRequests() 
       .antMatchers("/user", "/vehicles", "/signin", "/isautheticated").permitAll().anyRequest() 
       .authenticated(); 
    } 

// Access-Control-Allow-Origin header to be present 
    @Bean 
    public WebMvcConfigurer corsConfigurer() { 
     return new WebMvcConfigurerAdapter() { 
      @Override 
      public void addCorsMappings(CorsRegistry registry) { 
       registry.addMapping("/**"); 
      } 
     }; 
    } 

    /* To allow Pre-flight [OPTIONS] request from browser */ 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**"); 
    } 

Итак, как всплывающее окно можно отключить?

ответ

1

В вашей конфигурации следует указать formLogin() вместо httpBasic(). ваш метод configure должен выглядеть так.

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

    http 

      .formLogin() 
       .loginPage("/login"); 

      .and() 
      .csrf() 
      .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 

      .and() 
      .formLogin() 

      .and() 
      .authorizeRequests() 
      .antMatchers("/user", "/vehicles", "/signin", "/isautheticated").permitAll().anyRequest() 
      .authenticated(); 
} 
+0

Если я не помещаю 'HttpBasic', то логин не работает. – Cristian

0

Я думаю, что ваш запрос от angular2 принимает недопустимый Authorization Basic заголовок, он был обработан BasicAuthenticationFilter, и он бросил AuthenticationException и начать точку входа. Вы можете реализовать свою собственную точку входа, которая реализует AuthenticationEntryPoint, а затем вводить до BasicFilter, точка входа по умолчанию - BasicAuthenticationEntryPoint. Как вы можете видеть, он вернет ответный заголовок .

public void commence(HttpServletRequest request, HttpServletResponse response, 
      AuthenticationException authException) throws IOException, ServletException { 
     response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\""); 
     response.sendError(HttpServletResponse.SC_UNAUTHORIZED, 
       authException.getMessage()); 
    } 

 Смежные вопросы

  • Нет связанных вопросов^_^