Issue
I am developing a simple java ee application with front end in angular 4.
with login(url = '/login'), i am setting user in http session like below :
SessionUser user = userService.authenticateUser(email,password);
request.getSession().setAttribute('SESSIONUSER', user);
Also, I have written an Interceptor, which is intercepting each request (except login) and allow only if user is logged in i.e i am checking below :
if(null != request.getsession().getAttribute("SESSIONUSER"))
return true;
else
return false;
now after login success my dashboard is loading, on Loading of my dashboard i am making an another call to server to get some dashboard setting (url = '/dash-setting')
but, in my interceptor i am getting session as null , Hence interceptor not allowing to pass my next requests.
(note - i am getting JSESSIONID in response of login but i am not using it in any request, I hope this is not the issue? if yes then please let me know how to add this in next requests)
Solution
Currently, both URLs have their own individual sessions. What you essentially need is a "backend to backend” communication. You can achieve this by using a dev-server proxy. wiki
A dev-server proxy is a piece of software which is in between your JavaScript/Angular app doing the Ajax request and your backend API.
So, Assuming Instead of doing this
this.http.get('http://you-server-hostame:3604/service/vesion/data')
.map(res => res.json());
Use
this.http.get('/service/vesion/data')
.map(res => res.json());
Create a proxy.conf.json file at the root of your angular CLI project.
{
"/service/*": {
"target": "http://you-server-hostame:3604",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
All requests made to /service/... from within our application will be forwarded to http://you-server-hostame:3604/service/....
Also now for starting the server you need to use
ng serve --proxy-config proxy.config.json
Note :the changeOrigin property. You will definitely have to set this to true when you’re using some virtual proxies (such as configured with Apache2) on your backend.
What it does is to simply take the browser request at the same domain+port where you frontend application runs and then forwards that request to your backend API server.
Disclaimer: Not recommended for Production
Answered By - Count