Undertow refactor (#2462)

This commit is contained in:
Grzegorz Piwowarek 2017-08-21 14:52:31 +02:00 committed by GitHub
parent 82c8d88886
commit 84cbc580e5
3 changed files with 16 additions and 21 deletions

View File

@ -1,19 +1,19 @@
package com.baeldung.undertow.ftp; package com.baeldung.undertow.ftp;
import java.nio.file.Paths;
import io.undertow.Undertow; import io.undertow.Undertow;
import io.undertow.server.handlers.resource.PathResourceManager; import io.undertow.server.handlers.resource.PathResourceManager;
import java.nio.file.Paths;
import static io.undertow.Handlers.resource; import static io.undertow.Handlers.resource;
public class FileServer { public class FileServer {
public static void main(String[] args) { public static void main(String[] args) {
Undertow server = Undertow.builder().addHttpListener(8080, "localhost") Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100)) .setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
.setDirectoryListingEnabled(true)) .setDirectoryListingEnabled(true))
.build(); .build();
server.start(); server.start();
} }

View File

@ -54,12 +54,7 @@ public class CustomIdentityManager implements IdentityManager {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Principal principal = new Principal() { private final Principal principal = () -> id;
@Override
public String getName() {
return id;
}
};
@Override @Override
public Principal getPrincipal() { public Principal getPrincipal() {

View File

@ -1,10 +1,5 @@
package com.baeldung.undertow.secure; package com.baeldung.undertow.secure;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.undertow.Undertow; import io.undertow.Undertow;
import io.undertow.io.IoCallback; import io.undertow.io.IoCallback;
import io.undertow.security.api.AuthenticationMechanism; import io.undertow.security.api.AuthenticationMechanism;
@ -19,6 +14,11 @@ import io.undertow.security.impl.BasicAuthenticationMechanism;
import io.undertow.server.HttpHandler; import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpServerExchange;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SecureServer { public class SecureServer {
public static void main(String[] args) { public static void main(String[] args) {
@ -28,16 +28,16 @@ public class SecureServer {
final IdentityManager idm = new CustomIdentityManager(users); final IdentityManager idm = new CustomIdentityManager(users);
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity((exchange) -> { Undertow server = Undertow.builder()
setExchange(exchange); .addHttpListener(8080, "localhost")
}, idm)).build(); .setHandler(addSecurity(SecureServer::setExchange, idm)).build();
server.start(); server.start();
} }
private static void setExchange(HttpServerExchange exchange) { private static void setExchange(HttpServerExchange exchange) {
final SecurityContext context = exchange.getSecurityContext(); final SecurityContext context = exchange.getSecurityContext();
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(),IoCallback.END_EXCHANGE); exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(), IoCallback.END_EXCHANGE);
} }
private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) { private static HttpHandler addSecurity(final HttpHandler toWrap, final IdentityManager identityManager) {
@ -45,7 +45,7 @@ public class SecureServer {
handler = new AuthenticationCallHandler(handler); handler = new AuthenticationCallHandler(handler);
handler = new AuthenticationConstraintHandler(handler); handler = new AuthenticationConstraintHandler(handler);
final List<AuthenticationMechanism> mechanisms = Collections final List<AuthenticationMechanism> mechanisms = Collections
.<AuthenticationMechanism> singletonList(new BasicAuthenticationMechanism("Baeldung_Realm")); .singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
handler = new AuthenticationMechanismsHandler(handler, mechanisms); handler = new AuthenticationMechanismsHandler(handler, mechanisms);
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler); handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
return handler; return handler;