oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/BearerTokenAuthenticationEntryPoint.java
@Override
	public void commence(
			HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException)
			throws IOException, ServletException {

		HttpStatus status = HttpStatus.UNAUTHORIZED;

		Map<String, String> parameters = new LinkedHashMap<>();

		if (this.realmName != null) {
			parameters.put("realm", this.realmName);
		}

		if (authException instanceof OAuth2AuthenticationException) {
			OAuth2Error error = ((OAuth2AuthenticationException) authException).getError();

			parameters.put("error", error.getErrorCode());

			if (StringUtils.hasText(error.getDescription())) {
				parameters.put("error_description", error.getDescription());
			}

			if (StringUtils.hasText(error.getUri())) {
				parameters.put("error_uri", error.getUri());
			}

			if (error instanceof BearerTokenError) {
				BearerTokenError bearerTokenError = (BearerTokenError) error;

				if (StringUtils.hasText(bearerTokenError.getScope())) {
					parameters.put("scope", bearerTokenError.getScope());
				}

				status = ((BearerTokenError) error).getHttpStatus();
			}
		}

		String wwwAuthenticate = computeWWWAuthenticateHeaderValue(parameters);

		response.addHeader(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate);
		response.setStatus(status.value());
	}
@Override
	public void commence(
			HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException)
			throws IOException, ServletException {

		HttpStatus status = HttpStatus.UNAUTHORIZED;

		Map<String, String> parameters = new LinkedHashMap<>();

		if (this.realmName != null) {
			parameters.put("realm", this.realmName);
		}

		if (authException instanceof OAuth2AuthenticationException) {
			OAuth2Error error = ((OAuth2AuthenticationException) authException).getError();

			parameters.put("error", error.getErrorCode());

			if (StringUtils.hasText(error.getDescription())) {
				parameters.put("error_description", error.getDescription());
			}

			if (StringUtils.hasText(error.getUri())) {
				parameters.put("error_uri", error.getUri());
			}

			if (error instanceof BearerTokenError) {
				BearerTokenError bearerTokenError = (BearerTokenError) error;

				if (StringUtils.hasText(bearerTokenError.getScope())) {
					parameters.put("scope", bearerTokenError.getScope());
				}

				status = ((BearerTokenError) error).getHttpStatus();
			}
		}

		String wwwAuthenticate = computeWWWAuthenticateHeaderValue(parameters);

		response.addHeader(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate);
		response.setStatus(status.value());
	}
Collect error details from the provided parameters and format according to
RFC 6750, specifically {@code error}, {@code error_description}, {@code error_uri}, and {@scope scope}.

@param request that resulted in an <code>AuthenticationException</code>
@param response so that the user agent can begin authentication
@param authException that caused the invocation