【问题标题】:Redirecting control from one controller to another in spring boot在spring boot中将控制从一个控制器重定向到另一个控制器
【发布时间】:2018-02-11 05:01:27
【问题描述】:

我正在尝试将控制从一个控制器重定向到另一个控制器,但它没有发生。我经历了以前的 stackoverflow 问题,但他们的解决方案不适用于我的情况。

我的控制器是

@RestController
@RequestMapping( "/rest/auth" )
public class UnprotectedController extends AbstractController {

@Autowired
private UserServices userService;

@Autowired
private RequesterService requesterService;


@RequestMapping(value = "/login",method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response)
{
    return new ModelAndView("redirect:/login");
}
}

Postman,如果我点击http://localhost:8090/login,我会得到有效的回复。但是如果我点击http://localhost:8090/rest/auth/login,我会收到错误

{
"timestamp": 1504340884618,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/login"

}

似乎控制没有重定向到指定的redirect url。为什么会这样?

http://localhost:8090/loginspring security 验证用户凭据并创建Security Context 的路径

【问题讨论】:

  • 我就是这么做的。但它不起作用
  • 您使用的是哪个 Spring Security 版本?
  • 它发出POST 请求....那么没有黑客可以实现这一点吗?
  • 如果邮递员制作POST /login,那么我错了,它应该可以工作。显示你的 Spring Security 配置,可能有问题。

标签: spring spring-boot spring-security spring-security-oauth2


【解决方案1】:

好吧,我认为您可以通过其他方式做到这一点。通过使用这个 HttpServletResponse 你有一个参数。

response.sendRedirect("/login");

但是,它会引发两种类型的异常,因此也必须使用 try/catch。

您遇到的问题是您在控制器上使用 @RestController 注释,该注释为所有方法提供了 @ResponseBody 注释,我认为您不能将 ModelAndView 作为响应正文返回。

尝试返回一个类似的字符串

return "redirect:/login"

return "forward:/login";

【讨论】:

  • 我试过了,但它说 ` { "timestamp": 1504522158996, "status": 404, "error": "Not Found", "message": "No message available", "path ": "/login" }`
  • 检查 2 件事。当您的应用启动时,在日志中查看它显示的所有映射。你应该在那里找到你的控制器映射。如果不是,则它们不会被扫描拾取。另外,尝试路线。也许您需要添加一个绝对映射,例如“localhost:8080/login
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多