@@ -36,8 +36,8 @@ public class AdminFilter implements Filter {
3636
3737 private boolean disableFilter ;
3838 private String loginPage ;
39- private String errorPage ;
4039 private String indexPage ;
40+ private String redirectPrefix ;
4141
4242 @ Inject
4343 AdminSession adminSession ;
@@ -59,7 +59,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
5959 if (!has (loginPage )) {
6060 loginPage = has (adminConfig ) ? adminConfig .getLoginPage () : Constants .DEFAULT_LOGIN_PAGE ;
6161 }
62- errorPage = filterConfig .getServletContext ().getInitParameter (Constants .InitialParams .ERROR_PAGE );
62+ String errorPage = filterConfig .getServletContext ().getInitParameter (Constants .InitialParams .ERROR_PAGE );
6363 if (!has (errorPage )) {
6464 errorPage = Constants .DEFAULT_ERROR_PAGE ;
6565 }
@@ -73,8 +73,8 @@ public void init(FilterConfig filterConfig) throws ServletException {
7373 loginPage = loginPage .startsWith ("/" ) ? loginPage .substring (1 ) : loginPage ;
7474 indexPage = indexPage .startsWith ("/" ) ? indexPage .substring (1 ) : indexPage ;
7575
76- ignoredResources .add ("/" + loginPage .substring (0 ,loginPage .lastIndexOf ("." )));//we need leading slash for ignoredResources
77- ignoredResources .add ("/" + errorPage .substring (0 ,errorPage .lastIndexOf ("." )));
76+ ignoredResources .add ("/" + loginPage .substring (0 , loginPage .lastIndexOf ("." )));//we need leading slash for ignoredResources
77+ ignoredResources .add ("/" + errorPage .substring (0 , errorPage .lastIndexOf ("." )));
7878
7979 String configuredResouces = adminConfig .getIgnoredResources ();
8080 if (has (configuredResouces )) {
@@ -107,7 +107,7 @@ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain
107107
108108 if (request .getRequestURI ().equals (request .getContextPath () + "/" )
109109 || (adminSession .isLoggedIn () && request .getRequestURI ().endsWith (loginPage ))) {
110- response .sendRedirect (request .getContextPath () + "/" + indexPage );
110+ response .sendRedirect (getRedirectPrefix ( request ) + request .getContextPath () + "/" + indexPage );
111111 return ;
112112 }
113113
@@ -119,13 +119,15 @@ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain
119119 if (skipResource (request , response ) || adminSession .isLoggedIn ()) {
120120 if (!adminSession .isUserRedirected () && adminSession .isLoggedIn () && has (request .getHeader ("Referer" )) && request .getHeader ("Referer" ).contains ("?page=" )) {
121121 adminSession .setUserRedirected (true );
122- response .sendRedirect (request .getContextPath () + extractPageFromURL (request .getHeader ("Referer" )));
122+ String pageFromURL = request .getContextPath () + extractPageFromURL (request .getHeader ("Referer" ));
123+ log .info ("Redirecting user back to " + pageFromURL );
124+ response .sendRedirect (getRedirectPrefix (request ) + pageFromURL );
123125 return ;
124126 }
125127 try {
126128 chain .doFilter (req , resp );
127129 } catch (FileNotFoundException e ) {
128- log .log (Level .WARNING ,"File not found" , e );
130+ log .log (Level .WARNING , "File not found" , e );
129131 response .sendError (404 );
130132 }
131133 } else { //resource not skipped (e.g a page that is not logon page) AND user not logged in
@@ -140,7 +142,7 @@ private String extractPageFromURL(String referer) {
140142 try {
141143 return URLDecoder .decode (page , "UTF-8" );
142144 } catch (UnsupportedEncodingException e ) {
143- log .log (Level .WARNING ,"Could not extract page from url" , e );
145+ log .log (Level .WARNING , "Could not extract page from url" , e );
144146 return indexPage ;
145147 }
146148 }
@@ -158,8 +160,8 @@ public void destroy() {
158160 */
159161 private boolean skipResource (HttpServletRequest request , HttpServletResponse response ) {
160162 String path = request .getServletPath ();
161- if (path .contains ("." )) {
162- path = path .substring (0 ,path .lastIndexOf ("." ));
163+ if (path .contains ("." )) {
164+ path = path .substring (0 , path .lastIndexOf ("." ));
163165 }
164166 boolean skip = path .startsWith (FACES_RESOURCES ) || shouldIgnoreResource (path ) || response .getStatus () == HttpServletResponse .SC_INTERNAL_SERVER_ERROR ;
165167 return skip ;
@@ -200,23 +202,24 @@ private void redirectToLogon(HttpServletRequest request, HttpServletResponse res
200202 .append ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" )
201203 .printf ("<partial-response><redirect url=\" %s\" ></redirect></partial-response>" , redirectUrl );
202204 } else {//normal redirect
203- response .sendRedirect (redirectUrl );
205+ response .sendRedirect (getRedirectPrefix ( request ) + redirectUrl );
204206 }
205207
206208 } catch (Exception e ) {
207- log .log (Level .SEVERE ,"Could not redirect to " + loginPage , e );
209+ log .log (Level .SEVERE , "Could not redirect to " + loginPage , e );
208210 }
209211
210212 }
211213
212214 /**
213215 * Skip error pages, login and index page as recovery url because it doesn't make sense redirecting user to such pages
216+ *
214217 * @param recoveryUrl
215- * @return
218+ * @return
216219 */
217220 private boolean isValidRecoveryUrl (StringBuilder recoveryUrl ) {
218221 String pageSuffix = adminConfig .getPageSufix ();
219- return !recoveryUrl .toString ().contains (Constants .DEFAULT_INDEX_PAGE .replace ("xhtml" , pageSuffix )) && !recoveryUrl .toString ().contains (Constants .DEFAULT_ACCESS_DENIED_PAGE .replace ("xhtml" , adminConfig .getPageSufix ()))
222+ return !recoveryUrl .toString ().contains (Constants .DEFAULT_INDEX_PAGE .replace ("xhtml" , pageSuffix )) && !recoveryUrl .toString ().contains (Constants .DEFAULT_ACCESS_DENIED_PAGE .replace ("xhtml" , adminConfig .getPageSufix ()))
220223 && !recoveryUrl .toString ().contains (Constants .DEFAULT_EXPIRED_PAGE .replace ("xhtml" , pageSuffix )) && !recoveryUrl .toString ().contains (Constants .DEFAULT_OPTIMISTIC_PAGE .replace ("xhtml" , adminConfig .getPageSufix ()))
221224 && !recoveryUrl .toString ().contains (Constants .DEFAULT_LOGIN_PAGE .replace ("xhtml" , adminConfig .getPageSufix ()));
222225 }
@@ -228,11 +231,32 @@ private boolean isValidRecoveryUrl(StringBuilder recoveryUrl) {
228231 */
229232 private boolean shouldIgnoreResource (String path ) {
230233 for (String ignoredResource : ignoredResources ) {
231- if (path .startsWith (ignoredResource )) {
234+ if (path .startsWith (ignoredResource )) {
232235 return true ;
233236 }
234237 }
235238 return false ;
236239 }
237240
241+ private String getRedirectPrefix (HttpServletRequest request ) {
242+ if (redirectPrefix == null ) {
243+ String url = request .getRequestURL ().toString ();
244+ String uri = request .getRequestURI ();
245+ int offset = url .indexOf (uri );
246+ redirectPrefix = url .substring (0 , offset );
247+ if (useHttps (request )) {
248+ log .log (Level .WARNING ,"Changing request scheme to https." );
249+ redirectPrefix = redirectPrefix .replace ("http:" ,"https:" );
250+ }
251+ }
252+ return redirectPrefix ;
253+ }
254+
255+ private static boolean useHttps (HttpServletRequest request ) {
256+ String protocolProperty = System .getProperty ("admin.protocol" , System .getenv ("admin.protocol" ));
257+
258+ String protoHeader = request .getHeader ("X-Forwarded-Proto" );
259+ return request .isSecure () || (protoHeader != null && protoHeader .toLowerCase ().equals ("https" ))
260+ || (protocolProperty != null && protocolProperty .toLowerCase ().equals ("https" ));
261+ }
238262}
0 commit comments