【问题标题】:spring boot jwt ant-mather blocking requestspring boot jwt ant-mather 阻塞请求
【发布时间】:2020-06-27 20:58:12
【问题描述】:

我正在尝试在我的 Spring Boot 应用程序中实现 JWT。我这样实现WebSecurityConfig:


    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable()
                .authorizeRequests().antMatchers("/login/*").permitAll().
                anyRequest().authenticated().and().
                exceptionHandling().and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);

    }

在控制器内部,我只有几个端点,仅用于这样的测试:


@RestController("/login")
public class LoginController {

    @PostMapping("/signup")
    private void signup(@RequestBody LoginCredential loginCredential){
        System.out.println("signup");
    }

    @PostMapping("/signin")
    private void signin(@RequestBody LoginCredential loginCredential){
        System.out.println("signin");
    }

    @GetMapping("/test")
    private String test(){
        return "test, working ?";
    }

}

但是当我在http://localhost:8080/login/signup 中发布一些数据时,它显示的是 404。

然后我编写了一个过滤器/拦截器来检查请求的结束位置。 我发现了这个:http://localhost:8080/login/signup

作者: System.out.println(request.getRequestURL().toString());

我认为我在 antMatcher 中的WebSecurityConfigurerAdapter 有问题。

我该如何解决这个问题?

【问题讨论】:

    标签: spring spring-boot jwt


    【解决方案1】:

    你得到 404 因为你的路径不正确,正确的路径是

    http://localhost:8080/signup
    

    因为根据docs '/login' 是逻辑组件名称。

    如果你想让这个 URL 工作

    http://localhost:8080/login/signup
    

    然后您必须将RequestMapping 添加到RestController,因此您的控制器应该如下所示:

    @RestController
    @RequestMapping("/login")
    public class LoginController {
     ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 1970-01-01
      • 2011-07-07
      • 2015-06-05
      • 2019-10-21
      • 2020-12-09
      • 2022-11-21
      • 2013-11-12
      • 1970-01-01
      相关资源
      最近更新 更多