• Shiro入门-shiro与spring整合


    引入jar

    
    
        org.apache.shiro
        shiro-core
        1.3.2
    
    
    
    
        org.apache.shiro
        shiro-web
        1.3.2
    
    
    
    
        org.apache.shiro
        shiro-spring
        1.3.2
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    配置web.xml

    
    
    
        shiroFilter
        org.springframework.web.filter.DelegatingFilterProxy
        
        
            targetFilterLifecycle
            true
        
        
        
            targetBeanName
            shiroFilter
        
    
    
        shiroFilter
        /*
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    配置spring-shiro.xml
    注意:Shiro 集成 Spring 应该添加到父上下文中去,而不能添加到 Spring MVC 的子上下文中。

    
    
    
        
        
        
        
        
        
        
        
        
            
                
                
            
        
    
        
        
            
                
                /images/** = anon
                /js/** = anon
                /styles/** = anon
                
                /validatecode.jsp = anon
    
                
                /logout.action = logout
                
                
                
                /index.jsp  = user
                /first.action = user
                /welcome.jsp = user
                
                /** = authc
                
    
            
        
    
    
    
    
        
        
        
        
        
        
            
    
    
    
    
        
        
    
    
    
    
        
        
    
    
    
    
        
    
    
    
    
         
         
         
         
    
    
    
    
    
        
        
        
        
        
        
    
    
    
    
        
    
    
    
    
        
        
        
        
    
    
    • 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

    配置springmvc.xml

    
    
    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6


    对静态资源设置逆名访问

    
    /images/** = anon
    /js/** = anon
    /styles/** = anon
    
    • 1
    • 2
    • 3
    • 4

    登陆原理
    使用**FormAuthenticationFilter**过虑器实现 ,原理如下:

    将用户没有认证时,请求loginurl进行认证,用户身份和用户密码提交数据到loginurl
    FormAuthenticationFilter拦截住取出request中的username和password(两个参数名称是可以配置的
    FormAuthenticationFilter调用realm传入一个token(username和password)
    realm认证时根据username查询用户信息(在Activeuser中存储,包括 userid、usercode、username、menus)。
    如果查询不到,realm返回null,FormAuthenticationFilter向request域中填充一个参数(记录了异常信息)
    登陆页面
    由于FormAuthenticationFilter的用户身份和密码的input的默认值(username和password),修改页面的账号和密码 的input的名称为username和password
    也可以配置:

    
    
    
        
        
        
        
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    登录代码实现

    //登陆提交地址,和spring-shiro.xml中配置的loginurl一致
    @RequestMapping("login")
    public String login(HttpServletRequest request)throws Exception{
    
        //如果登陆失败从request中获取认证异常信息,shiroLoginFailure就是shiro异常类的全限定名
        String exceptionClassName = (String) 
                request.getAttribute("shiroLoginFailure");
        //根据shiro返回的异常类路径判断,抛出指定异常信息
        if(exceptionClassName!=null){
            if (UnknownAccountException.class.getName()
                .equals(exceptionClassName)) {
                //最终会抛给异常处理器
                throw new CustomException("账号不存在");
            } else if (IncorrectCredentialsException.class.getName()
                .equals(exceptionClassName)) {
                    throw new CustomException("用户名/密码错误");
            } else if("randomCodeError".equals(exceptionClassName)){
                    throw new CustomException("验证码错误 ");
            }else {
                    throw new Exception();//最终在异常处理器生成未知错误      
            }
        }
        //此方法不处理登陆成功(认证成功),shiro认证成功会自动跳转到上一个请求路径
        //登陆失败还到login页面
        return "login";
    }
    
    • 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

    退出使用LogoutFilter
    不用我们去实现退出,只要去访问一个退出的url(该 url是可以不存在),由LogoutFilter拦截住,清除session。

    在spring-shiro.xml配置LogoutFilter:

    
    /logout.action = logout
    
    • 1
    • 2

    问题总结

    1. 在spring-shiro.xml中配置过滤器链接,需要将全部的url和权限对应起来进行配置,比较发麻不方便使用。
    2. 每次授权都需要调用realm查询数据库,对于系统性能有很大影响,可以通过shiro缓存来解决。

    shiro的过虑器

    过滤器简称 对应的java类:
    anon org.apache.shiro.web.filter.authc.AnonymousFilter
    authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
    authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
    perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
    port org.apache.shiro.web.filter.authz.PortFilter
    rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
    roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
    ssl org.apache.shiro.web.filter.authz.SslFilter
    user org.apache.shiro.web.filter.authc.UserFilter
    logout org.apache.shiro.web.filter.authc.LogoutFilter

    anon:例子/admins/**=anon 没有参数,表示可以匿名使用。
    authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,FormAuthenticationFilter是表单认证,没有参数
    perms:例子/admins/user/=perms[user:add:],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/=perms[“user:add:,user:modify:*”],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。
    user:例如/admins/user/**=user没有参数表示必须存在用户, 身份认证通过或通过记住我认证通过的可以访问,当登入操作时不做检查

  • 相关阅读:
    第七讲:利用类事件改变对象的属性(上)
    深入类加载机制
    自动化测试执行过程和报告内容分析
    LeetCode 89 格雷编码
    腾讯云服务器+宝塔+后端+前端发布
    java计算机毕业设计教学质量评价系统源码+数据库+lw文档+系统
    创新创业理论研究与实践杂志社创新创业理论研究与实践编辑部2022年第18期目录
    云原生之nacos架构一览解读
    [Android] [ROOT] Magisk(魔术师/面具) 设置以及必装模块的安装
    qt5-入门-使用拖动方式创建Dialog
  • 原文地址:https://blog.csdn.net/m0_67401270/article/details/126496251