Java 如何从spring boot重定向/转发角度页面?

Java 如何从spring boot重定向/转发角度页面?,java,angular,spring-boot,Java,Angular,Spring Boot,我正在开发一个应用程序,使用angular 6作为前端,spring boot作为后端。在这篇文章中,在实现用户身份验证模块时,我想在相应地登录后重定向到学生和教职员工主页 但是,我无法从spring boot重定向页面。使用此解决方案,我得到了CORS错误: “飞行前响应中的访问控制允许标头不允许请求标头字段内容类型。” 或者,如果有其他方法来完成此任务 AuthenticationController.java package sgsits.cse.dis.user.controller;

我正在开发一个应用程序,使用angular 6作为前端,spring boot作为后端。在这篇文章中,在实现用户身份验证模块时,我想在相应地登录后重定向到学生和教职员工主页

但是,我无法从spring boot重定向页面。使用此解决方案,我得到了CORS错误: “飞行前响应中的访问控制允许标头不允许请求标头字段内容类型。”

或者,如果有其他方法来完成此任务

AuthenticationController.java

package sgsits.cse.dis.user.controller;

@CrossOrigin(origins = "*") // enables cross origin request
@RestController
@RequestMapping(value = "/dis")
@Api(value = "Authentication Resource")
public class AuthenticationController {

@Autowired
StudentRepository studRepo;
@Autowired
StaffRepository staffRepo;
@Autowired
EmailController email;

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

@ApiOperation(value = "login", response = Object.class, httpMethod = "POST", produces = "application/json")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestBody Authentication auth, HttpServletResponse httpServletResponse) {
    Optional<StaffProfile> staff = staffRepo.findByEmail(auth.getUsername());
    if (staff.isPresent()) {
        String md5_pass = MD5.getHash(auth.getPassword());
        if (staff.get().getPassword().equals(md5_pass)) {
            // set session
            // update last login

            return "forward:/localhost:4200/staff";
        } else

            return "You have entered incorrect password";
    } else {
        Optional<StudentProfile> student = studRepo.findByEnrollmentId(auth.getUsername());
        if (student.isPresent()) {
            String md5_pass = MD5.getHash(auth.getPassword());
            if (student.get().getPassword().equals(md5_pass)) {
                // set session
                // update last login

              httpServletResponse.setHeader("Location", "http://localhost:4200/reset-password");
              httpServletResponse.setStatus(302);
               return "redirect:localhost:4200/student";

            } else
                return "You have entered incorrect password";
        } else {
            return "You are not registered with the system. Please signup first";
        }
    }
  }
}
包sgsits.cse.dis.user.controller;
@交叉源(origins=“*”/)启用交叉源请求
@RestController
@请求映射(value=“/dis”)
@Api(value=“身份验证资源”)
公共类身份验证控制器{
@自动连线
研究性研究报告;
@自动连线
staffRepo;
@自动连线
电子邮件控制器电子邮件;
字符串模式=“MM dd yyyy”;
SimpleDataFormat SimpleDataFormat=新的SimpleDataFormat(模式);
@ApiOperation(value=“login”,response=Object.class,httpMethod=“POST”,products=“application/json”)
@RequestMapping(value=“/login”,method=RequestMethod.POST)
公共字符串登录(@RequestBody Authentication auth,HttpServletResponse-HttpServletResponse){
可选staff=staffRepo.findByEmail(auth.getUsername());
if(staff.isPresent()){
字符串md5_pass=md5.getHash(auth.getPassword());
if(staff.get().getPassword().equals(md5_pass)){
//设置会话
//更新上次登录
返回“转发:/localhost:4200/staff”;
}否则
返回“您输入的密码不正确”;
}否则{
可选student=studRepo.findByEnrollmentId(auth.getUsername());
if(student.isPresent()){
字符串md5_pass=md5.getHash(auth.getPassword());
如果(student.get().getPassword().equals(md5_pass)){
//设置会话
//更新上次登录
httpServletResponse.setHeader(“位置”http://localhost:4200/reset-密码);
httpServletResponse.setStatus(302);
返回“重定向:本地主机:4200/学生”;
}否则
返回“您输入的密码不正确”;
}否则{
return“您未在系统注册,请先注册”;
}
}
}
}

不要向控制器添加
@CrossOrigin(origins=“*”)
注释

假设api在8080上运行,angular代码在4200上运行。如果是真的

创建一个名为proxy.conf.json的文件

{
"/api/": {
    "target": "http://localhost:8080/",
    "secure": false,
    "changeOrigin": true
}
使用此命令启动angular应用程序

ng serve—代理配置proxy.conf.json

{
"/api/": {
    "target": "http://localhost:8080/",
    "secure": false,
    "changeOrigin": true
}

使用此配置,当您调用localhost:4200/api时,它将调用8080,并且不会出现任何CORS错误。为什么不返回正确的api响应和代码,并根据您的响应和代码,可以在前端重定向


它使您的API完全RESTFUL。我希望这会有所帮助。

我注意到,当您从Spring引导重定向到角度页面时,错误“飞行前响应中的访问控制允许标头不允许请求标头字段内容类型”意味着,Angular node.js服务器在Angular应用程序配置中的标题访问控制允许标题中未检测到字段内容类型。这里的要点是,在这个重定向中,后端服务器和客户端应用程序的角色有点相反

为了解决这个问题,我将通过以下方式向响应标头Access Control Allow标头中添加content type字段:

1. create file proxy.config.js just below the package.json file in your Angular project with this code:

module.exports = {
    "/": {
       "secure": false,
            "bypass": (req, res, proxyOptions) => {
             res.setHeader('Access-Control-Allow-Headers', "Content-Type");
         }
    }
};  
并通过添加以下行修改angular.json文件:

"proxyConfig": "proxy.config.js" 
到此位置:

projects >> client >> architect >> serve >> options 

这个解决方案在我的情况下奏效了。请注意,字段名第一个字母的大小写很重要。

嘿,我删除了@crossorigin并添加了proxy.conf.json,但我仍然收到CORS错误:CORS策略已阻止从源站“”访问“”处的XMLHttpRequest:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许源站”标头。当Angular 6作为前端工作时,必须使用Rest API。从邮件中,它将直接调用RESTAPI,而该API将不会在邮件中订阅