use of io.undertow.server.session.SessionAttachmentHandler in project undertow by undertow-io.
the class SsoTestCase method setup.
@BeforeClass
public static void setup() {
final SingleSignOnAuthenticationMechanism sso = new SingleSignOnAuthenticationMechanism(new InMemorySingleSignOnManager());
final PathHandler path = new PathHandler();
HttpHandler current = new ResponseHandler();
current = new AuthenticationCallHandler(current);
current = new AuthenticationConstraintHandler(current);
List<AuthenticationMechanism> mechs = new ArrayList<>();
mechs.add(sso);
mechs.add(new BasicAuthenticationMechanism("Test Realm"));
current = new AuthenticationMechanismsHandler(current, mechs);
current = new NotificationReceiverHandler(current, Collections.<NotificationReceiver>singleton(auditReceiver));
current = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, current);
path.addPrefixPath("/test1", current);
current = new ResponseHandler();
current = new AuthenticationCallHandler(current);
current = new AuthenticationConstraintHandler(current);
mechs = new ArrayList<>();
mechs.add(sso);
mechs.add(new FormAuthenticationMechanism("form", "/login", "/error"));
current = new AuthenticationMechanismsHandler(current, mechs);
current = new NotificationReceiverHandler(current, Collections.<NotificationReceiver>singleton(auditReceiver));
current = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, current);
path.addPrefixPath("/test2", current);
path.addPrefixPath("/login", new ResponseCodeHandler(StatusCodes.UNAUTHORIZED));
DefaultServer.setRootHandler(new SessionAttachmentHandler(path, new InMemorySessionManager(""), new SessionCookieConfig()));
}
use of io.undertow.server.session.SessionAttachmentHandler in project undertow by undertow-io.
the class SSLSessionTestCase method testSslSession.
@Test
public void testSslSession() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
InMemorySessionManager sessionManager = new InMemorySessionManager("");
final SslSessionConfig sessionConfig = new SslSessionConfig(sessionManager);
final SessionAttachmentHandler handler = new SessionAttachmentHandler(sessionManager, sessionConfig).setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
Session session = manager.getSession(exchange, sessionConfig);
if (session == null) {
session = manager.createSession(exchange, sessionConfig);
session.setAttribute(COUNT, 0);
}
Integer count = (Integer) session.getAttribute(COUNT);
exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
session.setAttribute(COUNT, ++count);
}
});
DefaultServer.startSSLServer();
client.setSSLContext(DefaultServer.getClientSSLContext());
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
Header[] header = result.getHeaders(COUNT);
Assert.assertEquals("0", header[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
header = result.getHeaders(COUNT);
Assert.assertEquals("1", header[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
header = result.getHeaders(COUNT);
Assert.assertEquals("2", header[0].getValue());
Assert.assertEquals(0, client.getCookieStore().getCookies().size());
} finally {
DefaultServer.stopSSLServer();
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.session.SessionAttachmentHandler in project undertow by undertow-io.
the class AbstractModClusterTestBase method createNode.
static Undertow createNode(final NodeTestConfig config) {
final Undertow.Builder builder = Undertow.builder();
final String type = config.getType();
switch(type) {
case "ajp":
builder.addAjpListener(config.getPort(), config.getHostname());
break;
case "http":
builder.addHttpListener(config.getPort(), config.getHostname());
break;
case "https":
builder.addHttpsListener(config.getPort(), config.getHostname(), DefaultServer.getServerSslContext());
break;
default:
throw new IllegalArgumentException(type);
}
final SessionCookieConfig sessionConfig = new SessionCookieConfig();
if (config.getStickySessionCookie() != null) {
sessionConfig.setCookieName(config.getStickySessionCookie());
}
final PathHandler pathHandler = path(ResponseCodeHandler.HANDLE_200).addPrefixPath("/name", new StringSendHandler(config.getJvmRoute())).addPrefixPath("/session", new SessionAttachmentHandler(new SessionTestHandler(config.getJvmRoute(), sessionConfig), new InMemorySessionManager(""), sessionConfig));
// Setup test handlers
config.setupHandlers(pathHandler);
builder.setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(jvmRoute("JSESSIONID", config.getJvmRoute(), pathHandler));
return builder.build();
}
use of io.undertow.server.session.SessionAttachmentHandler in project undertow by undertow-io.
the class InMemorySessionTestCase method inMemoryMaxSessionsTest.
@Test
public void inMemoryMaxSessionsTest() throws IOException {
TestHttpClient client1 = new TestHttpClient();
client1.setCookieStore(new BasicCookieStore());
TestHttpClient client2 = new TestHttpClient();
client2.setCookieStore(new BasicCookieStore());
try {
final SessionCookieConfig sessionConfig = new SessionCookieConfig();
final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager("", 1, true), sessionConfig);
handler.setNext(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
Session session = manager.getSession(exchange, sessionConfig);
if (session == null) {
session = manager.createSession(exchange, sessionConfig);
session.setAttribute(COUNT, 0);
}
Integer count = (Integer) session.getAttribute(COUNT);
exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
session.setAttribute(COUNT, ++count);
}
});
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
HttpResponse result = client1.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
Header[] header = result.getHeaders(COUNT);
Assert.assertEquals("0", header[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
result = client1.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
header = result.getHeaders(COUNT);
Assert.assertEquals("1", header[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
result = client2.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
header = result.getHeaders(COUNT);
Assert.assertEquals("0", header[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
result = client1.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
header = result.getHeaders(COUNT);
Assert.assertEquals("0", header[0].getValue());
} finally {
client1.getConnectionManager().shutdown();
client2.getConnectionManager().shutdown();
}
}
use of io.undertow.server.session.SessionAttachmentHandler in project undertow by undertow-io.
the class Http2Server method main.
public static void main(final String[] args) throws Exception {
String version = System.getProperty("java.version");
System.out.println("Java version " + version);
if (version.charAt(0) == '1' && Integer.parseInt(version.charAt(2) + "") < 8) {
System.out.println("This example requires Java 1.8 or later");
System.out.println("The HTTP2 spec requires certain cyphers that are not present in older JVM's");
System.out.println("See section 9.2.2 of the HTTP2 specification for details");
System.exit(1);
}
String bindAddress = System.getProperty("bind.address", "localhost");
SSLContext sslContext = createSSLContext(loadKeyStore("server.keystore"), loadKeyStore("server.truststore"));
Undertow server = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8080, bindAddress).addHttpsListener(8443, bindAddress, sslContext).setHandler(new SessionAttachmentHandler(new LearningPushHandler(100, -1, Handlers.header(predicate(secure(), resource(new PathResourceManager(Paths.get(System.getProperty("example.directory", System.getProperty("user.home"))), 100)).setDirectoryListingEnabled(true), new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().add(Headers.LOCATION, "https://" + exchange.getHostName() + ":" + (exchange.getHostPort() + 363) + exchange.getRelativePath());
exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
}
}), "x-undertow-transport", ExchangeAttributes.transportProtocol())), new InMemorySessionManager("test"), new SessionCookieConfig())).build();
server.start();
SSLContext clientSslContext = createSSLContext(loadKeyStore("client.keystore"), loadKeyStore("client.truststore"));
LoadBalancingProxyClient proxy = new LoadBalancingProxyClient().addHost(new URI("https://localhost:8443"), null, new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, clientSslContext), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).setConnectionsPerThread(20);
Undertow reverseProxy = Undertow.builder().setServerOption(UndertowOptions.ENABLE_HTTP2, true).addHttpListener(8081, bindAddress).addHttpsListener(8444, bindAddress, sslContext).setHandler(new ProxyHandler(proxy, 30000, ResponseCodeHandler.HANDLE_404)).build();
reverseProxy.start();
}
Aggregations