Skip to main content

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

Alan,

You first set the ConstraintSecurityHandler on the Server, then replaced it with the ContextHandler.  But looking at your code, I don't see the purpose of the ContextHandler. So you should just do:

* server.setHandler(security);
* security.setHandler(resource);

cheers
Jan

On 7 September 2016 at 12:49, Alan Nexus <alan.nexus.6@xxxxxxxxx> wrote:
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);





_______________________________________________
jetty-dev mailing list
jetty-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-dev



--
Jan Bartel <janb@xxxxxxxxxxx>
www.webtide.com
Expert assistance from the creators of Jetty and CometD


Back to the top