Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当尝试访问web的某些部分而不是重定向到登录页面时,提供简单的HTTP401_Java_Spring_Spring Security - Fatal编程技术网

Java 当尝试访问web的某些部分而不是重定向到登录页面时,提供简单的HTTP401

Java 当尝试访问web的某些部分而不是重定向到登录页面时,提供简单的HTTP401,java,spring,spring-security,Java,Spring,Spring Security,我已经使用SpringSecurity制作了一个spring页面。当我尝试访问此页面内的任何url时,如果未设置会话,它会将您重定向到登录页面:/login。这很好,但现在我在这个web中创建了一个简单的HTTPRESTAPI。如果我试图访问/api/**中的任何url,我想要的是删除401,而不是向login发送HTTP重定向 我制作了一个带有预处理功能的过滤器: 公共类BusinessKeyInterceptor扩展了HandlerInterceptorAdapter{ @Override

我已经使用SpringSecurity制作了一个spring页面。当我尝试访问此页面内的任何url时,如果未设置会话,它会将您重定向到登录页面:/login。这很好,但现在我在这个web中创建了一个简单的HTTPRESTAPI。如果我试图访问/api/**中的任何url,我想要的是删除401,而不是向login发送HTTP重定向

我制作了一个带有预处理功能的过滤器:

公共类BusinessKeyInterceptor扩展了HandlerInterceptorAdapter{

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.isAuthenticated()
            &&
            // when Anonymous Authentication is enabled
            !(auth instanceof AnonymousAuthenticationToken)) {
       // other stuf ....
        }
    } else {
        if (request.getRequestURI().startsWith("/api")) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return false;
        }
    }
    return true;
}
}
但在本例中,请求URI已经是/login

我的配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().sessionManagement()
                .invalidSessionUrl("/login?invalid")
                .and().csrf().disable().formLogin()
                .loginPage("/login")
                .failureHandler(customAuthenticationFailureHandler)
                .defaultSuccessUrl("/loggedIn")
                .usernameParameter("email")
                .passwordParameter("password")
                .successHandler(authenticationSuccessHandler)
                .and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/").and().exceptionHandling()
                .accessDeniedPage("/access-denied")
        ;
    }

我建议不要将以前的安全配置与新的REST api配置混合使用。您可以执行以下操作:

@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Configuration
    @Order(1)
    public static class WebConfiguration extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatchers("/web")
            ...
           /* Your previous config would go here */
        }
    }

    @Configuration
    @Order(2)
    public static class ApiConfiguration extends WebSecurityConfigurerAdapter {


        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatchers("/web")

            ... /* Your /api configuration goes here */

                .exceptionHandling()
                  .authenticationEntryPoint(customAuthenticationEntryPoint)

        }

        @Bean
        AuthenticationEntryPoint customAuthenticationEntryPoint() {
            return new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED);
        }
    }
}
通过这种方式,您可以单独配置rest api。现在,您可以为rest api设置不同的身份验证入口点。事实上,您很可能还希望提供一个自定义的失败处理程序和成功处理程序,您现在可以轻松地实现这一点,它将与web应用程序的其余部分保持独立