pull/1498/merge
Thomas Meyer 2022-05-02 14:09:15 +09:00 committed by GitHub
commit a0c1bf7220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 31 deletions

View File

@ -0,0 +1,49 @@
package org.mitre.openid.connect;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ErrorController {
private static final Logger logger = LoggerFactory.getLogger(ErrorController.class);
@RequestMapping("/error")
public String handle(HttpServletRequest req) {
Throwable errorException = (Throwable) req.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
String message = (String) req.getAttribute(RequestDispatcher.ERROR_MESSAGE);
String requestUri = (String) req.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
logger.error("request {} failed with {}", requestUri, message);
logger.error("exception", errorException);
processError(req);
return "/error-view";
}
private void processError(HttpServletRequest request) {
if (request.getAttribute("error") instanceof OAuth2Exception) {
request.setAttribute("errorCode", ((OAuth2Exception)request.getAttribute("error")).getOAuth2ErrorCode());
request.setAttribute("message", ((OAuth2Exception)request.getAttribute("error")).getMessage());
} else if (request.getAttribute(RequestDispatcher.ERROR_EXCEPTION) != null) {
Throwable t = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
request.setAttribute("errorCode", t.getClass().getSimpleName() + " (" + request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE) + ")");
request.setAttribute("message", t.getMessage());
} else if (request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE) != null) {
Integer code = (Integer)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
HttpStatus status = HttpStatus.valueOf(code);
request.setAttribute("errorCode", status.toString() + " " + status.getReasonPhrase());
request.setAttribute("message", request.getAttribute(RequestDispatcher.ERROR_MESSAGE));
} else {
request.setAttribute("errorCode", "Server error");
request.setAttribute("message", "See the logs for details");
}
}
}

View File

@ -55,6 +55,6 @@
<bean id="oauthRequestValidator" class="org.mitre.oauth2.token.ScopeServiceAwareOAuth2RequestValidator" />
<!-- Error page handler. -->
<mvc:view-controller path="/error" view-name="error" />
<mvc:view-controller path="/errorview" view-name="error-view" />
</beans>

View File

@ -4,27 +4,7 @@
<%@ taglib prefix="o" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<%@page import="org.springframework.security.oauth2.common.exceptions.OAuth2Exception"%>
<%
if (request.getAttribute("error") != null && request.getAttribute("error") instanceof OAuth2Exception) {
request.setAttribute("errorCode", ((OAuth2Exception)request.getAttribute("error")).getOAuth2ErrorCode());
request.setAttribute("message", ((OAuth2Exception)request.getAttribute("error")).getMessage());
} else if (request.getAttribute("javax.servlet.error.exception") != null) {
Throwable t = (Throwable)request.getAttribute("javax.servlet.error.exception");
request.setAttribute("errorCode", t.getClass().getSimpleName() + " (" + request.getAttribute("javax.servlet.error.status_code") + ")");
request.setAttribute("message", t.getMessage());
} else if (request.getAttribute("javax.servlet.error.status_code") != null) {
Integer code = (Integer)request.getAttribute("javax.servlet.error.status_code");
HttpStatus status = HttpStatus.valueOf(code);
request.setAttribute("errorCode", status.toString() + " " + status.getReasonPhrase());
request.setAttribute("message", request.getAttribute("javax.servlet.error.message"));
} else {
request.setAttribute("errorCode", "Server error");
request.setAttribute("message", "See the logs for details");
}
%>
<spring:message code="error.title" var="title"/>
<o:header title="${title}" />
<o:topbar pageName="Error" />
@ -38,11 +18,9 @@ if (request.getAttribute("error") != null && request.getAttribute("error") insta
<p>
<spring:message code="error.message"/>
<blockquote class="text-error"><b><c:out value="${ message }" /></b></blockquote>
</p>
</p>
</div>
</div>
</div>
</div>
<o:footer />
<o:footer/>

View File

@ -71,9 +71,9 @@
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
<error-page>
<location>/error</location>
<location>/errorController</location>
</error-page>
</web-app>

View File

@ -404,8 +404,8 @@
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -671,7 +671,7 @@
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>