• Shiro入门以及Shiro与web整合


    标题Shiro入门以及Shiro与web整合

    Shiro框架
    - 什么是Shiro?
    Apache shiro是一个强大,易用的java安全框架执行身份认证,授权,密码和会话管理。
    Shiro框架的核心组件
    主要核心组件:Subject, Security Manager Realms

    1. Subject: 即“当前操作的用户”,但是,在shiro中,Subject这一概念不仅仅指人,也可以是第三方进程,后台账户或其他类似事物.
    Subject代表了当前用户的安全操作,Security Manager则管理用户的安全操作。

    2. Security Manager : shiro框架的核心,典型的Facade模式,shiro通过Security Manager来管理内部组件实例,并通过它来提供安全管理的各种服务。
    3.Realms : Realms实质上是一个安全先关的DAO:它封装了数据源的连接细节,并在需要时把数据提供给shiro
    当配置Shiro时,你必须至少指定一个Realm,用于认证和(或)授权。配置多个Realm是可以的,但是至少需要一个。
    Shiro内置了可以连接大量安全数据源(又名目录)的Realm,如LDAP、关系数据库(JDBC)、类似INI的文本配置资源以及属性文件等
    。如果缺省的Realm不能满足需求,你还可以插入代表自定义数据源的自己的Realm实现。

    Shiro的入门案例

    第一步: 官网下载相对应的jar包,在你的maven工程里面导入pom.xml导入相关依赖
    官网地址:Shiro官网地址

     
            UTF-8
            1.7
            1.7
            3.7.0
    
            
            4.12
            4.0.0
            2.9.1
            1.7.7
            3.2.0
    
            1.2.5
        
    
    
        
            
            
                org.apache.shiro
                shiro-core
                ${shiro.version}
            
            
            
                org.apache.shiro
                shiro-web
                ${shiro.version}
            
            
            
                junit
                junit
                ${junit.version}
                test
            
            
            
                javax.servlet
                javax.servlet-api
                ${servlet.version}
                provided
            
            
            
                org.slf4j
                slf4j-api
                ${slf4j.version}
            
            
                org.slf4j
                jcl-over-slf4j
                ${slf4j.version}
                runtime
                
                    
                        slf4j-api
                        org.slf4j
                    
                
            
            
            
                org.apache.logging.log4j
                log4j-slf4j-impl
                ${log4j2.version}
                
                    
                        slf4j-api
                        org.slf4j
                    
                
            
            
            
                org.apache.logging.log4j
                log4j-api
                ${log4j2.version}
            
            
                org.apache.logging.log4j
                log4j-core
                ${log4j2.version}
            
            
            
                org.apache.logging.log4j
                log4j-web
                ${log4j2.version}
                runtime
            
            
            
                com.lmax
                disruptor
                ${log4j2.disruptor.version}
            
        
          
                
                
                    src/main/java
                    
                        **/*.xml
                    
                
                
                
                    src/main/resources
                    
                        *.properties
                        *.xml
                        *.ini
                    
                
            
             
                        org.apache.maven.plugins
                        maven-compiler-plugin
                        3.7.0
                        
                            1.8
                            1.8
                            UTF-8
                        
                    
                    
                    
                        org.apache.maven.plugins
                        maven-compiler-plugin
                        ${maven.compiler.plugin.version}
                        
                            ${maven.compiler.source}
                            ${maven.compiler.target}
                            ${project.build.sourceEncoding}
                        
                    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    注意 我这所用的jar包是接下来需要和web一起整合所需要的jar包,如果只是单独使用shiro只需要导入Shiro-core和相关日志信息的jar包即可
    第二步:编写写一个Shiro入门的案例,创建一个ShiroDemo类,Shiro认证
    在这里插入图片描述

      public static void main(String[] args) {
            /**
             * 1 获取数据源
             * 2 获取安全管理器
             * 3 安全管理器交给SecurityUtils
             * 4 securityUtils去获取Subject主体
             * 5 将前段jsp的信息传给形成令牌
             * 6 登录操作
             *
             */
            String username = "zs";
            String password = "123";
            IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
            SecurityManager instance = iniSecurityManagerFactory.getInstance();
            SecurityUtils.setSecurityManager(instance);
            Subject subject = SecurityUtils.getSubject();
            if(!subject.isAuthenticated()){
                UsernamePasswordToken token = new UsernamePasswordToken(username,password);
                token.setRememberMe(true);
                try {
                    subject.login(token);
                    System.out.println("login Success");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
    
            /**
             * 错误:org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zs, rememberMe=false] did not match the expected credentials
             * 原因:密码错误
             * 错误:org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@25b485ba] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - ss, rememberMe=false].
             * 错误原因:用户名错误
             */
            subject.logout();
            System.out.println("logout Success");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    Shiro与web整合

    1. 导入相关pom.xml依赖在上述Shiro入门时已将所有jar导入
    2. 导入resources导入realm(Shiro-web.ini)通俗的就是数据源

    [main]
    #定义身份认证失败后的请求url映射,loginUrl是身份认证过滤器中的一个属性
    authc.loginUrl=/login
    #定义角色认证失败后的请求url映射,unauthorizedUrl是角色认证过滤器中的一个属性
    roles.unauthorizedUrl=/unauthorized.jsp
    #定义权限认证失败后请求url映射,unauthorizedUrl是角色认证过滤器中的一个属性
    perms.unauthorizedUrl=/unauthorized.jsp
    
    [users]
    zs=123,role1
    ls=123,role2
    ww=123,role3
    zdm=123,admin
    
    
    [roles]
    role1=user:create
    role2=user:create,user:update
    role3=user:create,user:update,user:delete,user:view,user:load
    admin=user:*
    
    
    
    #定义请求的地址需要做什么验证
    [urls]
    #请求login的时候不需要权限,游客身份即可(anon)
    /login.do=anon
    
    #请求/user/updatePwd.jsp的时候,需要身份认证(authc)
    /user/updatePwd.jsp=authc
    
    #请求/admin的时候,需要角色认证,必须是拥有admin角色的用户才行
    /admin/*.jsp=roles[admin]
    
    #请求/teacher的时候,需要权限认证,必须是拥有user:create权限的角色的用户才行
    /user/teacher.jsp=perms["user:update"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    3. 配置web.xml,tomcat启动的时候加载Shiro所有文件

    
        shiroConfigLocations
        classpath:shiro-web.ini
      
      
        org.apache.shiro.web.env.EnvironmentLoaderListener
      
    
      
        ShiroFilter
        org.apache.shiro.web.servlet.ShiroFilter
      
      
        ShiroFilter
        /*
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4 编写一个Shiro与web整合的案例(模仿登录和退出)
    创建写一个LoginServlet类并继承HTTPServlet
    在这里插入图片描述配置web.xml

      
        loginServlet
        com.ahong.web.LoginServlet
      
      
        loginServlet
        /login
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    启动tomca进行测试
    当我们打开登录界面输入错误的密码,会把之前设置的值带给页面

    在这里插入图片描述
    输入正确登录信息进入主页面
    在这里插入图片描述
    当我们点击用户新增的时候,此时在shiro-web.ini中的zs这个用户是没有这个权限的
    在这里插入图片描述
    创建LogoutServlet类
    在这里插入图片描述
    配置web.xml

     
        logoutServlet
        com.ahong.web.LogoutServlet
      
      
        logoutServlet
        /logout
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    启动tomcat测试,点击退出系统就会回到登录界面
    在这里插入图片描述
    最后我们简述一个Shiro标签,此标签使用如果在你的Shiro-web.ini当用户登录的时候有此权限就会显示,反之不显示

     shiro标签
        
  • 用户新增
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    学习官网:Shiro学习网站

  • 相关阅读:
    美团三年,总结的10条血泪教训
    Worthington丨Worthington核糖核酸酶A和B介绍
    ubuntu16.04修改静态ip
    Leetcode416. 分割等和子集
    小谈设计模式(29)—访问者模式
    DBCO-PEG-IR825|IR825-PEG-DBCO|二苯并环辛炔-聚乙二醇-IR825荧光染料|DBCO-PEG-IR-825荧光染料
    Vue.js路由及Node.js的入门使用---超详细
    新零售系统平台解决方案 线上线下小程序怎么做
    如何使用Go与MQTT进行通信
    【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
  • 原文地址:https://blog.csdn.net/m0_67401417/article/details/126496252