HashLoginService和Jetty9

时间:2017-05-30 13:28:14

标签: java security jetty jetty-9

我正在尝试以编程方式设置用户名/密码组合:

    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"});

然而我得到401.我在这里想念什么? 曾经能够做到简单

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

但不适用于码头9。

会感激一些指导。谢谢!

1 个答案:

答案 0 :(得分:3)

使用以下代码解决您遇到的问题,针对jetty版本9.4.8进行测试:

HashLoginService loginService = new HashLoginService("MyRealm");

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

loginService.setUserStore(userStore);