Java 从spring boot重定向角度页面时出现CORS错误:飞行前访问控制允许标头不允许标头字段内容类型

Java 从spring boot重定向角度页面时出现CORS错误:飞行前访问控制允许标头不允许标头字段内容类型,java,spring-boot,cors,preflight,Java,Spring Boot,Cors,Preflight,我正在开发一个应用程序,使用angular 6作为前端,spring boot作为后端。目前我正在使用忘记密码功能: 从angular获取电子邮件id并向id发送电子邮件 这是网络上的请求头我在任何地方都没有看到@CrossOrigin注释,而且您没有显示网络请求中的HTTP头。需要注意的是,传递HttpServletRequest几乎总是一种迹象,表明您遇到的麻烦比必要的多。在这种情况下,改为传递UriComponentsBuilder;您可以使用MvcUriComponentsBuilder

我正在开发一个应用程序,使用angular 6作为前端,spring boot作为后端。目前我正在使用忘记密码功能:

  • 从angular获取电子邮件id并向id发送电子邮件

  • 这是网络上的请求头

    我在任何地方都没有看到
    @CrossOrigin
    注释,而且您没有显示网络请求中的HTTP头。需要注意的是,传递
    HttpServletRequest
    几乎总是一种迹象,表明您遇到的麻烦比必要的多。在这种情况下,改为传递
    UriComponentsBuilder
    ;您可以使用
    MvcUriComponentsBuilder.relativeTo
    以更干净的方式创建您想要的链接。@chrylis,嘿,我已经用
    @CrossOrigin
    更新了我的问题和请求头,请看一下,另外,我正在
    forgotPassword
    方法中传递
    HttpServletRequest
    ,这很好。此外,在
    setNewPassword
    method else部分工作正常,完全重定向到忘记密码页面,因此,我很困惑,如果同一个部分中的某个部分出现了什么问题,为什么它在请求标头origin中提供null您的CORS配置需要更新,以便服务器发回一个Access Control Allow Headers响应标头,该标头的值中包含“Content Type”。我在任何地方都没有看到
    @CrossOrigin
    注释,并且您没有显示网络请求中的HTTP标头。需要注意的是,传递
    HttpServletRequest
    几乎总是一种迹象,表明您遇到的麻烦比必要的多。在这种情况下,改为传递
    UriComponentsBuilder
    ;您可以使用
    MvcUriComponentsBuilder.relativeTo
    以更干净的方式创建您想要的链接。@chrylis,嘿,我已经用
    @CrossOrigin
    更新了我的问题和请求头,请看一下,另外,我正在
    forgotPassword
    方法中传递
    HttpServletRequest
    ,这很好。此外,在
    setNewPassword
    method else部分工作正常,完全重定向到忘记密码页面,因此,我很困惑,如果同一个部分中的某个部分出现了什么问题,为什么它在原始请求标头中提供null您的CORS配置需要更新,以便服务器发回一个Access Control Allow Headers响应标头,该标头的值中包含“Content Type”。
    @RequestMapping(value = "/forgotPassword", method = RequestMethod.POST)
    public ResponseEntity<?> forgotPassword(@RequestParam("email") String recepientemail, HttpServletRequest request) {
        Optional<User> userlist = userRepository.findByEmail(recepientemail);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (userlist.isPresent()) {
            User u = userlist.get();
            u.setResetToken(UUID.randomUUID().toString());       
               u.setResetTokenExpiry(simpleDateFormat.format(DateUtils.addDays(new Date(), 1)));
                u.setModifiedBy(u.getId());
                u.setModifiedDate(simpleDateFormat.format(new Date()));
                userRepository.save(u);
                String appUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort();
    
                // Email message
                email.sendSimpleEmail(u.getEmail(), "DIS Password Reset Request",
                        "To reset your password, click the link below:\n" + appUrl + "/dis/resetPassword?resetToken="
                                + u.getResetToken());
                return new ResponseEntity<>(
                        new ResponseMessage("A password reset link has been sent to registered email address!"),
                        HttpStatus.OK);
            }
            return new ResponseEntity<>(new ResponseMessage("We didn't find an account for this e-mail address!"),
                    HttpStatus.BAD_REQUEST);
        }
    
    @RequestMapping(value = "/resetPassword", method = RequestMethod.GET)
        public ModelAndView displayResetPasswordPage(@RequestParam("resetToken") String token) throws ParseException {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Optional<User> user = userRepository.findUserByResetToken(token);
            ModelAndView modelAndView = new ModelAndView();
            if (user.isPresent()) { // Token found in DB
                if (simpleDateFormat.parse(user.get().getResetTokenExpiry()).after(new Date())) {
                    modelAndView.addObject("resetToken", token);
                    modelAndView.setViewName("redirect:http://localhost:4200/reset-password");
                } else {
                    modelAndView.addObject("errorMessage", "Oops!  Your password reset link has expired.");
                    modelAndView.setViewName("redirect:http://localhost:4200/forgot-password");
                }
            } else { // Token not found in DB
                modelAndView.addObject("errorMessage", "Oops!  This is an invalid password reset link.");
                modelAndView.setViewName("redirect:http://localhost:4200/forgot-password");
            }
            return modelAndView;
        }
    
    @RequestMapping(value = "/processResetPassword", method = RequestMethod.POST)
        public ModelAndView setNewPassword(@RequestBody Map<String, String> requestParams, RedirectAttributes redir, HttpServletResponse response) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Optional<User> user = userRepository.findUserByResetToken(requestParams.get("resetToken"));
            ModelAndView modelAndView = new ModelAndView();
            if (user.isPresent()) {
                User resetUser = user.get();
                resetUser.setPassword(encoder.encode(requestParams.get("password")));
                resetUser.setResetToken(null);
                resetUser.setResetTokenExpiry(null);
                resetUser.setModifiedBy(resetUser.getId());
                resetUser.setModifiedDate(simpleDateFormat.format(new Date()));
                userRepository.save(resetUser);
                //redir.addFlashAttribute("successMessage", "You have successfully reset your password. You may now login.");
                //modelAndView.addObject("successMessage", "You have successfully reset your password. You may now login.");
               modelAndView.setViewName("redirect:http://localhost:4200");
                return modelAndView;
            } else {
                modelAndView.addObject("errorMessage", "Oops!  This is an invalid password reset link.");
                modelAndView.setViewName("redirect:http://localhost:4200/forgot-password");
            }
            return modelAndView;
        }
    
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RestController
    @RequestMapping("/dis")
    public class AuthRestAPIs {....}