Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html vs JSP - get request header token value

I am working on Java application . Front end would be Angular2 .

If I try to open my application home page( index.html is configured in web.xml as default page ) . Access URL should be http://localhost:8080/MyWebApp .

Then I have taken into an standard organization's login page for authentication. If authentication succes , HTTP Authorization token will be set in the request header and finally control comes to display my application home page.

If I use jsp, I can get request header as,

String authHeader = request.getHeader("authorization");          
out.println("<h2>HTTP Authorization header:</h2>");

if (authHeader == null) {            
     out.print("No authorization header");      
} else {            
     out.print("<textarea readonly id='authHeader' rows=\"5\" cols=\"80\">" + authHeader + "</textarea>");
}

But we are using html as front end, because of angular 2 .

So for my scenario, how I can I get the request header and token.

Please don't hesitate to edit my question, if it is not clear.

like image 626
Human Being Avatar asked Mar 08 '17 05:03

Human Being


People also ask

What happens when a browser requests a JSP page?

A browser requests a page with a . jsp file extension from WebLogic Server. WebLogic Server reads the request. Using the JSP compiler, WebLogic Server converts the JSP into a servlet class that implements the javax.

Should I send JWT token in header or body?

Yep. When making requests that needs authorization or when accessing a part in your API that needs authentication, you need to send the jwt to the server for authentication. The common and best practice is too add it to the request header as authorization header.


2 Answers

You can't get a value of a header from client-side JavaScript. The only exceptions are the User-Agent and Referrer headers, because the browser provides the values in the document and navigator objects.

You said you are working on a Java application with an Angular 2 front end and some other application provides a token (might be useful to specify if this is something standard, e.g. OAuth2). I will assume that it is a custom token. I believe you also meant you have some server side component, a servlet.

What you can do is to implement the authentication using the servlets (or even JSPs) and then redirect back to the Angular 2 front end application, passing the token in the URL as a query parameter. URL is easy to read in Angular 2. However this is not very secure, even if you use something like JWT. As an alternative to URL, you can use the Set-Cookie header and then read the cookie from Angular.

What would be almost secure would be to authenticate the user using the server side (servlet or even JSP). Then create a one-time token which is passed in the URL as a query parameter when redirecting to your HTML page. Then use the one-time token in a call to the server again to retrieve the real authentication token using a proper REST call from Angular 2 with request and response.

Depends on how much control you have and what kind of authentication the auth application uses, you might want to take a look at the OAuth2. It deals with plenty of different authentication scenarios. Specifically the OAuth2 implicit grant flow is used to authenticate users from client-side only applications. Even if you can't use that, it will give you some ideas.

like image 162
MartinTeeVarga Avatar answered Oct 13 '22 14:10

MartinTeeVarga


When you are using a server-side authorization, your server put headers with authorization to your HTML pages. But also you can put this tokens to your page response by meta tags at server side. And then access to meta tags by js.

<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>

Meta tags are similar to response headers and can complete or override response headers. Read this post please Spring Security CSRF Token not working with AJAX call & form submit in same JSP

like image 1
Alex Nikulin Avatar answered Oct 13 '22 12:10

Alex Nikulin