Skip to content

Commit

Permalink
Merge pull request #2312 from pjanouse/JENKINS-31618
Browse files Browse the repository at this point in the history
[JENKINS-31618] Fixed NoSuchMethodException in loginLink.jelly
  • Loading branch information
olivergondza committed May 13, 2016
2 parents ebb84da + 1642f64 commit 782804f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
54 changes: 54 additions & 0 deletions core/src/main/java/hudson/security/SecurityRealm.java
Expand Up @@ -48,6 +48,9 @@
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -62,6 +65,7 @@
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -484,6 +488,56 @@ public Filter createFilter(FilterConfig filterConfig) {
*/
public static final SecurityRealm NO_AUTHENTICATION = new None();

/**
* Perform a calculation where we should go back after successful login
*
* @return Encoded URI where we should go back after successful login
* or "/" if no way back or an issue occurred
*
* @since TODO
*/
@Restricted(DoNotUse.class)
public static String getFrom() {
String from = null, returnValue = null;
final StaplerRequest request = Stapler.getCurrentRequest();

// Try to obtain a return point either from the Session
// or from the QueryParameter in this order
if (request != null
&& request.getSession(false) != null) {
from = (String) request.getSession().getAttribute("from");
} else if (request != null) {
from = request.getParameter("from");
}

// If entry point was not found, try to deduce it from the request URI
// except pages related to login process
if (from == null
&& request != null
&& request.getRequestURI() != null
&& !request.getRequestURI().equals("/loginError")
&& !request.getRequestURI().equals("/login")) {

from = request.getRequestURI();
}

// If deduced entry point isn't deduced yet or the content is a blank value
// use the root web point "/" as a fallback
if (StringUtils.isBlank(from)) {
from = "/";
}
from.trim();

// Encode the return value
try {
returnValue = java.net.URLEncoder.encode(from, "UTF-8");
} catch (UnsupportedEncodingException e) { }

// Return encoded value or at least "/" in the case exception occurred during encode()
// or if the encoded content is blank value
return StringUtils.isBlank(returnValue) ? "/" : returnValue;
}

private static class None extends SecurityRealm {
public SecurityComponents createSecurityComponents() {
return new SecurityComponents(new AuthenticationManager() {
Expand Down
Expand Up @@ -25,9 +25,5 @@ THE SOFTWARE.

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<j:invokeStatic var="from" className="java.net.URLEncoder" method="encode">
<j:arg value="${if (request.session.attribute('from')!=null) request.session.getAttribute('from'); else if (request.getParameter('from')!=null) request.getParameter('from'); else if (request.requestURI=='/loginError' || request.requestURI=='/login') '/'; else request.requestURI;}"/>
<j:arg value="UTF-8"/>
</j:invokeStatic>
<a href="${rootURL}/${app.securityRealm.loginUrl}?from=${from}"><b>${%login}</b></a>
<a href="${rootURL}/${app.securityRealm.loginUrl}?from=${app.securityRealm.from}"><b>${%login}</b></a>
</j:jelly>

0 comments on commit 782804f

Please sign in to comment.