Merge pull request #1247 from Smartling/KEYCLOAK-1299
Add Servlet 2.x support to the Spring Security adapter
This commit is contained in:
commit
5aa90243a0
2 changed files with 19 additions and 1 deletions
|
@ -1,11 +1,15 @@
|
||||||
package org.keycloak.adapters.springsecurity.facade;
|
package org.keycloak.adapters.springsecurity.facade;
|
||||||
|
|
||||||
import org.keycloak.adapters.HttpFacade.Response;
|
import org.keycloak.adapters.HttpFacade.Response;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concrete Keycloak {@link Response response} implementation wrapping an {@link HttpServletResponse}.
|
* Concrete Keycloak {@link Response response} implementation wrapping an {@link HttpServletResponse}.
|
||||||
|
@ -15,6 +19,7 @@ import java.io.OutputStream;
|
||||||
*/
|
*/
|
||||||
class WrappedHttpServletResponse implements Response {
|
class WrappedHttpServletResponse implements Response {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(WrappedHttpServletResponse.class);
|
||||||
private final HttpServletResponse response;
|
private final HttpServletResponse response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,11 +55,23 @@ class WrappedHttpServletResponse implements Response {
|
||||||
|
|
||||||
cookie.setMaxAge(maxAge);
|
cookie.setMaxAge(maxAge);
|
||||||
cookie.setSecure(secure);
|
cookie.setSecure(secure);
|
||||||
cookie.setHttpOnly(httpOnly);
|
this.setHttpOnly(cookie, httpOnly);
|
||||||
|
|
||||||
response.addCookie(cookie);
|
response.addCookie(cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setHttpOnly(Cookie cookie, boolean httpOnly) {
|
||||||
|
Method method;
|
||||||
|
try {
|
||||||
|
method = Cookie.class.getMethod("setHttpOnly", boolean.class);
|
||||||
|
method.invoke(cookie, httpOnly);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
log.warn("Unable to set httpOnly on cookie [{}]; no such method on javax.servlet.http.Cookie", cookie.getName());
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
log.error("Unable to set httpOnly on cookie [{}]", cookie.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStatus(int status) {
|
public void setStatus(int status) {
|
||||||
response.setStatus(status);
|
response.setStatus(status);
|
||||||
|
|
|
@ -48,6 +48,7 @@ public class WrappedHttpServletResponseTest {
|
||||||
assertEquals(COOKIE_DOMAIN, mockResponse.getCookie(COOKIE_NAME).getDomain());
|
assertEquals(COOKIE_DOMAIN, mockResponse.getCookie(COOKIE_NAME).getDomain());
|
||||||
assertEquals(maxAge, mockResponse.getCookie(COOKIE_NAME).getMaxAge());
|
assertEquals(maxAge, mockResponse.getCookie(COOKIE_NAME).getMaxAge());
|
||||||
assertEquals(COOKIE_VALUE, mockResponse.getCookie(COOKIE_NAME).getValue());
|
assertEquals(COOKIE_VALUE, mockResponse.getCookie(COOKIE_NAME).getValue());
|
||||||
|
assertEquals(true, mockResponse.getCookie(COOKIE_NAME).isHttpOnly());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue