//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;
}