Подтвердить что ты не робот

Spring Безопасность загрузки показывает всплывающее окно Http-Basic-Auth после неудачного входа в систему

В настоящее время я создаю простое приложение для школьного проекта, Spring Бэкэнд для загрузки и внешний интерфейс AngularJS, но у меня проблема с безопасностью, которую я не могу решить.

Вход в систему работает отлично, но когда я ввожу неправильный пароль, появляется всплывающее имя входа по умолчанию, что довольно раздражает. Я пробовал аннотацию "BasicWebSecurity" и помещал httpBassic в отключенное, но без результата (что означает, что процедура входа в систему больше не работает).

Мой класс безопасности:

package be.italent.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web){
        web.ignoring()
        .antMatchers("/scripts/**/*.{js,html}")
        .antMatchers("/views/about.html")
        .antMatchers("/views/detail.html")
        .antMatchers("/views/home.html")
        .antMatchers("/views/login.html")
        .antMatchers("/bower_components/**")
        .antMatchers("/resources/*.json");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic()
                    .and()
                .authorizeRequests()
                .antMatchers("/user", "/index.html", "/", "/projects/listHome", "/projects/{id}", "/categories", "/login").permitAll().anyRequest()
                .authenticated()
                    .and()
                .csrf().csrfTokenRepository(csrfTokenRepository())
                    .and()
                .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class).formLogin();
    }

    private Filter csrfHeaderFilter() {
        return new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request,
                                            HttpServletResponse response, FilterChain filterChain)
                    throws ServletException, IOException {
                CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
                        .getName());
                if (csrf != null) {
                    Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                    String token = csrf.getToken();
                    if (cookie == null || token != null
                            && !token.equals(cookie.getValue())) {
                        cookie = new Cookie("XSRF-TOKEN", token);
                        cookie.setPath("/");
                        response.addCookie(cookie);
                    }
                }
                filterChain.doFilter(request, response);
            }
        };
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }
}

Есть ли у кого-нибудь идея о том, как предотвратить отображение этого всплывающего окна, не нарушая остальных?

Решение

Добавил это в мой Angular config:

myAngularApp.config(['$httpProvider',
  function ($httpProvider) {
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  }
]);
4b9b3361

Ответ 1

Давайте начнем с вашей проблемы

Это не "всплывающее окно безопасности Spring Boot", это всплывающее окно браузера, которое появляется, если ответ вашего приложения Spring Boot содержит следующий заголовок:

WWW-Authenticate: Basic

В вашей конфигурации безопасности появляется .formLogin(). Это не должно быть обязательным. Несмотря на то, что вы хотите проходить аутентификацию через форму в приложении AngularJS, ваш веб-интерфейс является независимым клиентом javascript, который должен использовать httpBasic вместо входа в систему.

Как может выглядеть ваша конфигурация безопасности

Я удалил .formLogin():

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .httpBasic()
                .and()
            .authorizeRequests()
            .antMatchers("/user", "/index.html", "/", "/projects/listHome", "/projects/{id}", "/categories", "/login").permitAll().anyRequest()
            .authenticated()
                .and()
            .csrf().csrfTokenRepository(csrfTokenRepository())
                .and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}

Как бороться с всплывающим окном браузера

Как упоминалось ранее, всплывающее окно отображается, если ответ вашего приложения Spring Boot содержит заголовок WWW-Authenticate: Basic. Этот параметр не следует отключать для всех запросов в приложении Spring Boot, поскольку он позволяет очень легко исследовать API в вашем браузере.

Spring Security имеет конфигурацию по умолчанию, которая позволяет указывать приложению Spring Boot в каждом запросе не добавлять этот заголовок в ответ. Это делается путем установки следующего заголовка для вашего запроса:

X-Requested-With: XMLHttpRequest

Как добавить этот заголовок к каждому запросу, сделанному вашим приложением AngularJS

Вы можете просто добавить заголовок по умолчанию в конфигурацию приложения следующим образом:

yourAngularApp.config(['$httpProvider',
  function ($httpProvider) {
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  }
]);

Бэкэнд теперь будет отвечать 401-ответом, который вы должны обработать своим угловым приложением (например, перехватчиком).

Если вам нужен пример, как это сделать, вы можете взглянуть на мое приложение со списком покупок. Это сделано с весенним ботинком и угловым js.

Ответ 2

Как сказал Янник Клем, это происходит из-за заголовка

WWW-Authenticate: Basic

Но spring есть способ отключить его, и это действительно просто. В вашей конфигурации просто добавьте:

.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint)

и поскольку authenticationEntryPoint еще не определен, автоувертите его в начале:

@Autowired private MyBasicAuthenticationEntryPoint authenticationEntryPoint;

А теперь создайте MyBasicAuthenticationEntryPoint.class и вставьте следующий код:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.stereotype.Component;

@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

/**
 * Used to make customizable error messages and codes when login fails
 */
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx) 
  throws IOException, ServletException {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    PrintWriter writer = response.getWriter();
    writer.println("HTTP Status 401 - " + authEx.getMessage());
}

@Override
public void afterPropertiesSet() throws Exception {
    setRealmName("YOUR REALM");
    super.afterPropertiesSet();
}
}

Теперь ваше приложение не будет отправлять WWW-Authenticate: Basic заголовок, из-за того, что всплывающие окна не будут отображаться, и нет необходимости возиться с заголовками в Angular.

Ответ 3

Как уже объяснено выше, проблема заключается в заголовке ответа, который установлен со значениями "WWW-Authenticate: Basic".

Другое решение, которое может решить эту проблему, - реализовать интерфейс AuthenticationEntryPoint (напрямую), не помещая эти значения в заголовок:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    //(....)

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

        http
        .csrf()
            .disable()
        .authorizeRequests()
            .antMatchers("/*.css","/*.js","/*.jsp").permitAll()
            .antMatchers("/app/**").permitAll()
            .antMatchers("/login").permitAll()

            .anyRequest().authenticated()

            .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/", true)
                .failureUrl("/login?error=true")
                .usernameParameter("username")
                .passwordParameter("password")
                .permitAll()
            .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .deleteCookies("JSESSIONID")
                .clearAuthentication(true)
                .invalidateHttpSession(true)
            .and()
                .exceptionHandling()
                .accessDeniedPage("/view/error/forbidden.jsp")
            .and()
                .httpBasic()
                .authenticationEntryPoint(new AuthenticationEntryPoint(){ //<< implementing this interface
                    @Override
                    public void commence(HttpServletRequest request, HttpServletResponse response,
                        AuthenticationException authException) throws IOException, ServletException {
                            //>>> response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\""); <<< (((REMOVED)))
                            response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
                    }
                });
    }

    //(....)
}

Ответ 4

Привет я получаю ту же проблему для следующего кода. Пожалуйста, помогите. @Autowired public void configureGlobal (AuthenticationManagerBuilder auth) выдает исключение {

    auth
    .inMemoryAuthentication()
    .withUser("user")
    .password("password")
    .roles("user");

}

public void configure(WebSecurity web) {
    web
    .ignoring()
    .antMatchers("/resources/**");
}

protected void configure(HttpSecurity http) throws Exception {
    http
     .httpBasic()
     .and()
 .authorizeRequests()
 .antMatchers("/user", "/index.html", "/").permitAll().anyRequest()
 .authenticated()
     .and()
 .csrf();
}