私はgrailsのApache Shiroプラグインを使ってユーザーの認証を処理しています。パスワードでログインApache ShiroでハッシュGrails
管理者が特定のユーザーのためにログインできるシステムの管理部分に機能を追加したいと思います。
私はadminのための新しい複雑なインターフェイスを作成しないでください。
私は彼のパスワードを知らないのに、彼のパスワードハッシュだけでユーザーとしてログインする方法?
私はgrailsのApache Shiroプラグインを使ってユーザーの認証を処理しています。パスワードでログインApache ShiroでハッシュGrails
管理者が特定のユーザーのためにログインできるシステムの管理部分に機能を追加したいと思います。
私はadminのための新しい複雑なインターフェイスを作成しないでください。
私は彼のパスワードを知らないのに、彼のパスワードハッシュだけでユーザーとしてログインする方法?
私は最終的にそれを解決したが、私は私の解決策のセキュリティについてわからないので、私はあなたがそれにコメントしたいと思います。
私はadminコントローラにある新しいアクションsignInを作成しました。このコントローラは管理者だけがアクセスでき、誰もアクセスできません。アクションは次のようになります。
def signIn(String username) {
def authToken = new UsernamePasswordToken(username, params.password as String)
// Specific setting for administrator signIn
//
params.rememberMe = false
params.targetUri = '/'
authToken.host = 'admin'
// If a controller redirected to this page, redirect back
// to it. Otherwise redirect to the root URI.
def targetUri = params.targetUri ?: "/index/index"
// Handle requests saved by Shiro filters.
SavedRequest savedRequest = WebUtils.getSavedRequest(request)
if (savedRequest) {
targetUri = savedRequest.requestURI - request.contextPath
if (savedRequest.queryString) targetUri = targetUri + '?' + savedRequest.queryString
}
try{
// Perform the actual login. An AuthenticationException
// will be thrown if the username is unrecognised or the
// password is incorrect.
SecurityUtils.subject
SecurityUtils.subject.login(authToken)
log.info "Redirecting to ${targetUri}."
redirect(uri: targetUri)
}
catch (AuthenticationException ex){
// Authentication failed, so display the appropriate message
// on the login page.
log.info "Authentication failure for user ${params.username}."
flash.message = message(code: "login.failed")
// Keep the username and "remember me" setting so that the
// user doesn't have to enter them again.
def m = [ username: params.username ]
// Remember the target URI too.
if (params.targetUri) {
m["targetUri"] = params.targetUri
}
// Now redirect back to the login page.
redirect(action: "login", params: m)
}
}
また、DbShiroRealmも少し修正しました。
def authenticate(authToken) {
log.info "Attempting to authenticate ${authToken.username} in DB realm..."
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.
def user = User.findByUsername(username)
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.password, "ShiroDbRealm")
if(!(authToken.host == 'admin')) { // skips password authentication
if (!credentialMatcher.doCredentialsMatch(authToken, account)) {
log.info "Invalid password (DB realm)"
throw new IncorrectCredentialsException("Invalid password for user ${username}")
}
}
return account
}
私がログインしている情報を運ぶためにUsernamePasswordTokenでホストフィールドを使用していました。
UsernamePasswordTokenをオーバーライドして、パスワード認証をスキップするための追加のブールフィールドを追加する方が適切であろう。
これは、セキュリティ上の問題を伴う恐ろしい考えのようです。代わりに、ログインした管理者が一時的にユーザーとしてログインできるようにすることを検討する必要があります。これらは、ユーザーとしてログインする方法であなたを助けることができ
はプログラム的Grails - ShiroSecurity - manually login userとGrails: ShiroSecurity - log in user without UsernamePasswordToken