【问题标题】:Deploying a SpringBoot - Angular application to digitalocean将 Spring Boot - Angular 应用程序部署到 digitalocean
【发布时间】:2018-10-31 13:08:54
【问题描述】:

我有一个网站在我的本地机器上运行,我想将此应用程序部署到 DigitalOcean。我创建了一个液滴,安装了必要的东西。当我输入服务器的 IP 地址时,我的 Angular 应用程序启动:

http://138.68.76.62

我还可以在 tomcat 上运行我的 Spring Boot 应用程序。但是这两个不同步运行。当我在本地机器上运行 spring 应用程序时,远程 Angular 应用程序连接它并运行良好。但我想用远程 spring 应用程序运行它。

@RequestMapping("/token")
public Map<String,String> token (HttpSession session, HttpServletRequest request){
    System.out.println(request.getRemoteHost());

    String remoteHost= request.getRemoteHost();
    int portNumber = request.getRemotePort();

    System.out.println(remoteHost+":"+portNumber);
    System.out.println(request.getRemoteAddr());

    return Collections.singletonMap("token",session.getId());
}

这是登录的请求映射。

@Injectable()
export class LoginService {

  constructor(private http: Http) { }

  sendCredential(username: string, password: string) {
    let url = "http://localhost:8181/token";
    let encodedCredentials = btoa(username+":"+password);
    let basicHeader = "Basic "+encodedCredentials;
    let headers = new Headers ({
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Authorization' : basicHeader
    });

    return this.http.get(url, {headers: headers});

 }

这是 Angular 应用中的登录功能。

我应该将http://localhost:8181/token 更改为http://138.68.76.62:8181/token 之类的吗?

【问题讨论】:

    标签: angular spring-boot tomcat digital-ocean


    【解决方案1】:

    只需将其更改为/

    @Injectable()
    export class LoginService {
    
      constructor(private http: Http) { }
    
      sendCredential(username: string, password: string) {
        let url = "/token";
        let encodedCredentials = btoa(username+":"+password);
        let basicHeader = "Basic "+encodedCredentials;
        let headers = new Headers ({
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Authorization' : basicHeader
        });
    
        return this.http.get(url, {headers: headers});
    
     }
    

    【讨论】:

    • 我按照你说的编辑了代码,但没有任何变化。服务器没有响应,但在本地服务器运行时运行良好。
    • 你的 Angular 应用程序在 Spring Boot 应用程序的静态文件夹中吗?
    • 不,不是。我与 spring 应用程序分开运行带有“ng serve”的角度应用程序。我将 ng build 生成的 dist 文件夹上传到 /var/www/html 文件夹
    • 嗯,好的。所以你必须在 url 中使用 localhost 。并验证您的服务器日志和浏览器控制台。可能是您遇到了 CORS 问题。
    • 我将应用程序的war文件部署到服务器上的tomcat。它运行但我如何向它提出请求? 138.68.76.62:8080/bookstore-0.0.1-SNAPSHOT 这是应用程序链接,但在 application.properties 文件中的端口号是 8181。我不明白我应该使用默认的 tomcat 端口 8080 还是可以在不同的端口上运行应用程序?
    【解决方案2】:

    使用相对路径和“/”

    例子:

    绝对路径:

     let url = "http://localhost:8181/token";
    

    相对路径:

    let url = "/token";
    

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2021-07-25
      • 2015-01-25
      • 2014-05-03
      • 2016-01-16
      • 2016-07-22
      • 2017-09-30
      • 2018-06-21
      • 2016-06-13
      相关资源
      最近更新 更多