Spring security / Angular 9: разрешить пользователям переходить на страницу входа angular вместо входа в систему Spring Security по умолчанию - PullRequest
0 голосов
/ 08 мая 2020

Это сложный вопрос, но мне трудно понять, как остановить Spring Security от перенаправления пользователей на страницу входа по умолчанию для доступа к моему Angular сайту. Вместо этого я хочу использовать представление входа в систему, которое я создал с помощью Angular, чтобы отправить информацию для входа в spring. Маршрут для этого представления - / # / login, но пользователи всегда перенаправляются на / login, чтобы вместо этого входить через веб-страницу Spring Security по умолчанию.

Вот немного моего метода настройки для WebSecurityConfig

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    UserService userDetailsService;

    @Autowired
    LoginSuccessHandler loginSuccessHandler;

    @Autowired
    LoginFailureHandler loginFailureHandler;

    @Autowired
    private LogoutSuccessHandlerImpl logoutSuccessHandler;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Bean
    public UserDetailsService userDetailsService() {
        return this.userDetailsService;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public void configAuthBuilder(AuthenticationManagerBuilder authBuilder) throws Exception {
        authBuilder.userDetailsService(userDetailsService);
    }

    private CsrfTokenRepository csrfTokenRepository() {
        CookieCsrfTokenRepository rep = new CookieCsrfTokenRepository();
        rep.setCookieHttpOnly(false);
        return rep;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String[] publicUrls = new String[] {
                "/api/v1/login",
                "/api/v1/logout"
        };

        http.cors()
                .and()
            .httpBasic()
                .and()
            .csrf()
                .csrfTokenRepository(csrfTokenRepository())
                .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/api/v1/**"))
                .ignoringAntMatchers(publicUrls)
                .and()
            .formLogin()
                .loginProcessingUrl("/api/v1/login")
                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailureHandler)
                .and()
            .logout()
                .logoutUrl("/api/v1/logout")
                .invalidateHttpSession(true)
                .logoutSuccessHandler(logoutSuccessHandler)
                .and()
            .authorizeRequests()
                .antMatchers("/api/v1/**").hasRole("USER")
            .anyRequest()
                .authenticated()
                .and()
            .exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler);
    }

}

Проект отлично работает при работе с сервером Angular dev и сервером Spring (потому что они работают на разных портах), но когда мой проект Angular компилируется и обслуживается через Spring Boot, я застрял с этой проблемой .

...