2011-12-28 18 views
2

私はセキュリティのためにTapestry5とapache shiroを使用しています。 データベーステーブルからユーザーを認証することができません。tapestry 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()); 
     } 

} 

答えて

4

Shiro's javadocより良い回答はありません。 doGetAuthenticationInfo()はAuthenticationInfoを返します。 SimpleAuthenticationInfoはAuthenticationInfoの実装です。 Subject "javadocの状態として、単一のアプリケーションユーザの状態とセキュリティ操作を表します"ので、ここではテーマを設定しませんが、フレームワークはリクエストごとに繰り返し設定します。 (シンプル)AuthenticationInfoの目的は、「認証/ログインプロセスに関連するアカウント情報を保存したサブジェクト(別名ユーザーの)」を表すことです。レルムの責任はAuthenticationInfo(ユーザーが見つかった場合)を作成し、CredentialsMatcherがAuthenticationTokenとAuthenticationInfoを比較して、指定された資格情報が有効かどうかを判断することです。

どのように「つぶれている」のか説明しませんが、findByUsername()が適切なUserAccountを返すと仮定すると、正しいCredentialsMatcherが設定されていない可能性があります。おそらく、あなたはset a HashedCredentialsMatcher to your realmにする必要があります。

関連する問題