Skip to content

Commit f0b0247

Browse files
committed
Fix #30648: redirect to app after login
1 parent ecb40a3 commit f0b0247

6 files changed

Lines changed: 54 additions & 28 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/auth/impl/KeycloakAuthenticationBackend.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.springframework.security.core.GrantedAuthority;
5555
import org.springframework.security.core.session.SessionRegistryImpl;
5656
import org.springframework.security.web.AuthenticationEntryPoint;
57-
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
57+
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
5858
import org.springframework.security.web.authentication.logout.LogoutFilter;
5959
import org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy;
6060
import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
@@ -77,8 +77,6 @@
7777
import java.util.Map;
7878
import java.util.stream.Collectors;
7979

80-
import static eu.openanalytics.containerproxy.ui.AuthController.AUTH_SUCCESS_URL;
81-
8280
@Component
8381
public class KeycloakAuthenticationBackend implements IAuthenticationBackend {
8482

@@ -94,6 +92,10 @@ public class KeycloakAuthenticationBackend implements IAuthenticationBackend {
9492
@Lazy
9593
AuthenticationManager authenticationManager;
9694

95+
@Inject
96+
@Lazy
97+
private SavedRequestAwareAuthenticationSuccessHandler successHandler;
98+
9799
@Override
98100
public String getName() {
99101
return NAME;
@@ -144,9 +146,7 @@ protected KeycloakAuthenticationProcessingFilter keycloakAuthenticationProcessin
144146
KeycloakAuthenticationProcessingFilter filter = new KeycloakAuthenticationProcessingFilter(authenticationManager, requestMatcher);
145147
filter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy());
146148
filter.setAuthenticationFailureHandler(keycloakAuthenticationFailureHandler());
147-
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler(AUTH_SUCCESS_URL);
148-
handler.setAlwaysUseDefaultTargetUrl(true);
149-
filter.setAuthenticationSuccessHandler(handler);
149+
filter.setAuthenticationSuccessHandler(successHandler);
150150
// Fix: call afterPropertiesSet manually, because Spring doesn't invoke it for some reason.
151151
filter.setApplicationContext(ctx);
152152
filter.afterPropertiesSet();

src/main/java/eu/openanalytics/containerproxy/auth/impl/OpenIDAuthenticationBackend.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.logging.log4j.LogManager;
3232
import org.apache.logging.log4j.Logger;
3333
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.context.annotation.Lazy;
3435
import org.springframework.core.env.Environment;
3536
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3637
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -58,6 +59,7 @@
5859
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
5960
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
6061
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
62+
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
6163
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
6264
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
6365
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
@@ -77,7 +79,6 @@
7779
import java.util.stream.Collectors;
7880

7981
import static eu.openanalytics.containerproxy.auth.impl.oidc.OpenIDConfiguration.REG_ID;
80-
import static eu.openanalytics.containerproxy.ui.AuthController.AUTH_SUCCESS_URL;
8182

8283
public class OpenIDAuthenticationBackend implements IAuthenticationBackend {
8384

@@ -93,6 +94,10 @@ public class OpenIDAuthenticationBackend implements IAuthenticationBackend {
9394
@Inject
9495
private ClientRegistrationRepository clientRegistrationRepo;
9596

97+
@Inject
98+
@Lazy
99+
private SavedRequestAwareAuthenticationSuccessHandler successHandler;
100+
96101
private static OAuth2AuthorizedClientService oAuth2AuthorizedClientService;
97102

98103
@Autowired
@@ -116,11 +121,11 @@ public boolean hasAuthorization() {
116121
@Override
117122
public void configureHttpSecurity(HttpSecurity http, AuthorizedUrl anyRequestConfigurer) throws Exception {
118123
anyRequestConfigurer.authenticated();
119-
124+
120125
http
121126
.oauth2Login()
122127
.loginPage("/login")
123-
.defaultSuccessUrl(AUTH_SUCCESS_URL, true)
128+
.successHandler(successHandler)
124129
.clientRegistrationRepository(clientRegistrationRepo)
125130
.authorizedClientService(oAuth2AuthorizedClientService)
126131
.authorizationEndpoint()
@@ -141,7 +146,7 @@ public void onAuthenticationFailure(HttpServletRequest request, HttpServletRespo
141146
.oidcUserService(createOidcUserService())
142147
.and()
143148
.and()
144-
.addFilterAfter(openIdReAuthorizeFilter, UsernamePasswordAuthenticationFilter.class);
149+
.addFilterAfter(openIdReAuthorizeFilter, UsernamePasswordAuthenticationFilter.class);
145150
}
146151

147152
private OAuth2AuthorizationRequestResolver authorizationRequestResolver() {

src/main/java/eu/openanalytics/containerproxy/auth/impl/SAMLAuthenticationBackend.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
29+
import org.springframework.context.annotation.Lazy;
2930
import org.springframework.core.env.Environment;
3031
import org.springframework.security.authentication.ProviderManager;
3132
import org.springframework.security.config.annotation.ObjectPostProcessor;
@@ -40,6 +41,7 @@
4041
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
4142
import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter;
4243
import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver;
44+
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
4345
import org.springframework.security.web.authentication.logout.LogoutFilter;
4446
import org.springframework.security.web.util.matcher.AndRequestMatcher;
4547
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@@ -76,6 +78,10 @@ public class SAMLAuthenticationBackend implements IAuthenticationBackend {
7678
@Inject
7779
private Saml2LogoutRequestResolver saml2LogoutRequestResolver;
7880

81+
@Inject
82+
@Lazy
83+
private SavedRequestAwareAuthenticationSuccessHandler successHandler;
84+
7985
@Override
8086
public String getName() {
8187
return NAME;
@@ -99,7 +105,7 @@ public void configureHttpSecurity(HttpSecurity http, AuthorizedUrl anyRequestCon
99105
.loginProcessingUrl(SAML_SERVICE_LOCATION_PATH)
100106
.authenticationManager(new ProviderManager(samlAuthenticationProvider))
101107
.failureHandler(failureHandler)
102-
.defaultSuccessUrl(AUTH_SUCCESS_URL, true))
108+
.successHandler(successHandler))
103109
.saml2Logout(saml -> saml
104110
.logoutUrl(SAML_LOGOUT_SERVICE_LOCATION_PATH)
105111
.logoutResponse(r -> r.logoutUrl(SAML_LOGOUT_SERVICE_RESPONSE_LOCATION_PATH))

src/main/java/eu/openanalytics/containerproxy/security/WebSecurityConfig.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.context.annotation.Lazy;
3738
import org.springframework.core.convert.converter.Converter;
3839
import org.springframework.core.env.Environment;
3940
import org.springframework.security.access.AccessDeniedException;
@@ -57,6 +58,7 @@
5758
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
5859
import org.springframework.security.web.access.AccessDeniedHandler;
5960
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
61+
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
6062
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
6163
import org.springframework.security.web.csrf.MissingCsrfTokenException;
6264
import org.springframework.security.web.header.Header;
@@ -102,6 +104,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
102104
@Autowired(required=false)
103105
private List<ICustomSecurityConfig> customConfigs;
104106

107+
@Inject
108+
@Lazy
109+
private SavedRequestAwareAuthenticationSuccessHandler successHandler;
110+
105111
public static final String PROP_DISABLE_NO_SNIFF_HEADER = "proxy.api-security.disable-no-sniff-header";
106112
public static final String PROP_DISABLE_HSTS_HEADER = "proxy.api-security.disable-hsts-header";
107113
public static final String PROP_DISABLE_XSS_PROTECTION_HEADER = "proxy.api-security.disable-xss-protection-header";
@@ -233,7 +239,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
233239
http
234240
.formLogin()
235241
.loginPage("/login")
236-
.defaultSuccessUrl(AUTH_SUCCESS_URL, true) // TODO
242+
.successHandler(successHandler)
237243
.and()
238244
.logout()
239245
.logoutUrl(auth.getLogoutURL())
@@ -323,6 +329,13 @@ public AuthenticationManager authenticationManagerBean() throws Exception {
323329
return super.authenticationManagerBean();
324330
}
325331

332+
@Bean
333+
public SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler() {
334+
SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler();
335+
savedRequestAwareAuthenticationSuccessHandler.setDefaultTargetUrl(AUTH_SUCCESS_URL);
336+
return savedRequestAwareAuthenticationSuccessHandler;
337+
}
338+
326339
private List<Header> getCustomHeaders() {
327340
List<Header> headers = new ArrayList<>();
328341

src/main/java/eu/openanalytics/containerproxy/ui/AuthController.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,28 @@
2020
*/
2121
package eu.openanalytics.containerproxy.ui;
2222

23-
import javax.inject.Inject;
24-
import javax.servlet.ServletException;
25-
import javax.servlet.http.HttpServletRequest;
26-
import javax.servlet.http.HttpServletResponse;
27-
23+
import eu.openanalytics.containerproxy.api.BaseController;
2824
import eu.openanalytics.containerproxy.auth.IAuthenticationBackend;
2925
import eu.openanalytics.containerproxy.auth.impl.OpenIDAuthenticationBackend;
3026
import eu.openanalytics.containerproxy.auth.impl.SAMLAuthenticationBackend;
31-
import eu.openanalytics.containerproxy.auth.impl.saml.SAMLConfiguration;
3227
import org.springframework.core.env.Environment;
33-
import org.springframework.http.HttpHeaders;
34-
import org.springframework.http.MediaType;
3528
import org.springframework.stereotype.Controller;
3629
import org.springframework.ui.ModelMap;
3730
import org.springframework.web.bind.annotation.RequestMapping;
3831
import org.springframework.web.bind.annotation.RequestMethod;
39-
40-
import eu.openanalytics.containerproxy.api.BaseController;
4132
import org.springframework.web.bind.annotation.RequestParam;
4233
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
4334
import org.springframework.web.servlet.view.RedirectView;
4435

45-
import java.io.IOException;
46-
import java.net.URLEncoder;
47-
import java.nio.charset.StandardCharsets;
36+
import javax.inject.Inject;
37+
import javax.servlet.http.HttpServletRequest;
4838
import java.util.Optional;
4939

5040
@Controller
5141
public class AuthController extends BaseController {
5242

5343
public static final String AUTH_SUCCESS_URL = "/auth-success";
44+
public static final String AUTH_SUCCESS_URL_SESSION_ATTR = "AUTH_SUCCESS_URL_SESSION_ATTR";
5445

5546
@Inject
5647
private Environment environment;
@@ -79,9 +70,19 @@ public Object getLoginPage(@RequestParam Optional<String> error, ModelMap map) {
7970
}
8071

8172
@RequestMapping(value = AUTH_SUCCESS_URL, method = RequestMethod.GET)
82-
public String authSuccess(ModelMap map) {
73+
public String authSuccess(ModelMap map, HttpServletRequest request) {
8374
prepareMap(map);
84-
map.put("mainPage", ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString());
75+
map.put("url", ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString()); // default url
76+
77+
Object redirectUrl = request.getSession().getAttribute(AUTH_SUCCESS_URL_SESSION_ATTR);
78+
if (redirectUrl instanceof String) {
79+
request.getSession().removeAttribute(AUTH_SUCCESS_URL_SESSION_ATTR);
80+
String sRedirectUrl = (String) redirectUrl;
81+
// sanity check: does the redirect url start with the url of this current request
82+
if (sRedirectUrl.startsWith(ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString())) {
83+
map.put("url", redirectUrl);
84+
}
85+
}
8586
return "auth-success";
8687
}
8788

src/main/resources/templates/auth-success.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
2929
<script type="text/javascript" th:inline="javascript">
3030
history.pushState({}, "", new URL(location));
31-
window.location.href = [[${mainPage}]];
31+
window.location.href = [[${url}]];
3232
</script>
3333
</head>
34+
Test
3435
</html>

0 commit comments

Comments
 (0)