doGetAuthenticationInfo,如果获取用户登录名和密码,跟三方做的对接,用户数据没有存在自己的数据库。
18 回复
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());
}
如果返回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
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());
}
添加回复
请先登陆