I am trying various Java Spring based security implementations as follows
1. JWT Authentication
- User access /
- Springboot identifies as protected resource and redirects user to /login
- User enters credentials and browsers does a POST to /authenticate
- Server validates the credentials and generates JWT token. Set into response header and redirects to /
- Browser loads /. AngularJS recognizes JWT token in the response header and stores the same in the localStorage
- All subsequent calls will have the Bearer token in header (injected through httpInterceptor)
Note: Stateless Session
2. OAuth2 authentication
- User access /
- Springboot identifies as protected resource and redirects user to /login
- /login is intercepted by Spring security. Redirects to Oauth2 authorization server with a generated state and redirect URL back to application
- User enters credentials
- Oauth server redirects back to application URL "/login?code=xxx&state=yyy"
- /login is intercepted by Spring security. Recognizes the code and state, generates Cookie and sets in response header. Redirects to /
- Browser loads /. Browser recognizes cookie in the response header and stores the same.
- If a call is made to /user, the Principal object is populated with the JWT which I am able to extract as follows
@RequestMapping(value= {"/user")
public ResponseEntity<Map<String, String>> user(Principal principal) throws Exception {
OAuth2Authentication obj = (OAuth2Authentication) principal;
authentication = obj.getUserAuthentication();
OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails) obj.getDetails();
String jwt = oAuth2AuthenticationDetails.getTokenValue();
- All subsequent calls will have the Cookie in the Request
Note: A Stateful Session is created in server side to store the session details. This required to decrypt the cookie and identify the user
Now I want to implement security using Oauth2+JWT but stateless at same time as follows
3. OAuth2 + JWT + Stateless
- User access /
- Springboot identifies as protected resource and redirects user to /login
- /login is interecepted by Spring security. Redirects to Oauth2 authorization server with a generated state and redirect URL back to application
- User enters credentials
- Oauth server redirects back to application URL "/login?code=xxx&state=yyy"
- /login is intercepted by Spring security. Recognizes the code and state, extract JWT token by invoking OAuth2AuthenticationDetails.getTokenValue() and set in response header. Redirect to /
- Browser loads /. AngularJS recognizes JWT token in the response header and stores the same in the localStorage
- All subsequent calls will have the Bearer token in header (injected through httpInterceptor)
Question
I am trying to figure out how to implement the highlighted step above