【问题标题】:tapestry shiro security authentication挂毯shiro安全认证
【发布时间】:2012-01-29 06:51:57
【问题描述】:

我使用 Tapestry5 和 apache shiro 来保证安全。 我一直无法从数据库表中验证用户身份。

在doGetAuthenticationInfo()这个函数中我们不需要设置Subject吗?

SimpleAuthenticationInfo 的用途是什么?

package com.kids.crm.services;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.SimpleByteSource;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.springframework.beans.factory.annotation.Autowired;

import com.kids.crm.dao.DatabaseDao;
import com.kids.crm.dao.UserAccountDao;
import com.kids.crm.dao.impl.UserAccountDaoImpl;
import com.kids.crm.db.Role;
import com.kids.crm.db.UserAccount;


public class UserRealm extends AuthorizingRealm {
        @Inject UserAccountDao userAccountDao;
        public UserRealm() {
                setName("localaccounts");
                setAuthenticationTokenClass(UsernamePasswordToken.class);
        }

        private UserAccount findByUsername(String userName) {
                return (UserAccount) userAccountDao.getUserByUserName(userName);
        }

        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
                //Subject currentUser = SecurityUtils.getSubject();
                UsernamePasswordToken upToken = (UsernamePasswordToken) token;

                        String username = upToken.getUsername();
                        upToken.setRememberMe(true);
                        // Null username is invalid
                        if (username == null) { throw new AccountException("Null usernames are not allowed by this realm."); }
                        UserAccount user = findByUsername(username);

                return new SimpleAuthenticationInfo(username, user.getEncodedPassword(), new SimpleByteSource(user.getPasswordSalt()), getName());
        }

} 

【问题讨论】:

    标签: tapestry shiro


    【解决方案1】:

    没有比Shiro's javadoc 更好的答案来源了。 doGetAuthenticationInfo() 返回一个 AuthenticationInfo。 SimpleAuthenticationInfo 是 AuthenticationInfo 的实现。 Subject“代表单个应用程序用户的状态和安全操作”正如javadoc所说,所以不,我们不在这里设置主题,但框架会为每个请求重复设置它。 (Simple)AuthenticationInfo 的目的是表示“仅与身份验证/登录过程相关的主题(又名用户)存储的帐户信息”。领域的职责是创建一个 AuthenticationInfo(如果找到用户),然后 CredentialsMatcher 将 AuthenticationToken 与 AuthenticationInfo 进行比较,以确定给定的凭据是否有效。

    您没有解释您是如何“被卡住”的,但假设您的 findByUsername() 返回了适当的 UserAccount,您可能没有配置正确的 CredentialsMatcher。也许你需要set a HashedCredentialsMatcher to your realm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      相关资源
      最近更新 更多