【问题标题】:Spring RestController endpoints all redirected to '/' root routeSpring RestController 端点全部重定向到 '/' 根路由
【发布时间】:2018-06-27 02:03:07
【问题描述】:

我有一个看起来像这样的休息端点:

@RequestMapping(path = "api/profile/majors", method = RequestMethod.GET)
public String getMajors() {
  //return profileDAO.getMajors();
  return "abc";
}

由于某种原因,每次我去浏览器访问:http://localhost:8080/api/profile/majors 似乎请求总是被重定向到http://localhost:8080

当我在return "abc" 处设置断点时,程序确实停在那里,但发送回浏览器的响应仍然是http://localhost:8080/ 处的任何内容

知道为什么吗??

(更多背景知识:我确实运行了 Spring Security 过滤器,我确实调用了filterChain.doFilter(request, response);,但这个问题仍然存在..)

任何提示都会有所帮助!

我的安全配置是这样的:

http.cors()
                .and()
                .csrf()
                .disable()
                .authorizeRequests()
                .antMatchers(Endpoints.PROTECTED_API_PATTERN)
                .authenticated()
                .and()
                .addFilterBefore(
                        new JWTLoginFilter(loginRequestMatcher, authenticationManager(), successHandler),
                        UsernamePasswordAuthenticationFilter.class
                )
                .addFilterBefore(
                        new JWTAPIFilter(protectedAPIMatcher, authenticationManager(), cookieService),
                        UsernamePasswordAuthenticationFilter.class
                );

【问题讨论】:

  • 访问http://localhost:8080/api/profile/majors时得到的响应码是什么?
  • 找到了 302
  • 请求 URL:localhost:8080/api/profile/majors 请求方法:GET 状态码:302 找到远程地址:[::1]:8080 推荐人策略:no-referrer-when-downgrade
  • 我记错了。但这可能是在登录成功后,您被定向到此页面。也许也可以在这里分享 spring 安全配置。
  • 我有一个扩展AbstractAuthenticationProcessingFilter 的自定义过滤器,我将"/api/**" 传递给了它的构造函数defaultFilterProcessesUrl。如果我将其设置为任何其他值,例如 "/" 一切都会正常工作......有什么想法吗?

标签: java spring spring-mvc spring-boot


【解决方案1】:

尝试在路径的开头使用斜杠。你的代码应该是这样的

@RequestMapping(path = "/api/profile/majors", method = RequestMethod.GET)

希望有帮助!

【讨论】:

  • 我的问题似乎出在AbstractAuthenticationProcessingFilter defaultFilterProcessesUrl ... 请参阅上面的 cmets.. 不知道为什么会发生这种情况..
【解决方案2】:

您可以尝试在方法 getMajors 上方添加@ResponseBody。

【讨论】:

  • 我的问题似乎出在AbstractAuthenticationProcessingFilter defaultFilterProcessesUrl ... 请参阅上面的 cmets.. 不知道为什么会发生这种情况
猜你喜欢
  • 2019-05-11
  • 1970-01-01
  • 2018-10-22
  • 2013-08-10
  • 2015-05-19
  • 1970-01-01
  • 2020-06-28
  • 2015-06-23
相关资源
最近更新 更多