《基于SpringBoot+Shiro的权限管理实现》论文笔记

《基于SpringBoot+Shiro的权限管理实现》论文笔记

一、基本信息

  • 标题:基于SpringBoot+Shiro的权限管理实现

  • 来源:成都大学信息工程与科学学院,成都,610100

  • 关键字:Shiro 框架;SpringBoot 框架;权限管理

    二、研究内容

  1. Shiro 概述
    • Apache Shiro 是一个拥有许多功能的综合性的程序安全框架, Shiro 提供了一个干净而直观的API, 它干净利落地处理身份认证、授权、企业会话管理和加密。Shiro 易于使用和理解, 同时功能十分强大, 能验证用户来核实他们的身份、对用户执行访问控制、判断用户是否被分配了一个确定的安全角色、判断用户是否被允许做某事。
    • Shiro 当中的Session 功能是在相应应用中创建会话编程范式, 其与容器是相互独立的关系, 因此Shiro 支持在任何环境下使用Session API, 即使没有Web 或EJB容器。Shiro 还能在身份验证访问控制期间或在会话的生命周期, 对事件作出反应。
    • Apache Shiro 的架构有3 个主要的概念: Subject,SecurityManager 和Realms。其中Subject 是一个比较抽象的概念, 通常我们会将Subject 对象理解为一个任何可以与应用交互的“用户”, 但它也有可能是一个三方程序, 可以理解为任何与系统交互的“东西” 都是Subject。在Shiro 框架中通过Subject 完成登录、退出、校验权限、获得Session 等。SecurityManager 是Shiro 的心脏, 是Shiro 中最核心的组件; 所有具体的安全操作都通过SecurityManager 进行控制; SecurityManager 管理着所有Subject, 并且关于Subject 的所有操作都由SecurityManager进行交互。Realms 担当Shiro 和应用程序的安全数据之间的“桥梁” 或“连接器”, 用于用户认证和授权; Realms 本质上是一个特定安全的DAO: 它封装了数据源的连接详细信息, 使Shiro 所需的相关的数据可用。当配置Shiro 时, 必须指定至少一个Realm 用来进行身份验证和授权。SecurityManager 可以配置多个Realms, 但至少需要配置一个。
    1. 配置pom 文件
      xml <! --集成shiro--> <dependency> <group|d> org.apache.shiro </group|d> <artifact|d> shiro-spring </artifact|d> <version> 1.4.0 </version> </dependency>
    2. 自定义Reaml
    @Component
    public class MyShiroUserRealm extends AuthorizingRealm {
    @Autowired
    UserServiceImpl userService;
    
    /**
     * 用于授权
     *
     * @param principa|s
     * @return 授权信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        UserInforMation userInfo = (UserInforMation) principals.getPrimaryPrincipal();
    //用户权限列表
        Set<String> userPermsSet = userService.getUserPermissions(userInfo);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(userPermsSet);
        return info;
    }
    
    /**
     * 用于认证
     *
     * @param token
     * @return 认证信息
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //用户信息获取
        String userNameInput = (String) token.getPrincipal();
        String passwordInput = new String((char[]) token.getCredentials());
        //查询用户信息
        UserInforMation user = userService.findld(userNameInput);
        //用户不存在
        if (user == null) {
            throw new UnknownAccountException("用户账号不存在! ");
        }
        //密码错误
        if (!passwordInput.equals(user.getPassword())) {
            throw new IncorrectCredentialsException("账号用户名或者密码错误! ");
        }
        //账号被注销
        if (user.getState().equals("0")) {
            throw new LockedAccountException("账户已被注销! ");
        }
        System.out.println("用户登陆成功! ");
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), user.getName());
        return info;
    }
    }

    三、结论

  • 通过在SpringBoot 项目中使用Shiro 安全框架为项目 提供了安全访问控制的功能,同时实现了用户身份认证、授权、会话管理等功能。任何程序都需要安全控制,将 程序的安全控制交给Shiro,利用Shiro 简单易用的特点 配合SpringBoot 快速便捷开发,减少了为项目安全控制 编写大量重复代码的工作.

四、参考文献

  • [1]王杉文.基于SpringBoot+Shiro的权限管理实现[J].电脑编程技巧与维护,2019,(9):160-161,173.

猜你喜欢

转载自www.cnblogs.com/clamye/p/12045432.html