【问题标题】:How to redirect a Single Page Application in Spring Boot?如何在 Spring Boot 中重定向单页应用程序?
【发布时间】:2017-05-12 12:46:15
【问题描述】:

我有一个 Spring Boot 应用程序的注册端点。 UI 是一个 React 单页应用程序。

我想让 SPA 应用重定向到“/login”(发出 GET 请求)。

我应该怎么做?

我尝试了以下两种方法:

第一种方法不起作用,因为 postman 中的响应 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";
}

我也尝试了以下方法。但它会引发Method Not Allowed 异常。因为当请求类型为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);
}

我应该如何处理这个重定向?最佳做法是什么?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    最好的做法是你根本不应该在 Spring Boot 控制器中重定向。

    你需要做的是从/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);
    }
    

    因此,当系统中存在具有该用户名的用户时,端点将返回 HTTP 状态 200,当没有找到具有给定用户名的用户时,端点将返回 HTTP 状态 404。您必须使用此信息在前端单页应用程序中进行路由(这是在 AngularJS 等中完成的方式)

    【讨论】:

    • 如果我还想发送一个自定义错误代码,暗示存在现有用户名怎么办?
    • 可以发送任何状态码,只要在前端处理即可。
    • 注销怎么办?我应该如何在 SPA 中处理它?我应该停止将 JWT(身份验证令牌)放在标头中吗?
    • 是的。 SPA 应该使用 REST API,而 REST 据说是无状态的。这意味着您永远不会登录会话。只需从客户端清除 JWT Token。
    • 难道我们不需要在服务器端删除 JSESSION cookie 并使其失效吗?
    猜你喜欢
    • 2017-11-25
    • 1970-01-01
    • 2015-08-17
    • 2016-08-29
    • 2020-01-08
    • 1970-01-01
    • 2015-02-05
    • 2023-03-13
    • 2018-08-08
    相关资源
    最近更新 更多