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的更多相关文章

  1. Python学习之for循环--输出1-100中的偶数和登录身份认证

    输出1-100中的偶数 效果图: 实现代码: for i in range(2,101,2): print(i,end = '\t') if(i == 34): print('\n') if (i = ...

  2. Shiro之身份认证、与spring集成(入门级)

    目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...

  3. SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)在pom.xml中添加依赖: <properties> <shi ...

  4. springboot集成shiro实现身份认证

    github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...

  5. asp.net core 登录身份认证(Cookie)

    asp.net core 2最简单的登录功能 源代码在此 创建asp.net core Web Mvc项目 配置下选项 项目目录结构 在Models文件夹下新建两个实体类 public class T ...

  6. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  7. shiro登录认证过程讲解

      先粘出登录的代码 1. 可以看到已经获取到了username和password ,为了接下来的认证过程,我们需要获取subject对象,也就是代表当前登录用户,并且要将username和passw ...

  8. spring-boot整合shiro作权限认证

    spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说  引入pom文件 <!--shiro集成spring--& ...

  9. Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

随机推荐

  1. python opencv:使用鼠标当做画笔

    鼠标事件 import cv2 events=[i for i in dir(cv2) if 'EVENT'in i] print events 双击画圆圈 import cv2 import num ...

  2. scrapy爬取阳光电影网全站资源

    说一下我的爬取过程吧 第一步: 当然是 scrapy startproject  + 名字   新建爬虫项目 第二步:  scrapy genspider -t crawl +爬虫名字+ 所爬取网站的 ...

  3. string的一些特殊点

    无论是String还是new String最终都指向了String constant pool中,只不过是String直接指向了Stringconstant pool中.而new String是在He ...

  4. Nexus升级、license安装和恢复密码

    原文链接:https://blog.csdn.net/ligang636/article/details/42386639 一.Nexus系列物理硬件1.1 Nexus 7010 1.2 Nexus ...

  5. 小程序云函数调用webservice接口

    https://www.jianshu.com/p/2692e56251ac 小程序最近新出来了云开发能力,主要依赖了node.js(运行在服务器上的js),可以让我们在没有服务器的情况下,使用云开发 ...

  6. Update(Stage4):spark_rdd算子:第2节 RDD_action算子_分区_缓存:算子和分区

    一.reduce和reduceByKey: 二.:RDD 的算子总结 RDD 的算子大部分都会生成一些专用的 RDD map, flatMap, filter 等算子会生成 MapPartitions ...

  7. [原]Java工程打包注意事项

    注意事项(持续增加...): 如果Java工程中用到了注解,在用eclipse打jar包时需要注意一下,勾上“Add directory entries”,否则注解的类会注册不上

  8. 《程序之美系列(套装共6册)》[美]斯宾耐立思 等 (作者) epub+mobi+azw3

    <架构之美>内容包括:facebook的架构如何建立在以数据为中心的应用生态系统之上.xen的创新架构对操作系统未来的影响.kde项目的社群过程如何让软件的架构从粗略的草图成为漂亮的系统. ...

  9. redhat 7.6 流量监控命令、软件(2) iftop 监控网络IP实时流量

    1.安装iftop,先要安装flex.bison.libpcap编译安装 解压红箭头的两个文件 tar  -zxvpf  iftop-0.16.tar.gz tar  -zxvpf   libpcap ...

  10. toPlainString() 和 toString()(转载)

    函数 toPlainString() 和 toString() 对于 BigDecimal b ; (b=(0.4321)^ 20) String s = b.toPlainString() ; Sy ...