首页 > 解决方案 > 生成令牌 JWT 后 Spring Security 302 重定向

问题描述

我有一个 POST,它在请求的标头中生成 jwt 令牌,但是在邮递员发出请求的那一刻,它以 302 响应并重定向到“localhost”。

查看邮递员控制台,POST 具有令牌。

请求 302

http.authorizeRequests()
            .and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().authorizeRequests().antMatchers("/", "/css/**", "/js/**", "/images/**", "/listar/**", "/api/**").permitAll()
            .anyRequest().authenticated()
            .and().addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .httpBasic();

public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
    this.authenticationManager = authenticationManager;
    this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/api/login", "POST"));
}

在调试模式下检查,您可以看到“AbstractAuthenticationTargetUrlRequestHandler.defaultTargetUrl”的值为“/”,因此为 302。

我怎么能定义它根本不重定向?

标签: javaspring-bootspring-securityjwt

解决方案


推荐阅读