Grails:ShiroSecurity - 没有UsernamePasswordToken的用户登录 [英] Grails: ShiroSecurity - log in user without UsernamePasswordToken

查看:182
本文介绍了Grails:ShiroSecurity - 没有UsernamePasswordToken的用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

验证用户的通常方式是调用:

SecurityUtils.subject.login(新的UsernamePasswordToken(params.username,params.password))



但是,如果我想以自动方式记录他,而不必输入用户名和密码呢?我在userService中创建了这样的方法:

$ p $ def logIn(User user){
Object userIdentity = user.email
String realmName =ShiroDbRealm;
PrincipalCollection principals = new SimplePrincipalCollection(userIdentity,realmName);
Subject subject = new Subject.Builder()。principals(principals).buildSubject();
ThreadContext.bind(subject)
}

但这不起作用,任何提示?

解决方案

我设法解决了这个问题。我创建了自己的实现了AuthenticationToken接口的类:

 类UsernameToken实现了AuthenticationToken {

专用用户名;

公共UsernameToken(字符串用户名){
this.username = username;
}

public String getPrincipal(){
return username;
}
public String getCredentials(){
return username;
}
}

并创建了新的Realm,它看起来与默认的一个,但没有密码验证。现在我可以使用

$ p $ SecurityUtils.subject.login(新的UsernameToken(user.username))


The usual way of authenticating user is to invoke:

SecurityUtils.subject.login(new UsernamePasswordToken(params.username, params.password))

However, what if I would like to log him in automagically, without necessity of typing username and password? I have created method in userService like this:

   def logIn(User user){
    Object userIdentity = user.email
    String realmName = "ShiroDbRealm";
    PrincipalCollection principals = new SimplePrincipalCollection(userIdentity, realmName);
    Subject subject = new Subject.Builder().principals(principals).buildSubject();
    ThreadContext.bind(subject)
   }

But this does not work, any hints?

解决方案

I have managed to solve the issue. I have created my own class that implements AuthenticationToken interface:

class UsernameToken implements AuthenticationToken{

  private username;

  public UsernameToken(String username) {
    this.username = username;
  }

  public String getPrincipal() {
    return username;
  }
  public String getCredentials() {
    return username;
  }
}

and created new Realm which looks exactly the same as the default one but without password verification. Now I can use

SecurityUtils.subject.login(new UsernameToken(user.username))

这篇关于Grails:ShiroSecurity - 没有UsernamePasswordToken的用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆