【问题标题】:Multiple web security causing ERR_TOO_MANY_REDIRECTS多个网络安全导致 ERR_TOO_MANY_REDIRECTS
【发布时间】:2016-06-18 17:16:28
【问题描述】:

我正在尝试通过 Java Config 配置 Spring Security 以处理我的应用程序上的两种身份验证:基于表单(用户登录)和基于令牌(REST api)。

表单配置很简单,除了我必须创建自己的SecuritySocialConfigurer 的部分(基本上是SpringSocialConfigurer 的副本,带有一个自定义身份验证成功处理程序,它生成一个 JWT 令牌并在响应中设置一个 cookie )。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    super.configure(auth);
    auth
        .userDetailsService(userDetailsService())
        .passwordEncoder(NoOpPasswordEncoder.getInstance());
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/img/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .formLogin()
            .loginPage("/signin")
            .loginProcessingUrl("/signin/authenticate")
            .failureUrl("/signin?param.error=bad_credentials")
        .and()
            .logout()
                .logoutUrl("/signout")
                .deleteCookies("JSESSIONID")
        .and()
            .authorizeRequests()
                .antMatchers("/admin/**", "favicon.ico", "/public/**", "/auth/**", "/signin/**").permitAll()
                .antMatchers("/**").hasRole("USER")
        .and()
            .rememberMe()
        .and()
            .apply(new MilesSocialSecurityConfigurer());
}

当仅使用此配置时,我可以访问 http://localhost:8080 并被重定向到 http://localhost:8080/signin 以执行登录。成功登录后,我检查 JWT 令牌 cookie 是否存在。

第二个安全配置目的是在调用 REST api 时检查 JWT 令牌的存在。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .addFilterAfter(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
        .antMatcher("/api/**")
            .csrf()
                .disable()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
            .exceptionHandling()
                .authenticationEntryPoint(restAuthenticationEntryPoint)
        .and()
            .authorizeRequests()
                .antMatchers("favicon.ico", "/public/**", "/auth/**", "/signin/**").permitAll()
                .antMatchers("/**").authenticated()
        ;
}

@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
    JwtAuthenticationFilter filter = new JwtAuthenticationFilter("/api/**");
    filter.setAuthenticationSuccessHandler(jwtAuthenticationSuccessHandler);
    filter.setAuthenticationManager(apiAuthenticationManager());
    return filter;
}

@Bean
public ProviderManager apiAuthenticationManager() {
    return new ProviderManager(Arrays.asList(jwtAuthenticationProvider));
}

JwtAuthenticationProvider 是一个解析 JWT 令牌并生成 UserDetails 对象或在令牌不存在或无效时抛出 AuthenticationException 的类。

当第二个配置到位时,我无法导航到 http://localhost:8080(或 /signin)来启动登录过程 - 浏览器返回 ERR_TOO_MANY_REDIRECTS。

我尝试了一些事情但没有成功。任何有关正在发生的事情的线索将不胜感激。

谢谢。

【问题讨论】:

    标签: spring-security jwt spring-social-google


    【解决方案1】:

    改变

    .antMatchers("/**").authenticated()
    

     .anyRequest().authenticated()
    

    这意味着任何与您明确定义的不匹配的 URL 都需要进行身份验证。

    【讨论】:

    • 很遗憾没有解决问题。尝试访问任何应用 URL 时,我仍然有 ERR_TOO_MANY_REDIRECTS。
    猜你喜欢
    • 2021-10-18
    • 2019-11-25
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多