简体   繁体   English

Angular 2,Spring boot,spring security,登录表单

[英]Angular 2, Spring boot , spring security , login form

I have a Front end application that runs on http:// localhost:4200 , with angular 2 (I've used angular-cli for generating the project). 我有一个在http:// localhost:4200上运行的前端应用程序, 角度为2 (我使用angular-cli生成项目)。
The application runs without any problems (I can get and post info from/to database using Angular 2 Http service: 应用程序运行没有任何问题(我可以使用Angular 2 Http服务从/向数据库获取和发布信息:

getList () {
    return this._http.get("http:// localhost:8080/").map(res => res.json());
}

On the other hand, I have a Back end application running with spring boot on http:// localhost:8080/ (note the ports), that provide a REST Api for my angular 2 application. 另一方面,我有一个在http:// localhost:8080/ (注意端口)上使用spring boot运行的后端应用程序,它为我的angular 2应用程序提供REST Api。

Sending requests from http://localhost:4200/ to http://localhost:8080/ works as expected. http://localhost:4200/发送到http://localhost:8080/请求按预期工作。

Before I've programmed an application with spring framework & angularJS 1 , spring security took care of login and security in the same app (maven project). 在我使用spring framework和angularJS 1编写应用程序之前, spring security在同一个应用程序(maven项目)中负责登录和安全性。

Now I have two separate applications that are communicating via http (RESTful Api with spring boot , and front end Api with angular 2) 现在我有两个独立的应用程序通过http进行通信(RESTful Api与弹簧启动,前端Api与角度2)

How can I set my login form and where to put it, and how to configure spring security with my angular 2 application? 如何设置我的登录表单以及放置它的位置,以及如何使用angular 2应用程序配置弹簧安全性?

When you spin up the Angular Application with Angular CLI, your Angular Pages are served by NodeJs in the backend. 当您使用Angular CLI启动Angular应用程序时,Angular页面由后端的NodeJ提供服务。 You have a couple of options here. 你有几个选择。

  1. The login can be handled at NodeJS server which can in turn invoke Spring Boot application to Authenticate and Authorize.(I think, you might need to do some tweaks like using express server instead of lite server. take a look here https://stackoverflow.com/a/37561973/6785908 ) 登录可以在NodeJS服务器上处理,NodeJS服务器又可以调用Spring Boot应用程序进行身份验证和授权。(我想,你可能需要做一些调整,比如使用快速服务器而不是精简服务器。看看这里https:// stackoverflow .com / a / 37561973/6785908

  2. After initial development, you can copy over your AngularJS resources to a Spring MVC (Spring Boot) server and use it to serve your pages (as well as the rest APIs), just as you were doing before. 在初始开发之后,您可以将AngularJS资源复制到Spring MVC(Spring Boot)服务器,并使用它来为您的页面(以及其他API)提供服务,就像您以前一样。

  3. Make the Angular App invoke the spring boot Service (I guess , you are using the $http.post to submit the form, if that's the case you can just change the URL so that it can hit spring boot app instead.) For the production deployment, use nginx/httpd to serve the static files (AngularJS/CSS etc) and route/proxy all the dynamic request to the spring boot app. 让Angular App调用spring boot服务(我想,你使用$ http.post提交表单,如果是这样的话,你可以改变URL,这样它就可以命中spring boot app。)对于生产部署,使用nginx / httpd来提供静态文件(AngularJS / CSS等)并将所有动态请求路由/代理到spring boot app。

I would strongly suggest the 3rd option. 我强烈建议第3种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM