【问题标题】:How Do I Authenticate a User With Shiro?如何使用 Shiro 对用户进行身份验证?
【发布时间】:2012-08-26 09:33:52
【问题描述】:

我已经一遍又一遍地讨论如何让用户使用 Shiro 登录,但似乎仍然缺少一个重要的部分:shiro 如何根据存储的用户名和密码验证给定的用户名和密码?我想出的最多的是It is each Realm's responsibility to match submitted credentials with those stored in the Realm's backing data storefrom here。但是这是怎么做到的呢?

以下是我尝试过的,但结果仍然是无效的身份验证。

登录控制器

@RequestMapping(value = "/login.htm", method = RequestMethod.POST)
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd, BindException errors) throws Exception {

    LoginCommand command = (LoginCommand) cmd;
    UsernamePasswordToken token = new UsernamePasswordToken(command.getUsername(), command.getPassword());
    System.out.println("onSubmit");
    System.out.println(token.getUsername());
    System.out.println(token.getPassword());

    try
    {
        SecurityUtils.getSubject().login(token);
    } catch (AuthenticationException e) {
        errors.reject("error.invalidLogin", "The username or password was not correct.");
    }

    if (errors.hasErrors()) {
        return showForm(request, response, errors);
    } else {
        return new ModelAndView("accessTest");
    }
}

领域

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

    System.out.println("doGetAuthenticationInfo");
    System.out.println(user.getUsername());
    System.out.println(user.getPassword());

    // user is a test object in place of a database
    if( user != null ) {
        return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());
    } else {
        return null;
    }
}

【问题讨论】:

  • 你能分享输出吗?我的第一个猜测是 token.getPassword() != user.getPassword()。
  • 是的。我已经测试了所有的输出,所有的输出都是相等的。

标签: shiro


【解决方案1】:

找到了答案。这是一个愚蠢的。我复制了一些示例代码,他们将凭据匹配器设置为 HashedCredentialsMatcher。我没有做任何散列,所以它不起作用。删除了 setCredentialsMatcher 并且它起作用了。

【讨论】:

  • 很抱歉打扰您这么久。您能否发布您领域的完整课程。我找不到任何解释类名应该是什么的例子,所以我可以在我的数据库中对用户进行身份验证:(
  • 对不起@chrisloughnane,那段代码早已不复存在,所以我恐怕帮不了你。
猜你喜欢
  • 2013-06-30
  • 2015-09-28
  • 2017-07-23
  • 2012-10-05
  • 2014-07-09
  • 2017-02-20
  • 2021-07-26
  • 2019-05-02
  • 2013-01-19
相关资源
最近更新 更多