iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >shiro拦截认证的全过程记录
  • 339
分享到

shiro拦截认证的全过程记录

2024-04-02 19:04:59 339人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录概述shrio 拦截认证全过程总结概述 shiro是apache旗下一个开源安全框架(Http://shiro.apache.org/),它将软件系统的安全认证相关的功能抽取出来

概述

shiro是apache旗下一个开源安全框架(Http://shiro.apache.org/),它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权、加密、会话管理等功能,组成了一个通用的安全认证框架。使用shiro就可以非常快速的完成认证、授权等功能的开发,降低系统成本。

Shiro框架三大核心对象

说明:

1)Subject :主体对象,负责提交用户认证和授权信息。

2)SecurityManager:安全管理器,负责认证,授权等业务实现。(核心)

3)Realm:领域对象,负责从数据层获取业务数据。

shrio 拦截认证全过程

 1.FilterReGIStrationBean过滤注册bean


@Bean
public FilterRegistrationBean shiroFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new DelegatingFilterProxy("shiroFilter"));
    //该值缺省为false,表示生命周期由springApplicationContext管理,设置为true则表示由ServletContainer管理
    registration.addInitParameter("targetFilterLifecycle", "true");
    registration.setEnabled(true);
    registration.setOrder(Integer.MAX_VALUE - 1);
    registration.addUrlPatterns("
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
    //获取请求token,如果token不存在,直接返回401
    String token = getRequestToken((httpservletRequest) request);
    if(StringUtils.isBlank(token)){
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setContentType("application/JSON;charset=utf-8");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());

        String json = new Gson().toJson(new Result().error(ErrorCode.UNAUTHORIZED));

        httpResponse.getWriter().print(json);

        return false;
    }

    return executeLogin(request, response);
}

4.调用父类 executeLogin 进行登录验证


protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
    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);
            subject.login(token);
            return this.onLoginSuccess(token, subject, request, response);
        } catch (AuthenticationException var5) {
            return this.onLoginFailure(token, var5, request, response);
        }
    }
}

5.subject.login(token); 进行登录

login方法被DelegatingSubject重写


public void login(AuthenticationToken token) throws AuthenticationException {
    **
    Subject subject = this.securityManager.login(this, token);
    **
}

6.securityManager.login(this, token) login被DefaultSecurityManager

接下来几步没那么重要省略部分

7.ModularRealmAuthenticator AuthenticationInfo 授权信息获取方法


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

getRealms 获取我们自己重写的Realms类,主要用户获取用户信息

8.接下来则进入我们自己写的Realms类 我的类叫Oauth2Realm



@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    String accessToken = (String) token.getPrincipal();

    //根据accessToken,查询用户信息
    SysUserTokenEntity tokenEntity = shiroService.getByToken(accessToken);
    //token失效
    if(tokenEntity == null || tokenEntity.getExpireDate().getTime() < System.currentTimeMillis()){
        throw new IncorrectCredentialsException(MessageUtils.getMessage(ErrorCode.TOKEN_INVALID));
    }

    //查询用户信息
    SysUserEntity userEntity = shiroService.getUser(tokenEntity.getUserId());

    //转换成UserDetail对象
    UserDetail userDetail = ConvertUtils.sourceToTarget(userEntity, UserDetail.class);

    //获取用户对应的部门数据权限
    List<Long> deptIdList = shiroService.getDataScopeList(userDetail.getId());
    userDetail.setDeptIdList(deptIdList);

    //账号定
    if(userDetail.getStatus() == 0){
        throw new LockedAccountException(MessageUtils.getMessage(ErrorCode.ACCOUNT_LOCK));
    }

    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userDetail, accessToken, getName());
    return info;
}

负责获取用户信息的方法

这并不是登录的过程,而是授权过滤的过程,通过token到数据库查询是否有这个用户,且没有过期,则证明已经登录。

总结

到此这篇关于shrio拦截认证的文章就介绍到这了,更多相关shrio拦截认证内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: shiro拦截认证的全过程记录

本文链接: https://www.lsjlt.com/news/158762.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作