Search in sources :

Example 16 with SecurityContext

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);
}
Also used : Account(io.undertow.security.idm.Account) SecurityContext(io.undertow.security.api.SecurityContext)

Example 17 with SecurityContext

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();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) IdentityManager(io.undertow.security.idm.IdentityManager) HashMap(java.util.HashMap) SecurityContext(io.undertow.security.api.SecurityContext) Undertow(io.undertow.Undertow)

Example 18 with SecurityContext

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();
        }
    }
}
Also used : SecurityContext(io.undertow.security.api.SecurityContext) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Aggregations

SecurityContext (io.undertow.security.api.SecurityContext)18 Account (io.undertow.security.idm.Account)4 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)4 HttpServerExchange (io.undertow.server.HttpServerExchange)3 HttpSession (javax.servlet.http.HttpSession)2 Undertow (io.undertow.Undertow)1 AuthenticatedSessionManager (io.undertow.security.api.AuthenticatedSessionManager)1 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)1 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)1 AuthenticationMechanismContext (io.undertow.security.api.AuthenticationMechanismContext)1 IdentityManager (io.undertow.security.idm.IdentityManager)1 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)1 HttpHandler (io.undertow.server.HttpHandler)1 Session (io.undertow.server.session.Session)1 SessionConfig (io.undertow.server.session.SessionConfig)1 SessionManager (io.undertow.server.session.SessionManager)1 AuthorizationManager (io.undertow.servlet.api.AuthorizationManager)1 Deployment (io.undertow.servlet.api.Deployment)1 SingleConstraintMatch (io.undertow.servlet.api.SingleConstraintMatch)1 ServletChain (io.undertow.servlet.handlers.ServletChain)1