1

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)

3
  • Are you using a fully qualified URL for your service call Commented Jan 22, 2018 at 11:14
  • I am assuming this an an angular CLI dev enviroment Commented Jan 22, 2018 at 11:49
  • yes it is an angular cli dev. and yes I am using fully qualified domain as hardcode string Commented Jan 22, 2018 at 12:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you.:)..its working now. Thanks for nice explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.