私は比較的簡単なことをしようとしています:ユーザーを手動でログインします。FacebookGraphプラグインを使用してFacebookに接続しています。ユーザーがFacebook経由でログインした場合、私は彼のIDを取得し、ShiroSecurityで彼を認証したいと思います。もちろん、そのような些細なこと
session.user = user
動作しません。私はウィキでコードを見つけました、それはトリックをするはずです:
Object userIdentity = user.email
String realmName = "username";
PrincipalCollection principals = new SimplePrincipalCollection(userIdentity, realmName);
Subject subject = new Subject.Builder().principals(principals).buildSubject();
ただし、動作しません。ShiroSubjectがnullであるというlog.debugメッセージでauth/loginにリダイレクトされます。これは、サービスでこのコードを呼び出したためかもしれません。これを機能させる方法はありますか?
アップデート:
def authenticate(authToken) {
log.info "Attempting to authenticate ${authToken.username} in DB realm..."+authToken.encodeAsJSON()
def username = authToken.username
// Null username is invalid
if (username == null) {
throw new AccountException("Null usernames are not allowed by this realm.")
}
// Get the user with the given username. If the user is not
// found, then they don't have an account and we throw an
// exception.
log.debug "reached this point2"
def user = ShiroUser.findByUsername(username)
log.debug "reached this point"
if (!user) {
throw new UnknownAccountException("No account found for user [${username}]")
}
log.info "Found user '${user.username}' in DB"
// Now check the user's password against the hashed value stored
// in the database.
def account = new SimpleAccount(username, user.passwordHash, "ShiroDbRealm")
if (!credentialMatcher.doCredentialsMatch(authToken, account)) {
log.info "Invalid password (DB realm)"
throw new IncorrectCredentialsException("Invalid password for user '${username}'")
}
return account
}