NutzCN Logo
问答 doGetAuthenticationInfo 获取用户登录名密码
发布于 2411天前 作者 qq_32ee9e89 12037 次浏览 复制 上一个帖子 下一个帖子
标签: shiro

doGetAuthenticationInfo,如果获取用户登录名和密码,跟三方做的对接,用户数据没有存在自己的数据库。

18 回复

doGetAuthenticationInfo是什么东西

自定义Realm重写的doGetAuthenticationInfo方法

取决于token类

如果是oauth之类的, SimpleShiroToken是最佳选择, 不涉及密码

SimpleShiroToken获取不到密码吧

难道你需要把密码发送给第三方??

return 的info需要获取用户名和密码,直接返回一个不带参数的SimpleAuthenticationInfo是不行的

@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SimpleShiroToken upToken = (SimpleShiroToken) token;
        System.out.println("pppp"+upToken.getCredentials());
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("username","password" .toCharArray(),getName());
        return info;
    }

nutzcn的写法

	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		SimpleShiroToken upToken = (SimpleShiroToken) token;

		User user = dao().fetch(User.class, (Long)upToken.getPrincipal());
		if (user == null)
			return null;
		if (user.isLocked())
			throw new LockedAccountException("Account [" + user.getName() + "] is locked.");
		return new SimpleAccount(user.getId(), user.getPassword(), getName());
	}

三方提供的登录,我没有自己的库,没法从数据库里获取到User对象。

return new SimpleAccount(user.getId(), new char[0], getName());

return new SimpleAccount(((SimpleShiroToken)token).getPrincipal(), "", getName());

程序是可以了,能问一下这是什么意思吗?

啥的什么意思??? 语句??

AuthenticationInfo有两个实现类,SimpleAccount和SimpleAuthenticationInfo,不知道这两个类分别的作用区别。

如果返回new SimpleAccount(((SimpleShiroToken)token).getPrincipal(), "", getName());
通过 SecurityUtils.getSubject().getPrincipal()获取到的信息为:
org.apache.shiro.authc.UsernamePasswordToken - XXX, rememberMe=false
我想只获取XXX还得自己做字符串截取吗?
如果返回的是new SimpleAuthenticationInfo(WebConstant.userName, WebConstant.password .toCharArray(),getName());
通过 SecurityUtils.getSubject().getPrincipal()获取到的信息就正确为:XXX

那是你构建SimpleShiroToken对象的时候传错东西了

UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(new SimpleShiroToken(token));

@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SimpleShiroToken upToken = (SimpleShiroToken) token;
        System.out.println(upToken.getPrincipal()+"--------------00000000000------------"+getName());
        return new SimpleAuthenticationInfo(upToken.getPrincipal(), new char[0], getName());
    }
new SimpleShiroToken(dao.fetch(User.class, name).getId());

添加回复
请先登陆
回到顶部