diff --git a/src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java b/src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java index e02ba171..d2cee0b8 100644 --- a/src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java +++ b/src/main/java/org/jenkinsci/plugins/oic/OicSecurityRealm.java @@ -619,7 +619,12 @@ private String buildOAuthRedirectUrl() throws NullPointerException { * @return an HttpResponse */ public HttpResponse doFinishLogin(StaplerRequest request) { - return OicSession.getCurrent().doFinishLogin(request); + OicSession currentSession = OicSession.getCurrent(); + if(currentSession==null) { + LOGGER.fine("No session to resume (perhaps jenkins was restarted?)"); + return HttpResponses.errorWithoutStack(401, "Unauthorized"); + } + return currentSession.doFinishLogin(request); } /**