Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-dev] Using Jetty 9.3.11.v20160721

I'm trying to add basic authentication to a resource handler I created and the following is not working.  Can anyone provide me a current working example please?
Thanks -Alan

                    // Trying to create a basic authentication k/v scheme for a resource handler
            HashLoginService loginService = new HashLoginService("MyRealm");
            loginService.putUser("username1", new Password("password1"), new String[]{"user1"});
            loginService.putUser("username2", new Password("password2"), new String[]{"user2"});
            loginService.putUser("username3", new Password("password3"), new String[]{"user3"});
            server.addBean(loginService);

            ConstraintSecurityHandler security = new ConstraintSecurityHandler();
            server.setHandler(security);

            Constraint constraint = new Constraint();
            constraint.setName(Constraint.__BASIC_AUTH);
            constraint.setAuthenticate(true);
            constraint.setRoles(new String[]{"user", "admin"});

            ConstraintMapping mapping = new ConstraintMapping();
            mapping.setPathSpec("/*");
            mapping.setConstraint(constraint);

            security.setConstraintMappings(Collections.singletonList(mapping));
            security.setAuthenticator(new BasicAuthenticator());
            security.setLoginService(loginService);

            ResourceHandler resource = new ResourceHandler();
            resource.setDirectoriesListed(true);
            resource.setResourceBase("/root");

            ContextHandler contextHandler = new ContextHandler();
            contextHandler.setContextPath("/root");
            contextHandler.setHandler(resource);
            server.setHandler(contextHandler);
            
            // This did not work ...
            security.setHandler(contextHandler);





Back to the top