Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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 不同的端点看起来是随机授权的_Java_Spring_Spring Security - Fatal编程技术网

Java 不同的端点看起来是随机授权的

Java 不同的端点看起来是随机授权的,java,spring,spring-security,Java,Spring,Spring Security,我正在编写一个应用程序,试图在其中配置不同端点的可见性 我编写了以下代码: @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable().authorizeRequests() .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll() .a

我正在编写一个应用程序,试图在其中配置不同端点的可见性

我编写了以下代码:

@Override
   protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
        .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
        .antMatchers(HttpMethod.POST, "/login").permitAll()
        .antMatchers(HttpMethod.GET, "/login").permitAll()
        .antMatchers(HttpMethod.GET, "/").authenticated()
        .antMatchers(HttpMethod.GET, UPVOTE_URL).authenticated()
        .antMatchers(HttpMethod.GET, DOWNVOTE_URL).authenticated()
        .antMatchers(HttpMethod.POST, LOG_OUT_URL).authenticated()
        .antMatchers(HttpMethod.DELETE, DELETE_URL).authenticated()
        .antMatchers(HttpMethod.POST, ADD_URL).authenticated()
        .anyRequest().authenticated()
        .and()
        .addFilter(new JWTAuthenticationFilter(authenticationManager()))
        .addFilter(new JWTAuthorizationFilter(authenticationManager()))      
     .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .logout()
        .and()
        .exceptionHandling()
        .authenticationEntryPoint(new Http401AuthenticationEntryPoint("No authorization"));
我的程序的行为非常奇怪,因为当我试图到达“/login”或“/”端点时,程序有时会抛出401(如果用户尚未登录,afaik应该将其重定向到登录页面)

在那之后,我重新启动了它,可能在其他地方做了一些小的改动,这些改动似乎完全不相关,我的网站又重新运行了


你们有没有遇到过这样的问题?原因是什么?我在配置中有没有做错什么?

这里有三个突出的地方

  • 您有一个自定义的入口点,其感觉是入口点发送401,而不是重定向到/login

  • 您没有
    formLogin()
    ,因此处理登录页面的筛选器不起作用

  • 我们不知道你的过滤器在什么时候做什么

  • 至于配置,让我们先开始

    http
    .cors()
    .及()
    .csrf()
    .disable()
    .授权请求()
    .antMatchers(HttpMethod.POST,SIGN\u URL).permitAll()
    .antMatchers(“/login”).permitAll()
    .antMatchers(HttpMethod.GET,“/”).authenticated()
    .antMatchers(HttpMethod.GET,UPVOTE_URL).authenticated()
    .antMatchers(HttpMethod.GET,DOWNVOTE_URL).authenticated()
    .antMatchers(HttpMethod.POST,注销URL).authenticated()
    .antMatchers(HttpMethod.DELETE,DELETE_URL).authenticated()
    .antMatchers(HttpMethod.POST,ADD_URL).authenticated()
    .anyRequest().authenticated()
    .及()
    .addFilterBefore(新的JWTAuthenticationFilter(authenticationManager()),HeaderWriterFilter.class)
    .addFilterAfter(新的JWTAuthorizationFilter(authenticationManager()),JWTAuthenticationFilter.class)
    .sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS)
    .及()
    .formLogin()
    .及()
    .logout()
    ;
    
    我们改变了什么

  • 移除身份验证入口点并将JWT筛选器移到前面。 如果这些过滤器触发(并且它们不应该为非REST端点触发,因此您必须编写该逻辑),那么系统要么经过身份验证,要么过滤器本身返回401,而不抛出异常。也许你可以让我们知道这些过滤器是否真的做了正确的事情

  • 如果JWT过滤器什么都不做,其他的一切都会起作用。因此,我们在formLogin()中添加了所有默认配置。如果由于请求在应该进行身份验证时未进行身份验证而调用身份验证入口点,则会发生重定向到/login