[英]How to redirect a Single Page Application in Spring Boot?
I have a signup endpoint for a Spring Boot app. 我有一个Spring Boot应用程序的注册端点。 The UI is a React Single Page App.
UI是一个React Single Page App。
I want to make the SPA app redirect to "/login" (making a GET request). 我想使SPA应用程序重定向到“ / login”(发出GET请求)。
How should I do this? 我应该怎么做?
I tried the following two approaches: 我尝试了以下两种方法:
The first approach doesn't work as the response http code in postman is 200 and the body only includes "redirect:/login" 第一种方法不起作用,因为邮递员中的响应http代码为200,并且正文仅包含“ redirect:/ login”
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String signup(@RequestBody CustomUserDetails user, HttpServletResponse response) {
String userName = user.getUsername();
logger.debug("User signup attempt with username: " + userName);
try{
if(customUserDetailsService.exists(userName))
{
logger.debug("Duplicate username " + userName);
return "redirect:/login";
} else {
customUserDetailsService.save(user);
authenticateUserAndSetSession(user, response);
}
} catch(Exception ex) {
}
return "redirect:/login";
}
I also tried the following approach. 我还尝试了以下方法。 But it throws
Method Not Allowed
exception. 但是它会抛出“
Method Not Allowed
异常。 Because I have a controller for login
when the request is of type POST
因为当请求的类型为
POST
时,我有一个用于login
的控制器
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public ModelAndView signup(@RequestBody CustomUserDetails user, ModelMap model, HttpServletResponse response) {
String userName = user.getUsername();
logger.debug("User signup attempt with username: " + userName);
try{
if(customUserDetailsService.exists(userName))
{
logger.debug("Duplicate username " + userName);
return new ModelAndView("redirect:/login", model);
} else {
customUserDetailsService.save(user);
authenticateUserAndSetSession(user, response);
}
} catch(Exception ex) {
}
return new ModelAndView("redirect:/redirectedUrl", model);
}
How am I supposed to handle this redirect ? 我应该如何处理此重定向? What is the best practice ?
最佳做法是什么?
The best practice is you shouldn't redirect in the Spring Boot Controllers at all. 最佳实践是您根本不应该在Spring Boot Controllers中重定向。
What you need to do is return status codes from /signup
endpoint. 您需要做的是从
/signup
端点返回状态代码。
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public ResponseEntity<String> signup(@RequestBody CustomUserDetails user, HttpServletResponse response) {
String userName = user.getUsername();
logger.debug("User signup attempt with username: " + userName);
try{
if(customUserDetailsService.exists(userName))
{
logger.debug("Duplicate username " + userName);
return new ResponseEntity<String>(HttpStatus.OK);
} else {
customUserDetailsService.save(user);
authenticateUserAndSetSession(user, response);
}
} catch(Exception ex) {
}
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
So the endpoint will return HTTP status 200 when there is a User with the username in the system and HTTP status 404 when there is no User found with the given username. 因此,当系统中有一个具有用户名的用户时,端点将返回HTTP状态200,而在没有找到具有给定用户名的用户时,端点将返回HTTP状态404。 You have to use this information to make the Routing in the Front End Single Page Application (This is how it is done in AngularJS, etc.)
您必须使用此信息在前端单页应用程序中进行路由(这是在AngularJS中完成的方法,等等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.