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;
}
标签:TUser,queryWrapper,String,登录,获取,authenticationToken,new,shiro 来源: https://www.cnblogs.com/yydown/p/15489435.html