Skip to content

Commit

Permalink
[JENKINS-31618] Fixed NoSuchMethodException in loginLink.jelly
Browse files Browse the repository at this point in the history
The logic is moved from template back to Java and modified to fix the issue.
The argorithm is rewritten for better legibility.
  • Loading branch information
pjanouse committed May 12, 2016
1 parent d3b61b9 commit 6c6f34c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
39 changes: 39 additions & 0 deletions core/src/main/java/hudson/security/SecurityRealm.java
Expand Up @@ -62,6 +62,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 +485,44 @@ public Filter createFilter(FilterConfig filterConfig) {
*/
public static final SecurityRealm NO_AUTHENTICATION = new None();

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

if (request.getSession(false) != null) {
from = (String) request.getSession().getAttribute("from");
}

if (from == null) {
from = request.getParameter("from");
}

final String requestURI = request.getRequestURI();
if (from == null && requestURI != null
&& requestURI.compareTo("/loginError") != 0 && requestURI.compareTo("/login") != 0) {
from = requestURI;
}

if (from == null || from.trim().isEmpty()) {
from = "/";
}

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

return from;
}

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>
</j:jelly>

0 comments on commit 6c6f34c

Please sign in to comment.