File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\security\AuthenticationProcessingFilter2.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Matthew R. Harrah
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.security;
25
26 import java.util.Properties;
27 import java.util.logging.Logger;
28 import java.util.logging.Level;
29 import java.io.IOException;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import hudson.Util;
35 import org.acegisecurity.Authentication;
36 import org.acegisecurity.AuthenticationException;
37 import org.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
38
39 /**
40 * {@link AuthenticationProcessingFilter} with a change for Jenkins so that
41 * we can pick up the hidden "from" form field defined in <tt>login.jelly</tt>
42 * to send the user back to where he came from, after a successful authentication.
43 *
44 * @author Kohsuke Kawaguchi
45 */
46 public class AuthenticationProcessingFilter2 extends AuthenticationProcessingFilter {
47 @Override
48 protected String determineTargetUrl(HttpServletRequest request) {
49 String targetUrl = request.getParameter("from");
50 request.getSession().setAttribute("from", targetUrl);
51
52 if (targetUrl == null)
53 return getDefaultTargetUrl();
54
55 if (Util.isAbsoluteUri(targetUrl))
56 return "."; // avoid open redirect
57
58 // URL returned from determineTargetUrl() is resolved against the context path,
59 // whereas the "from" URL is resolved against the top of the website, so adjust this.
60 if(targetUrl.startsWith(request.getContextPath()))
61 return targetUrl.substring(request.getContextPath().length());
62
63 // not sure when this happens, but apparently this happens in some case.
64 // see #1274
65 return targetUrl;
66 }
67
68 /**
69 * @see org.acegisecurity.ui.AbstractProcessingFilter#determineFailureUrl(javax.servlet.http.HttpServletRequest, org.acegisecurity.AuthenticationException)
70 */
71 @Override
72 protected String determineFailureUrl(HttpServletRequest request, AuthenticationException failed) {
73 Properties excMap = getExceptionMappings();
74 String failedClassName = failed.getClass().getName();
75 String whereFrom = request.getParameter("from");
76 request.getSession().setAttribute("from", whereFrom);
77 return excMap.getProperty(failedClassName, getAuthenticationFailureUrl());
78 }
79
80 @Override
81 protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException {
82 super.onSuccessfulAuthentication(request,response,authResult);
83 // make sure we have a session to store this successful authentication, given that we no longer
84 // let HttpSessionContextIntegrationFilter2 to create sessions.
85 // HttpSessionContextIntegrationFilter stores the updated SecurityContext object into this session later
86 // (either when a redirect is issued, via its HttpResponseWrapper, or when the execution returns to its
87 // doFilter method.
88 request.getSession();
89 }
90
91 /**
92 * Leave the information about login failure.
93 *
94 * <p>
95 * Otherwise it seems like Acegi doesn't really leave the detail of the failure anywhere.
96 */
97 @Override
98 protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
99 super.onUnsuccessfulAuthentication(request, response, failed);
100 LOGGER.log(Level.INFO, "Login attempt failed", failed);
101 }
102
103 private static final Logger LOGGER = Logger.getLogger(AuthenticationProcessingFilter2.class.getName());
104 }
105