shiro框架源码解析与改造(九)---FormAuthenticationFilter

FormAuthenticationFilter是登陆已经认证的关键过滤器。

父类是AuthenticationFilter
由onPreHandle方法为起点,先判断当前用户是否已经登陆,然后当前账号是否是当前用户最后登陆的,如果不是,则拒绝,onAccessDenied,重定向到登陆界面
auth认证,

  protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        Subject subject=this.getSubject(request,response);
        return subject.isAuthenticated() && isCurrentAccount(subject) ||onAccessDenied(request,response);
    }

登陆走下面的方法,如果当前访问的路径是登陆路径,则执行登陆流程

  protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        if (this.isLoginRequest(request, response)){
            return this.executeLogin(request, response);
        }
        return super.onPreHandle(request, response, mappedValue);
    }
public boolean executeLogin(ServletRequest request, ServletResponse response) {
        AuthenticationToken token=this.createToken(request,response);
        if (token==null){
            String msg = "createToken method implementation returned null. A valid non-null AuthenticationToken must be created in order to execute a login attempt.";
            throw new IllegalStateException(msg);
        }else {
            try {
                Subject subject=this.getSubject(request,response);
//                boolean existing = subject.getSession(false) != null;
//                if (!existing){
//                    subject.getSession(true);
//                }
                subject.login(token);
                return this.onLoginSuccess(token, subject, request, response);
            } catch (AuthenticationException var5) {
                return this.onLoginFailure(token, var5, request, response);
            }
        }
    }

login方法:

Subject subject = this.securityManager.login(this, token);

public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
        AuthenticationInfo authenticationInfo;
        try {
            authenticationInfo=this.authenticate(token);
        }catch (AuthenticationException e){
            try {
                this.onFailedLogin(token,e,subject);
            }catch (Exception ez){
                if(log.isInfoEnabled()) {
                    log.info("onFailedLogin method threw an exception.  Logging and propagating original AuthenticationException.", ez);
                }
            }
            throw e;
        }
        Subject loggedIn=this.createSubject(token,authenticationInfo,subject);
        this.onSuccessfulLogin(token, authenticationInfo, loggedIn);
        return loggedIn;
    }

this.authenticate(token);会调用releam获取数据源的账号信息,也就是用户自定义的数据源(比如数据库)中的账号密码,与用户输入的账号密码对比。

@Override
    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);
    }
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token){
        AuthenticationInfo info = this.doGetAuthenticationInfo(token);
        if(info != null) {
            this.assertCredentialsMatch(token, info);
        }
        return info;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shiro-redis-spring-boot-starter是一个用于集成Apache Shiro和Redis的Spring Boot Starter项目。Apache Shiro是一个强大而灵活的Java安全框架,用于身份验证、授权和会话管理等安全功能。而Redis是一个高性能的内存数据库,其具有快速的数据存取能力和持久化支持。 shiro-redis-spring-boot-starter提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易。通过使用该Starter,我们可以方便地将Shiro的会话管理功能存储到Redis中,从而支持分布式环境下的会话共享和管理。 使用shiro-redis-spring-boot-starter可以带来以下好处: 1. 分布式环境的会话共享:通过将Shiro的会话数据存储到Redis中,不同的应用节点可以共享同一个会话,从而实现分布式环境下的会话管理和跨节点的身份验证和授权。 2. 高可用性和性能:Redis作为一个高性能的内存数据库,具有出色的数据读写能力和持久化支持,可以提供可靠的会话存储和高性能的数据访问能力。 3. 简化配置和集成:shiro-redis-spring-boot-starter提供了封装好的配置和集成方式,减少了我们自己实现集成的复杂性和工作量。 总结来说,shiro-redis-spring-boot-starter为我们提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易和高效。通过它,我们可以实现分布式环境下的会话共享和管理,提供高可用性和性能的数据存取能力,同时简化了配置和集成的复杂性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值