Undertow refactor (#2462)
This commit is contained in:
parent
82c8d88886
commit
84cbc580e5
@ -1,19 +1,19 @@
|
||||
package com.baeldung.undertow.ftp;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import io.undertow.server.handlers.resource.PathResourceManager;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static io.undertow.Handlers.resource;
|
||||
|
||||
public class FileServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost")
|
||||
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
|
||||
.setDirectoryListingEnabled(true))
|
||||
.build();
|
||||
.setHandler(resource(new PathResourceManager(Paths.get(System.getProperty("user.home")), 100))
|
||||
.setDirectoryListingEnabled(true))
|
||||
.build();
|
||||
server.start();
|
||||
}
|
||||
|
||||
|
@ -54,12 +54,7 @@ public class CustomIdentityManager implements IdentityManager {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Principal principal = new Principal() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return id;
|
||||
}
|
||||
};
|
||||
private final Principal principal = () -> id;
|
||||
|
||||
@Override
|
||||
public Principal getPrincipal() {
|
||||
|
@ -1,10 +1,5 @@
|
||||
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.io.IoCallback;
|
||||
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.HttpServerExchange;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SecureServer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -28,16 +28,16 @@ public class SecureServer {
|
||||
|
||||
final IdentityManager idm = new CustomIdentityManager(users);
|
||||
|
||||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity((exchange) -> {
|
||||
setExchange(exchange);
|
||||
}, idm)).build();
|
||||
Undertow server = Undertow.builder()
|
||||
.addHttpListener(8080, "localhost")
|
||||
.setHandler(addSecurity(SecureServer::setExchange, idm)).build();
|
||||
|
||||
server.start();
|
||||
}
|
||||
|
||||
private static void setExchange(HttpServerExchange exchange) {
|
||||
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) {
|
||||
@ -45,7 +45,7 @@ public class SecureServer {
|
||||
handler = new AuthenticationCallHandler(handler);
|
||||
handler = new AuthenticationConstraintHandler(handler);
|
||||
final List<AuthenticationMechanism> mechanisms = Collections
|
||||
.<AuthenticationMechanism> singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
|
||||
.singletonList(new BasicAuthenticationMechanism("Baeldung_Realm"));
|
||||
handler = new AuthenticationMechanismsHandler(handler, mechanisms);
|
||||
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
|
||||
return handler;
|
||||
|
Loading…
x
Reference in New Issue
Block a user