use of io.undertow.security.api.SecurityContext in project undertow by undertow-io.
the class AsyncWebSocketHttpServerExchange method isUserInRole.
@Override
public boolean isUserInRole(String role) {
SecurityContext sc = exchange.getSecurityContext();
if (sc == null) {
return false;
}
Account authenticatedAccount = sc.getAuthenticatedAccount();
if (authenticatedAccount == null) {
return false;
}
return authenticatedAccount.getRoles().contains(role);
}
use of io.undertow.security.api.SecurityContext in project undertow by undertow-io.
the class BasicAuthServer method main.
public static void main(final String[] args) {
System.out.println("You can login with the following credentials:");
System.out.println("User: userOne Password: passwordOne");
System.out.println("User: userTwo Password: passwordTwo");
final Map<String, char[]> users = new HashMap<>(2);
users.put("userOne", "passwordOne".toCharArray());
users.put("userTwo", "passwordTwo".toCharArray());
final IdentityManager identityManager = new MapIdentityManager(users);
Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(addSecurity(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final SecurityContext context = exchange.getSecurityContext();
exchange.getResponseSender().send("Hello " + context.getAuthenticatedAccount().getPrincipal().getName(), IoCallback.END_EXCHANGE);
}
}, identityManager)).build();
server.start();
}
use of io.undertow.security.api.SecurityContext in project undertow by undertow-io.
the class ServletAuthenticationCallHandler method handleRequest.
/**
* Only allow the request through if successfully authenticated or if authentication is not required.
*
* @see io.undertow.server.HttpHandler#handleRequest(io.undertow.server.HttpServerExchange)
*/
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
SecurityContext context = exchange.getSecurityContext();
if (context.authenticate()) {
if (!exchange.isComplete()) {
next.handleRequest(exchange);
}
} else {
if (exchange.getStatusCode() >= StatusCodes.BAD_REQUEST && !exchange.isComplete()) {
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
src.getOriginalResponse().sendError(exchange.getStatusCode());
} else {
exchange.endExchange();
}
}
}
Aggregations