Hi,
I am trying to run an Angular + Spring Boot + PostgreSQL App in Gitpod.
The app consists of 3-tiers - the front-end Angular app, the back-end Spring Boot API, and lastly a PostgreSQL database.
The app works fine on the local host.
.gitpod.Dockerfile
FROM gitpod/workspace-postgres
#Install Angular CLI and JDK 1.8
RUN npm install -g @angular/cli@8.3.29
RUN bash -c “. /home/gitpod/.sdkman/bin/sdkman-init.sh
&& sdk install java 8.0.275.hs-adpt”
.gitpod.yml file
image:
file: .gitpod.Dockerfile
#exposed back-end API port
ports:
-port: 5555
Back-end Spring Boot API
The back-end Spring Boot API needs SSL because we store certain information in secure cookies. Therefore SSL is enabled with a self-signed certificate.
CORS is enabled and we correctly set up a CORS Filter (see below). CSRF has been disabled.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
//.ignoringAntMatchers("/auth", “/auth/logout”)
//.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(
“/auth”,
“/reporting/viewResource/",
"/register/”,
“/password/forgot”,
“/password/reset”,
“/v2/api-docs”,
“/actuator/",
“/configuration/ui”,
“/swagger-resources”,
“/configuration/security”,
"/v3/api-docs/”,
“/swagger-ui/",
“/swagger-ui.html”,
"/webjars/”,
“/swagger-resources/configuration/ui”,
“/swagger-resources/configuration/security”,
“/browser/index.html#”,
“/browser/**”
)
.permitAll()
.antMatchers(HttpMethod.POST, REGISTER)
.permitAll()
.antMatchers(HttpMethod.POST, CONFIRM)
.permitAll()
.anyRequest()
.authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager(), context, userRepository))
.addFilter(new JWTAuthorizationFilter(authenticationManager(), context));
}
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
The back-end compiles and runs fine on port 5555.
PostgreSQL Database
We are using gitpod/workspace-postgres as the image and therefore we already have a postgres database with gitpod user setup and this all works.
Front-end Angular app
We ran the front-end angular app using the following command:
ng serve --disable-host-check --host 0.0.0.0
The app UI comes up correctly, but we are unable to login to the application.
In summary
1. Back-end API CSRF is disabled
2. Back-end API SSL is enabled (we have to enable this or our app will not work)
3. The back-end is running fine on port 5555 using https
4. The front-end is running fine on port 4200 (default Angular port) and brining up with UI
5. The front-end Angular environment.ts file is as follows:*
export const environment = {
production: false,
apiUrl: ‘/api’,
};
6. The front-end proxy.config.json file is as follows:
{
“/api”: {
“target”: “http://5555-a1b757c0-92ba-4110-9fbd-d34e9e47466e.ws-us03.gitpod.io”,
“secure”: false,
“pathRewrite”: {
“^/api”: “”
},
“changeOrigin”: true
}
NOTE: We had to use http (instead of https) above because GitPod wraps http with https. If we use https, we get a different error that is as folBad Request
This combination of host and port requires TLS
Errors we get when we try to login (with the URL in proxy set to http)
Access to XMLHttpRequest at ‘https://5555-a1b757c0-92ba-4110-9fbd-d34e9e47466e.ws-us03.gitpod.io/login’ (redirected from ‘https://4200-a1b757c0-92ba-4110-9fbd-d34e9e47466e.ws-us03.gitpod.io/api/login’) from origin ‘https://4200-a1b757c0-92ba-4110-9fbd-d34e9e47466e.ws-us03.gitpod.io’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
GET https://5555-a1b757c0-92ba-4110-9fbd-d34e9e47466e.ws-us03.gitpod.io/login net::ERR_FAILED
Can you please help? Has anyone been able to successfully run a Angular/SpringBoot/DB app using GitPod?
Here is a link to the workspace snapshot: https://gitpod.io/#snapshot/73c6f655-8883-4a29-8d88-24dc7bfdbf26
Thank you.
Sunil