Merge pull request #1868 from stianst/KEYCLOAK-2043
KEYCLOAK-2043 Set cache-control header in well-known endpoints
This commit is contained in:
commit
e664a4a6b2
3 changed files with 31 additions and 11 deletions
|
@ -3,6 +3,7 @@ package org.keycloak.services.resources;
|
|||
import org.jboss.logging.Logger;
|
||||
import org.jboss.resteasy.spi.NotFoundException;
|
||||
import org.jboss.resteasy.spi.ResteasyProviderFactory;
|
||||
import org.keycloak.Config;
|
||||
import org.keycloak.common.ClientConnection;
|
||||
import org.keycloak.events.EventBuilder;
|
||||
import org.keycloak.models.ClientModel;
|
||||
|
@ -17,6 +18,7 @@ import org.keycloak.services.clientregistration.ClientRegistrationService;
|
|||
import org.keycloak.services.managers.AuthenticationManager;
|
||||
import org.keycloak.services.managers.BruteForceProtector;
|
||||
import org.keycloak.services.managers.RealmManager;
|
||||
import org.keycloak.services.util.CacheControlUtil;
|
||||
import org.keycloak.wellknown.WellKnownProvider;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
|
@ -24,11 +26,7 @@ import javax.ws.rs.Path;
|
|||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import javax.ws.rs.core.*;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
|
@ -190,7 +188,7 @@ public class RealmsResource {
|
|||
init(name);
|
||||
|
||||
WellKnownProvider wellKnown = session.getProvider(WellKnownProvider.class, providerName);
|
||||
return Response.ok(wellKnown.getConfig()).build();
|
||||
return Response.ok(wellKnown.getConfig()).cacheControl(CacheControlUtil.getDefaultCacheControl()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.keycloak.freemarker.Theme;
|
|||
import org.keycloak.freemarker.ThemeProvider;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.common.util.MimeTypeUtil;
|
||||
import org.keycloak.services.util.CacheControlUtil;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
@ -49,11 +50,7 @@ public class ThemeResource {
|
|||
Theme theme = themeProvider.getTheme(themeName, Theme.Type.valueOf(themType.toUpperCase()));
|
||||
InputStream resource = theme.getResourceAsStream(path);
|
||||
if (resource != null) {
|
||||
CacheControl cacheControl = new CacheControl();
|
||||
cacheControl.setNoTransform(false);
|
||||
cacheControl.setMaxAge(Config.scope("theme").getInt("staticMaxAge", -1));
|
||||
|
||||
return Response.ok(resource).type(MimeTypeUtil.getContentType(path)).cacheControl(cacheControl).build();
|
||||
return Response.ok(resource).type(MimeTypeUtil.getContentType(path)).cacheControl(CacheControlUtil.getDefaultCacheControl()).build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package org.keycloak.services.util;
|
||||
|
||||
import org.keycloak.Config;
|
||||
|
||||
import javax.ws.rs.core.CacheControl;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
||||
*/
|
||||
public class CacheControlUtil {
|
||||
|
||||
public static CacheControl getDefaultCacheControl() {
|
||||
CacheControl cacheControl = new CacheControl();
|
||||
cacheControl.setNoTransform(false);
|
||||
Integer maxAge = Config.scope("theme").getInt("staticMaxAge");
|
||||
if (maxAge != null && maxAge > 0) {
|
||||
cacheControl.setMaxAge(maxAge);
|
||||
} else {
|
||||
cacheControl.setNoCache(true);
|
||||
}
|
||||
return cacheControl;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue