Search code examples
javasecurityjettyjetty-9

HashLoginService and Jetty9


I am trying to set a username/password combination programatically:

    HashLoginService loginService = new HashLoginService();
    loginService.setName("myrealm");

    Credential credential = new Password("password");

    AbstractLoginService.UserPrincipal principal =
            new AbstractLoginService.UserPrincipal("username",credential);

    Subject subject = new Subject();
    subject.getPrincipals().add(principal);
    subject.getPrivateCredentials().add(credential);


    loginService.getIdentityService().newUserIdentity(
            subject,
            principal,
            new String[]{"users"});

Yet I am getting 401. What do I miss here? Used to be able to do simple

  loginService.putUser("username",
            new Password("password"),
            new String[]{"users"});

but not with jetty 9.

Would appreciate some guidance. Thank you!


Solution

  • Use the following code to resolve the issue you have, tested against jetty version 9.4.8:

    HashLoginService loginService = new HashLoginService("MyRealm");
    
    UserStore userStore = new UserStore();
    userStore.addUser(userName, new Password("password"), new String[] { "users"});
    
    loginService.setUserStore(userStore);