Open dsyer opened 10 years ago
Great! This is exactly what I was looking for.
Awesome Harleen. This may not be an oath protocol, but this should be a basic/necessary feature in spring-oauth to make oauth protocol work correctly.
User switching or parallel logins (in the same browser) are a typical use case in businesses, so please provide an option for that.
This worked wonderfully for me. When a user logs out of my ui service, they would just get logged back in automatically since their auth server session never expired. Now, when the user logs out of the ui server, they are brought to the auth server login page as expected.
Outstanding--exactly what I needed! @harleen, when my people come to enslave your planet, you will not be harmed.
Note that the aspect above will work, but it's neater to do the same thing with a HandlerInterceptor
(may not have been possible at the time the feature request was made). E.g. this should work in a AuthorizationServerConfigurerAdapter
:
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
...
endpoints.addInterceptor(new HandlerInterceptorAdapter() {
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if (modelAndView != null
&& modelAndView.getView() instanceof RedirectView) {
RedirectView redirect = (RedirectView) modelAndView.getView();
String url = redirect.getUrl();
if (url.contains("code=") || url.contains("error=")) {
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}
}
}
}
});
}
This interceptor jest fine when form login shows as result of standard process - ie. GET /oauth/authenticate which redirects to /login.html at which you log in. But when you directly enter at /login.html (manually write URL or click backward in browser) and log in then this interceptor is not even called and session stayed alive.
@tomasz-werminski that's expected though, I should think, because you aren't going to go through the OAuth2 protocol in that case. You just authenticated with the auth server, and now you could, for instance do some admin actions in there or something. It's nothing to do with OAuth2 at that point. Or am I missing something?
@dsyer Yes, you are right, /login.html page on her own is not a part of oauth2 flow. But from user's point of view it may be a bit confiusing. User starts authentication process - ie. /oauth/authenticate call happens, then redirect to /login.html, then interceptor kicks in and finally redirect to client-app happens. But if at this moment user clicks browser backward button he or she is able to see login page again. So he or she can log-in too and leave active session. In this case, next call for /oauth/authenticate won't trigger login page.
This is, of course, not a problem with this interceptor because its role is to invalidate session on oauth2 auhtentication process. But if your authorization server's only role is to authorize oauth clients above flow could be a problem (luckily it's no frequent).
I was faced with such a flow so I've decided to describe it here because it's related to this topic, maybe it'll be helpful for somebody. As a workaround in login controller I check if /login.html call is "a part of normal flow", ie. whether in session is any saved request which triggered login proces. If not I make redirect to some configured URL.
For newcomers, use the HandlerInterceptor approach instead of the Aspect's, because the last one will invalidate the session "always", even when user mistype his/her credentials. After that, a successful login will not be able to trigger the original redirection.
How this feature can be implemented with Single Sign on Kerberos/Spnego authentication. I am using pretty older version of zuul 1.1.0 . A example or pointer would be great.
Note that the aspect above will work, but it's neater to do the same thing with a
HandlerInterceptor
(may not have been possible at the time the feature request was made). E.g. this should work in aAuthorizationServerConfigurerAdapter
:@Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { ... endpoints.addInterceptor(new HandlerInterceptorAdapter() { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (modelAndView != null && modelAndView.getView() instanceof RedirectView) { RedirectView redirect = (RedirectView) modelAndView.getView(); String url = redirect.getUrl(); if (url.contains("code=") || url.contains("error=")) { HttpSession session = request.getSession(false); if (session != null) { session.invalidate(); } } } } }); }
This might be a silly question . But how do I attach this handler interceptor via xml config? my organization uses <oauth2:authorization-server... and it's either that or I migrate everything to annotations. Currently I just placed a filter at the top of the chain that intercepts the auth code response and invalidates the session. However it seems a bit hacky to me.
So what's the correct and better approach to fix this behaviour? The HandlerInterceptor or the SessionInvalidationOauth2GrantAspect?
It'd be a nice feature to only maintain the session on the authorization server as long the user is granting approval to an oauth2 client, and no longer. This is useful for when your authorization server's sole role is for authorizing oauth2 clients, and nothing else. In this case, a longer lived session has no real value.
Also, if an oauth2 client wants to switch users for oauth2 access, it can't do so easily today. As long as the user's session is alive on the authorization server what will happen is as the client sends the user to /oauth/authorize, the client will be using the user's old session, and the client will be auto approved since it has a current access token for that user.
This feature should be configurable.
Currently I've accomplished this with use of aspects:
Comments:
david_syer on Mon, 30 Dec 2013 10:57:23 +0000
harleen on Mon, 30 Dec 2013 14:52:48 +0000