|
23 | 23 | import eu.openanalytics.containerproxy.auth.IAuthenticationBackend; |
24 | 24 | import eu.openanalytics.containerproxy.security.ICustomSecurityConfig; |
25 | 25 | import eu.openanalytics.containerproxy.service.UserService; |
| 26 | +import org.springframework.context.annotation.Lazy; |
26 | 27 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 28 | +import org.springframework.security.web.DefaultRedirectStrategy; |
27 | 29 | import org.springframework.security.web.access.ExceptionTranslationFilter; |
| 30 | +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; |
28 | 31 | import org.springframework.stereotype.Component; |
| 32 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
29 | 33 |
|
30 | 34 | import javax.inject.Inject; |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | +import java.io.IOException; |
| 38 | + |
| 39 | +import static eu.openanalytics.containerproxy.ui.AuthController.AUTH_SUCCESS_URL_SESSION_ATTR; |
31 | 40 |
|
32 | 41 | @Component |
33 | 42 | public class UISecurityConfig implements ICustomSecurityConfig { |
34 | 43 |
|
35 | | - @Inject |
36 | | - private IAuthenticationBackend auth; |
37 | | - |
38 | | - @Inject |
39 | | - private UserService userService; |
40 | | - |
41 | | - @Override |
42 | | - public void apply(HttpSecurity http) throws Exception { |
43 | | - if (auth.hasAuthorization()) { |
44 | | - |
45 | | - // Limit access to the app pages according to spec permissions |
46 | | - http.authorizeRequests().antMatchers("/app/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
47 | | - http.authorizeRequests().antMatchers("/app_i/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
48 | | - http.authorizeRequests().antMatchers("/app_direct/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
49 | | - http.authorizeRequests().antMatchers("/app_direct_i/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
50 | | - |
51 | | - // Limit access to the admin pages |
52 | | - http.authorizeRequests().antMatchers("/admin").hasAnyRole(userService.getAdminGroups()); |
53 | | - http.authorizeRequests().antMatchers("/admin/data").hasAnyRole(userService.getAdminGroups()); |
54 | | - |
55 | | - http.addFilterAfter(new AuthenticationRequiredFilter(), ExceptionTranslationFilter.class); |
56 | | - } |
57 | | - |
58 | | - } |
| 44 | + @Inject |
| 45 | + private IAuthenticationBackend auth; |
| 46 | + |
| 47 | + @Inject |
| 48 | + private UserService userService; |
| 49 | + |
| 50 | + @Inject |
| 51 | + @Lazy |
| 52 | + private SavedRequestAwareAuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void apply(HttpSecurity http) throws Exception { |
| 56 | + if (auth.hasAuthorization()) { |
| 57 | + |
| 58 | + // Limit access to the app pages according to spec permissions |
| 59 | + http.authorizeRequests().antMatchers("/app/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
| 60 | + http.authorizeRequests().antMatchers("/app_i/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
| 61 | + http.authorizeRequests().antMatchers("/app_direct/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
| 62 | + http.authorizeRequests().antMatchers("/app_direct_i/{specId}/**").access("@proxyAccessControlService.canAccessOrHasExistingProxy(authentication, #specId)"); |
| 63 | + |
| 64 | + http.addFilterAfter(new AuthenticationRequiredFilter(), ExceptionTranslationFilter.class); |
| 65 | + |
| 66 | + savedRequestAwareAuthenticationSuccessHandler.setRedirectStrategy(new DefaultRedirectStrategy() { |
| 67 | + @Override |
| 68 | + public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { |
| 69 | + String redirectUrl = calculateRedirectUrl(request.getContextPath(), url); |
| 70 | + AppRequestInfo appRequestInfo = AppRequestInfo.fromURI(redirectUrl); |
| 71 | + if (appRequestInfo != null) { |
| 72 | + // before auth, the user tried to open the page of an app, redirect back to that app |
| 73 | + // (we don't redirect to any other app, see #30648 and #28624) |
| 74 | + request.getSession().setAttribute(AUTH_SUCCESS_URL_SESSION_ATTR, url); |
| 75 | + } |
| 76 | + response.sendRedirect(ServletUriComponentsBuilder.fromCurrentContextPath().path("/auth-success").build().toUriString()); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + // Limit access to the admin pages |
| 81 | + http.authorizeRequests().antMatchers("/admin").access("@userService.isAdmin()"); |
| 82 | + http.authorizeRequests().antMatchers("/admin/data").access("@userService.isAdmin()"); |
| 83 | + |
| 84 | + } |
59 | 85 | } |
0 commit comments