@@ -132,10 +132,6 @@ public void setLandingPage(String landingPage) {
132132 protected boolean doAuthenticate (Request request , HttpServletResponse response )
133133 throws IOException {
134134
135- if (checkForCachedAuthentication (request , response , true )) {
136- return true ;
137- }
138-
139135 // References to objects we will need later
140136 Session session = null ;
141137 Principal principal = null ;
@@ -154,9 +150,8 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
154150 }
155151 principal = context .getRealm ().authenticate (username , password );
156152 if (principal != null ) {
157- session . setNote ( Constants . FORM_PRINCIPAL_NOTE , principal );
153+ register ( request , response , principal , HttpServletRequest . FORM_AUTH , username , password );
158154 if (!matchRequest (request )) {
159- register (request , response , principal , HttpServletRequest .FORM_AUTH , username , password );
160155 return true ;
161156 }
162157 }
@@ -173,16 +168,6 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
173168 if (log .isDebugEnabled ()) {
174169 log .debug ("Restore request from session '" + session .getIdInternal () + "'" );
175170 }
176- principal = (Principal ) session .getNote (Constants .FORM_PRINCIPAL_NOTE );
177- register (request , response , principal , HttpServletRequest .FORM_AUTH ,
178- (String ) session .getNote (Constants .SESS_USERNAME_NOTE ),
179- (String ) session .getNote (Constants .SESS_PASSWORD_NOTE ));
180- // If we're caching principals we no longer need the user name
181- // and password in the session, so remove them
182- if (cache ) {
183- session .removeNote (Constants .SESS_USERNAME_NOTE );
184- session .removeNote (Constants .SESS_PASSWORD_NOTE );
185- }
186171 if (restoreRequest (request , session )) {
187172 if (log .isDebugEnabled ()) {
188173 log .debug ("Proceed to restored request" );
@@ -197,6 +182,12 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
197182 }
198183 }
199184
185+ // This check has to be after the previous check for a matching request
186+ // because that matching request may also include a cached Principal.
187+ if (checkForCachedAuthentication (request , response , true )) {
188+ return true ;
189+ }
190+
200191 // Acquire references to objects we will need to evaluate
201192 String contextPath = request .getContextPath ();
202193 String requestURI = request .getDecodedRequestURI ();
@@ -283,12 +274,7 @@ protected boolean doAuthenticate(Request request, HttpServletResponse response)
283274 return false ;
284275 }
285276
286- // Save the authenticated Principal in our session
287- session .setNote (Constants .FORM_PRINCIPAL_NOTE , principal );
288-
289- // Save the username and password as well
290- session .setNote (Constants .SESS_USERNAME_NOTE , username );
291- session .setNote (Constants .SESS_PASSWORD_NOTE , password );
277+ register (request , response , principal , HttpServletRequest .FORM_AUTH , username , password );
292278
293279 // Redirect the user to the original request URI (which will cause
294280 // the original request to be restored)
@@ -489,7 +475,7 @@ protected boolean matchRequest(Request request) {
489475 }
490476
491477 // Is there a saved principal?
492- if (session .getNote ( Constants . FORM_PRINCIPAL_NOTE ) == null ) {
478+ if (cache && session .getPrincipal () == null || ! cache && request . getPrincipal ( ) == null ) {
493479 return false ;
494480 }
495481
@@ -519,7 +505,6 @@ protected boolean restoreRequest(Request request, Session session)
519505 // Retrieve and remove the SavedRequest object from our session
520506 SavedRequest saved = (SavedRequest ) session .getNote (Constants .FORM_REQUEST_NOTE );
521507 session .removeNote (Constants .FORM_REQUEST_NOTE );
522- session .removeNote (Constants .FORM_PRINCIPAL_NOTE );
523508 if (saved == null ) {
524509 return false ;
525510 }
0 commit comments