|
22 | 22 | import javax.servlet.http.HttpServletResponse; |
23 | 23 | import javax.servlet.http.HttpSession; |
24 | 24 |
|
| 25 | +import com.vaadin.flow.router.BeforeEnterEvent; |
25 | 26 | import com.vaadin.flow.server.auth.ViewAccessChecker; |
26 | 27 |
|
| 28 | +import org.springframework.core.log.LogMessage; |
27 | 29 | import org.springframework.security.core.Authentication; |
28 | 30 | import org.springframework.security.web.DefaultRedirectStrategy; |
29 | 31 | import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; |
30 | 32 | import org.springframework.security.web.csrf.CsrfToken; |
31 | 33 | import org.springframework.security.web.savedrequest.HttpSessionRequestCache; |
32 | 34 | import org.springframework.security.web.savedrequest.RequestCache; |
33 | 35 | import org.springframework.security.web.savedrequest.SavedRequest; |
| 36 | +import org.springframework.util.StringUtils; |
34 | 37 |
|
35 | 38 | /** |
36 | 39 | * A version of {@link SavedRequestAwareAuthenticationSuccessHandler} that |
@@ -89,16 +92,9 @@ public static class RedirectStrategy extends DefaultRedirectStrategy { |
89 | 92 | @Override |
90 | 93 | public void sendRedirect(HttpServletRequest request, |
91 | 94 | HttpServletResponse response, String url) throws IOException { |
92 | | - String redirectUrl; |
93 | | - String savedRedirectUrl = response.getHeader(SAVED_URL_HEADER); |
94 | | - if (savedRedirectUrl != null) { |
95 | | - redirectUrl = savedRedirectUrl; |
96 | | - } else { |
97 | | - redirectUrl = url; |
98 | | - } |
99 | 95 |
|
100 | 96 | if (!isTypescriptLogin(request)) { |
101 | | - super.sendRedirect(request, response, redirectUrl); |
| 97 | + super.sendRedirect(request, response, url); |
102 | 98 | return; |
103 | 99 | } |
104 | 100 |
|
@@ -126,33 +122,111 @@ public void sendRedirect(HttpServletRequest request, |
126 | 122 | */ |
127 | 123 | public VaadinSavedRequestAwareAuthenticationSuccessHandler() { |
128 | 124 | setRedirectStrategy(new RedirectStrategy()); |
| 125 | + setTargetUrlParameter(SAVED_URL_HEADER); |
129 | 126 | } |
130 | 127 |
|
| 128 | + /** |
| 129 | + * Called when a user has been successfully authenticated and finds out |
| 130 | + * whether it should redirect the user back to a default success url or the |
| 131 | + * originally requested url before the authentication. |
| 132 | + * <p> |
| 133 | + * As the user might have initiated the request to a restricted resource in |
| 134 | + * different ways, this method is responsible for extracting the final |
| 135 | + * target for redirection of the user and to set it on the response header, |
| 136 | + * so that it can be used by the redirection strategy in a unified way. See |
| 137 | + * {@link RedirectStrategy} and |
| 138 | + * {@link VaadinSavedRequestAwareAuthenticationSuccessHandler#determineTargetUrl(HttpServletRequest, HttpServletResponse)} |
| 139 | + * <p> |
| 140 | + * If the redirection to the login page for authentication is initiated by |
| 141 | + * spring security (such as entering some URI manually into the address bar |
| 142 | + * and not navigating via Vaadin application), then a SavedRequest object |
| 143 | + * containing the originally requested path is pushed to the request cache |
| 144 | + * by the Spring Security so the redirect target url would be extracted from |
| 145 | + * that. |
| 146 | + * <p> |
| 147 | + * Contrarily, navigating via Vaadin application router (e.g. via menus or |
| 148 | + * the links within the application) will result in requests being sent to |
| 149 | + * "/" or "/{app-context-root}", so the Spring Security will not intercept |
| 150 | + * and the SavedRequest will be null. In this case, the target redirect url |
| 151 | + * can be extracted from the session. See |
| 152 | + * {@link ViewAccessChecker#beforeEnter(BeforeEnterEvent)} |
| 153 | + * |
| 154 | + * @param request |
| 155 | + * the request which caused the successful authentication |
| 156 | + * @param response |
| 157 | + * the response |
| 158 | + * @param authentication |
| 159 | + * the <tt>Authentication</tt> object which was created during |
| 160 | + * the authentication process. |
| 161 | + */ |
131 | 162 | @Override |
132 | 163 | public void onAuthenticationSuccess(HttpServletRequest request, |
133 | 164 | HttpServletResponse response, Authentication authentication) |
134 | 165 | throws ServletException, IOException { |
135 | | - SavedRequest savedRequest = this.requestCache.getRequest(request, |
136 | | - response); |
137 | | - String storedServerNavigation = getStoredServerNavigation(request); |
138 | | - if (storedServerNavigation != null) { |
139 | | - response.setHeader(SAVED_URL_HEADER, storedServerNavigation); |
140 | | - } else if (savedRequest != null) { |
141 | | - /* |
142 | | - * This is here instead of in sendRedirect as we do not want to |
143 | | - * fallback to the default URL but instead send that separately. |
144 | | - */ |
145 | | - response.setHeader(SAVED_URL_HEADER, savedRequest.getRedirectUrl()); |
146 | | - } |
147 | 166 |
|
148 | 167 | if (isTypescriptLogin(request)) { |
149 | 168 | response.setHeader(DEFAULT_URL_HEADER, |
150 | 169 | determineTargetUrl(request, response)); |
151 | 170 | } |
152 | 171 |
|
| 172 | + SavedRequest savedRequest = this.requestCache.getRequest(request, |
| 173 | + response); |
| 174 | + String fullySavedRequestUrl = getStoredServerNavigation(request); |
| 175 | + |
| 176 | + if (savedRequest != null) { |
| 177 | + String targetUrlParameter = this.getTargetUrlParameter(); |
| 178 | + if (!this.isAlwaysUseDefaultTargetUrl() |
| 179 | + && (targetUrlParameter == null || !StringUtils.hasText( |
| 180 | + request.getParameter(targetUrlParameter)))) { |
| 181 | + this.clearAuthenticationAttributes(request); |
| 182 | + String targetUrl = savedRequest.getRedirectUrl(); |
| 183 | + response.setHeader(SAVED_URL_HEADER, targetUrl); |
| 184 | + this.getRedirectStrategy().sendRedirect(request, response, |
| 185 | + targetUrl); |
| 186 | + return; |
| 187 | + } else { |
| 188 | + this.requestCache.removeRequest(request, response); |
| 189 | + } |
| 190 | + } else if (fullySavedRequestUrl != null) { |
| 191 | + response.setHeader(SAVED_URL_HEADER, fullySavedRequestUrl); |
| 192 | + } |
| 193 | + |
153 | 194 | super.onAuthenticationSuccess(request, response, authentication); |
154 | 195 | } |
155 | 196 |
|
| 197 | + /** |
| 198 | + * Determines the originally requested path by the user before |
| 199 | + * authentication by reading the target redirect url from the response |
| 200 | + * header. |
| 201 | + * <p> |
| 202 | + * Note that if a defaultSuccessUrl has been configured on the http security |
| 203 | + * configurer, or the value of {@code targetUrlParameter} is {@code null}, |
| 204 | + * it will fall back to the default super class implementation. |
| 205 | + * |
| 206 | + * @param request |
| 207 | + * the http servlet request instance |
| 208 | + * @param response |
| 209 | + * the http servlet response instance |
| 210 | + * @return the original requested path by the user before authentication. |
| 211 | + */ |
| 212 | + @Override |
| 213 | + protected String determineTargetUrl(HttpServletRequest request, |
| 214 | + HttpServletResponse response) { |
| 215 | + if (!isAlwaysUseDefaultTargetUrl() |
| 216 | + && this.getTargetUrlParameter() != null) { |
| 217 | + String targetUrl = response.getHeader(this.getTargetUrlParameter()); |
| 218 | + if (StringUtils.hasText(targetUrl)) { |
| 219 | + if (this.logger.isTraceEnabled()) { |
| 220 | + this.logger.trace(LogMessage.format( |
| 221 | + "Using url %s from response header %s", targetUrl, |
| 222 | + this.getTargetUrlParameter())); |
| 223 | + } |
| 224 | + return targetUrl; |
| 225 | + } |
| 226 | + } |
| 227 | + return super.determineTargetUrl(request, response); |
| 228 | + } |
| 229 | + |
156 | 230 | /** |
157 | 231 | * Gets the target URL potentially stored by the server side view access |
158 | 232 | * control. |
|
0 commit comments