shiro 获取已经登录的用户


//shiro 获取登录的用户信息
TUser user = (TUser)SecurityUtils.getSubject().getPrincipal();

//前提是需要的在登录的认证的方法中存入已经登录的用户如下

 

 代码如下

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("-------身份认证方法--------");
String userName = (String) authenticationToken.getPrincipal();
String userPwd = new String((char[]) authenticationToken.getCredentials());
QueryWrapper<TUser> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("username",userName);
TUser one = userService.getOne(queryWrapper);//根据用户名从数据库获取密码
if(one==null){
throw new AccountException("用户不正确");
}
if (!userPwd.equals(one.getPassword() )) {
throw new IncorrectCredentialsException("密码不正确");
}
//用户存入shiro 缓存
SimpleAuthenticationInfo info =new SimpleAuthenticationInfo(one, one.getPassword(),getName());
return info;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM