From e4a8ced8b0735e650f081a4383761b686a12c807 Mon Sep 17 00:00:00 2001 From: mposolda Date: Fri, 16 Jan 2015 12:24:56 +0100 Subject: [PATCH] KEYCLOAK-974 ServerVersionResource doesn't handle cors requests --- .../src/main/webapp/index.html | 10 ++++++++ .../src/main/webapp/js/app.js | 9 ++++++++ .../resources/ServerVersionResource.java | 23 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/examples/cors/angular-product-app/src/main/webapp/index.html b/examples/cors/angular-product-app/src/main/webapp/index.html index 6cef8b9166..b385fe3d35 100755 --- a/examples/cors/angular-product-app/src/main/webapp/index.html +++ b/examples/cors/angular-product-app/src/main/webapp/index.html @@ -81,6 +81,16 @@
+
+

Server version

+ + +
+ Keycloak version: {{version.version}}
+ Keycloak build time: {{version['build-time'] | date:'yyyy-MM-dd HH:mm:ss'}}
+
+
+
diff --git a/examples/cors/angular-product-app/src/main/webapp/js/app.js b/examples/cors/angular-product-app/src/main/webapp/js/app.js index 699644ed50..d6e9fd41a2 100755 --- a/examples/cors/angular-product-app/src/main/webapp/js/app.js +++ b/examples/cors/angular-product-app/src/main/webapp/js/app.js @@ -32,6 +32,9 @@ angular.element(document).ready(function ($http) { module.controller('GlobalCtrl', function($scope, $http) { $scope.products = []; $scope.roles = []; + $scope.serverInfo = []; + $scope.realm = []; + $scope.version = []; $scope.reloadData = function() { $http.get("http://localhost-db:8080/cors-database/products").success(function(data) { $scope.products = angular.fromJson(data); @@ -72,6 +75,12 @@ module.controller('GlobalCtrl', function($scope, $http) { }); }; + $scope.loadVersion = function() { + $http.get("http://localhost-auth:8080/auth/version").success(function(data) { + $scope.version = angular.fromJson(data); + }); + }; + $scope.logout = logout; }); diff --git a/services/src/main/java/org/keycloak/services/resources/ServerVersionResource.java b/services/src/main/java/org/keycloak/services/resources/ServerVersionResource.java index ad703f986a..4bf4b2fdbc 100755 --- a/services/src/main/java/org/keycloak/services/resources/ServerVersionResource.java +++ b/services/src/main/java/org/keycloak/services/resources/ServerVersionResource.java @@ -1,12 +1,19 @@ package org.keycloak.services.resources; +import org.jboss.logging.Logger; import org.jboss.resteasy.annotations.cache.NoCache; +import org.jboss.resteasy.spi.HttpRequest; +import org.jboss.resteasy.spi.HttpResponse; import org.keycloak.Version; import javax.ws.rs.GET; +import javax.ws.rs.OPTIONS; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; /** * @author Bill Burke @@ -15,10 +22,26 @@ import javax.ws.rs.core.MediaType; @Path("/version") public class ServerVersionResource { + protected static final Logger logger = Logger.getLogger(ServerVersionResource.class); + + @Context + protected HttpRequest request; + + @Context + protected HttpResponse response; + + @OPTIONS + @Produces(MediaType.APPLICATION_JSON) + public Response getVersionPreflight() { + logger.debugv("cors request from: {0}", request.getHttpHeaders().getRequestHeaders().getFirst("Origin")); + return Cors.add(request, Response.ok()).allowedMethods("GET").auth().preflight().build(); + } + @GET @NoCache @Produces(MediaType.APPLICATION_JSON) public Version getVersion() { + Cors.add(request).allowedOrigins("*").allowedMethods("GET").auth().build(response); return Version.SINGLETON; } }