【发布时间】:2019-06-01 19:53:17
【问题描述】:
我正在开发一个应用程序,其中 Angular 6 作为前端,spring boot 作为后端。在实现用户身份验证模块时,我想在登录后相应地重定向到学生和教职员工的家。
但是,我无法从 Spring Boot 重定向页面。使用 Redirect to an external URL from controller action in Spring MVC 这个解决方案我得到了 CORS 错误: “预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段内容类型。”
或者,如果有其他方法可以完成这项任务?
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";
}
}
}
}
【问题讨论】:
-
angular 是单页,不接受重定向到页面。因此,对于重定向到角度页面,您应该使用 rest api 发送请求,作为响应,您可以在模块之间进行路由。
标签: java angular spring-boot