Shiro登录身份认证(从SecurityUtils.getSubject().login(token))到Realm的doGetAuthenticationInfo
ssm框架下,controller接收到登录请求交给Service并开始处理流程:
1.Service的login方法:
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
SysUserMapper mapper;
@Override
public LoginResult login(SysUser sysUser) {
LoginResult loginResult = new LoginResult();
try {
List<SysUser> sysUsers = mapper.selectByExample(sysUser.getUsername());
BackUserToken backUserToken=new BackUserToken(sysUser.getUsername(), sysUser.getPwd(),sysUsers);
SecurityUtils.getSubject().login(backUserToken);
loginResult.setStatus(RespMSG.STATUS_SUCCESS);
loginResult.setMsg(RespMSG.MSG_SUCCESS);
loginResult.setSysUser(sysUsers.get(0));
return loginResult;
} catch (UnknownAccountException e) {
loginResult.setStatus(RespMSG.STATUS_NON_EXISTENT);
loginResult.setMsg(RespMSG.MSG_NON_EXISTENT);
return loginResult;
} catch (AccountException e) {
e.printStackTrace();
// loginResult.setStatus(RespMSG.);
// loginResult.setMsg(RespMSG.MSG_NON_EXISTENT);
// return loginResult;
} catch (DeadlineException e) {
loginResult.setStatus(RespMSG.STATUS_OVERDUE);
loginResult.setMsg(RespMSG.MSG_OVERDUE);
return loginResult;
} catch (AccountRepeatException e) {
loginResult.setStatus(RespMSG.STATUS_REPETITION);
loginResult.setMsg(RespMSG.MSG_REPETITION);
return loginResult;
} catch (IncorrectCredentialsException e) {
loginResult.setStatus(RespMSG.STATUS_WRONG_PWD);
loginResult.setMsg(RespMSG.MSG_WRONG_PWD);
return loginResult;
}
loginResult.setMsg(RespMSG.MSG_UNKNOW);
loginResult.setStatus(RespMSG.STATUS_UNKNOW);
return loginResult;
}
}
BackUserToken集成了UsernamePasswordToken,主要是构造参数多了一个List<SysUser>.因为我需要在service中将登录用户的详细
信息返回给客户端,同时将登录用户的信息传递给Realm进行登录验证。
关键方法是:
SecurityUtils.getSubject().login(backUserToken);
其具体实现是:
2.DelegatingSubject implements Subject
public class DelegatingSubject implements Subject {
public void login(AuthenticationToken token) throws AuthenticationException {
...
Subject subject = this.securityManager.login(this, token);
...
}
}
token继续传递:
3.DefaultSecurityManager
public class DefaultSecurityManager extends SessionsSecurityManager {
public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
...
info = this.authenticate(token);
...
return loggedIn;
}
}
向下继续
4.AuthenticatingSecurityManager
public abstract class AuthenticatingSecurityManager extends RealmSecurityManager {
public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
return this.authenticator.authenticate(token);
}
}
继续向下
5.AbstractAuthenticator
public abstract class AbstractAuthenticator implements Authenticator, LogoutAware {
public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
...
info = this.doAuthenticate(token);
...
return info;
}
}
}
6.ModularRealmAuthenticator
public class ModularRealmAuthenticator extends AbstractAuthenticator {
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
this.assertRealmsConfigured();
Collection<Realm> realms = this.getRealms();
return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);
}
}
还是在ModularRealmAuthenticator中
protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
...
AuthenticationInfo info = realm.getAuthenticationInfo(token);
...
}
继续
7.AuthenticatingRealm
public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
...
info = this.doGetAuthenticationInfo(token);
...
return info;
}
}
然后进入最后一步,在继承了AuthorizingRealm的自定义的realm中进行处理。
8.自定义realm举例
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
BackUserToken token = (BackUserToken) authenticationToken;
List<SysUser> sysUsers = token.getUser();
if (sysUsers == null || sysUsers.size() == 0) {
throw new UnknownAccountException();
}
if (sysUsers.size() > 1) {
//用户重复
throw new AccountRepeatException();
}
if (用户已过期) {
if (sysUsers.get(0).getIsAbleDate() == null||sysUsers.get(0).getIsAbleDate().getTime() < new Date().getTime()) {
//用户过期(自定义异常)
throw new DeadlineException();
}
}
SysUser sysUser = sysUsers.get(0);
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
sysUser,
sysUser.getPwd(),
getName()
);
return authenticationInfo;
}
Shiro登录身份认证(从SecurityUtils.getSubject().login(token))到Realm的doGetAuthenticationInfo的更多相关文章
- Python学习之for循环--输出1-100中的偶数和登录身份认证
输出1-100中的偶数 效果图: 实现代码: for i in range(2,101,2): print(i,end = '\t') if(i == 34): print('\n') if (i = ...
- Shiro之身份认证、与spring集成(入门级)
目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...
- SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)在pom.xml中添加依赖: <properties> <shi ...
- springboot集成shiro实现身份认证
github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...
- asp.net core 登录身份认证(Cookie)
asp.net core 2最简单的登录功能 源代码在此 创建asp.net core Web Mvc项目 配置下选项 项目目录结构 在Models文件夹下新建两个实体类 public class T ...
- springboot+mybatis+shiro——登录认证和权限控制
转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...
- shiro登录认证过程讲解
先粘出登录的代码 1. 可以看到已经获取到了username和password ,为了接下来的认证过程,我们需要获取subject对象,也就是代表当前登录用户,并且要将username和passw ...
- spring-boot整合shiro作权限认证
spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说 引入pom文件 <!--shiro集成spring--& ...
- Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
随机推荐
- Linux :ls 命令
常用命令: ls:列出当前路径下的文件和目录 ls -a:列出当前路径下的所有文件和目录(包括隐藏文件和目录) ls -l:以列表方式显示文件或目录的详细信息 ls -al:可以结合使用 ls xxx ...
- 语义化标签&唯一性标签
语义化标签 em\i表示倾斜,b\strong表示加粗,但其中只有strong和em具有着重的语义 img:alt属性当图片无法显示,显示alt的文字,根本需求是为了SEO,是必须属性,alt属性长度 ...
- Unity中调用Windows窗口选择文件
1.OpenFileName数据接收类,如下: using UnityEngine; using System.Collections; using System; using System.Runt ...
- 后端——框架——持久层框架——Mybatis——补充——pageHelper(分页)插件
Pagehelper插件的知识点大致可以分为三个部分 搭建环境,引入jar包,配置. 使用方式,只需要记住一种即可.类似于在写SQL语句中,可以left join,也可以right join,它们实现 ...
- class A<T>where T:new()是什么意思
这是C#泛型类声明的语法class A<T> 表示 A类接受某一种类型,泛型类型为T,需要运行时传入where表明了对类型变量T的约束关系.where T:new()指明了创建T的实例时应 ...
- Shell函数!
1.作用:将命令序列按格式写在一起,可方便重复使用命令序列2.Shell 函数定义格式:[ function ] 函数名(){命令序列[ return x ]}3.调用函数的方法:函数名 [ 参数 1 ...
- jmeter download historyList
https://archive.apache.org/dist/jmeter/binaries/ 反馈,问题和评论应发送到Apache JMeter Users 邮件列表. 有关更多信息, 请访问Ap ...
- centos7搭建hadoop2.10完全分布式
本篇介绍在centos7中大家hadoop2.10完全分布式,首先准备4台机器:1台nn(namenode);3台dn(datanode) IP hostname 进程 192.168.30.141 ...
- Python 基础之正则之一 单字符,多字符匹配及开头结尾匹配
一.正则表达式之单个字符匹配 格式:lst = re.findall(正则表达式,要匹配的字符串)预定义字符集 匹配内容 .匹配任意字符,除了换行符\n \d匹配数字 \D匹配非数字 \w匹配字母或数 ...
- el-dialog 如何自定义大小样式
使用属性:custom-class 然后在css中根据这个类型编写指定的样式即可(比如宽高) 举例: