我的应用程序由2个模块组成:Angular 5 uimodule
和Spring Boot REST servermodule
。我使用frontend-maven-plugin
构建uimodule
并将其资源文件复制到META-INF/resources
文件夹以帮助Spring Boot发现它们。因此,2个模块在同一个域(http://localhost:8080
)中运行。
现在我正在努力实施这个工作流程的第3点:
1-A用户在应用程序中注册(uimodule
将用户的数据发布到服务器)
2 - 收到数据后,servermodule
会向用户发送验证邮件,并附带验证链接。
3 - 当用户点击链接时,服务器会验证用户,并且应将他重定向到登录页面。
/login
中已存在uimodule
路由。现在我的问题是:
如何将用户从服务器端重定向到登录路由?
我试过了,但没有工作:
@Controller
@RequestMapping( "/activate" )
public class ActivationController {
@GetMapping
public String activate() {
return "redirect:/login";
}
}
当我点击定义的登录按钮(http://localhost:8080/login
)时,相同的路径(index.html
)工作但是当它来自服务器或我手动编写时不起作用(我得到了) 404错误)
帮助!
答案 0 :(得分:0)
服务器不知道该url,你必须将它重定向到你的角度主页面,你需要一个映射网址的控制器
@RequestMapping(value = "/login")
public String redirect() {
return "index";
}
其中index是您的角度页面
答案 1 :(得分:0)
我具有使用相同设置实现的相同功能(即Angular 8和Spring Boot作为具有gradle构建的单个应用程序),唯一的区别是我使用的是webflux模块。 经过研究,我发现我需要一个UrlBasedViewResolver实现来处理转发或重定向。因此,我包括了用于配置ViewResolver的“ org.springframework.boot:spring-boot-starter-thymeleaf”模块,然后就可以了。
与您一样,在发送邮件并在用户单击链接以验证电子邮件之后,该邮件被重定向到我想要的页面。下面是一个小代码段。
@GetMapping(value = "/verify/{email}")
public Mono<String> verify(@PathVariable String email)
{
log.info("Verifying email {}", email);
Subscribe subscribe = new Subscribe();
subscribe.setEmail(email);
subscribe.setVerified(false);
Example<Subscribe> es = Example.of(subscribe);
return this.subscribeRepository.findOne(es)
.doOnSuccess(sub ->
{
log.info("Setting verified to true");
sub.setVerified(true);
this.subscribeRepository.save(sub)
.subscribe();
})
.then(Mono.just("redirect:/verified"))
.onErrorResume(e -> Mono.just("redirect:/error"));
我已经有了用于验证和错误的Angular路线。希望这会有所帮助。