Shiro验证策略-shiro自定义实现Realm来实现身份验证-shiro散列加密算法-shiro授权-shiro自定义Realm实现授权 2022-11-26 01:40 1047阅读 0赞 # Shiro验证策略 # Authentication Strategy:认证策略,在shiro中有三种认证策略; <table> <tbody> <tr> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">AtleastOneSuccessfulStrategy【默认】</p> </td> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">如果一个或多个Realm验证成功,则整体的尝试被认为是成功的。如果没有一个验证成功,则整体尝试失败;</p> </td> </tr> <tr> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">FirstSuccessfulStrategy</p> </td> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">只有第一个成功地验证的Realm返回的信息将被使用。所有进一步的Realm将被忽略。如果没有一个验证成功,则整体尝试失败。</p> </td> </tr> <tr> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">AllSuccessfulStrategy</p> </td> <td style="vertical-align:top;width:207.4pt;"> <p style="margin-left:0cm;">为了整体的尝试成功,所有配置的Realm必须验证成功。如果没有一个验证成功,则整体尝试失败</p> </td> </tr> <tr> <td colspan="2" style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">authenticationStrategy=org.apache.shrio.authc.pam.FirstSuccessfulStrategy</p> <p style="margin-left:0cm;">securityManager.authentication.authenticationStrategy=$authenticationStrategy</p> </td> </tr> <tr> <td colspan="2" style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">[main]</p> <p style="margin-left:0cm;">dataSource=com.mchange.v2.c3p0.ComboPooledDataSource</p> <p style="margin-left:0cm;">dataSource.driverClass=com.mysql.jdbc.Driver</p> <p style="margin-left:0cm;">dataSource.jdbcUrl=jdbc:mysql://localhost:3306/myshiro</p> <p style="margin-left:0cm;">dataSource.user=root</p> <p style="margin-left:0cm;">dataSource.password=1234567</p> <p style="margin-left:0cm;">jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm</p> <p style="margin-left:0cm;">#$表示引用对象</p> <p style="margin-left:0cm;">jdbcRealm.dataSource=$dataSource</p> <p style="margin-left:0cm;">#配置验证器</p> <p style="margin-left:0cm;">authenticationStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy</p> <p style="margin-left:0cm;">securityManager.realm=$jdbcRealm</p> <p style="margin-left:0cm;">securityManager.authenticator.authenticationStrategy=$authenticationStrategy</p> </td> </tr> </tbody> </table> # 自定义实现realm来实现身份认证 # <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">JdbcRealm已经实现了从数据库中获取用户的验证信息,但是jdbcRealm的灵活性太差。如果要实现自己的一些特殊应用时将不能支持。这时候我们可以通过自定义realm来实现身份认证的功能。</p> <p style="margin-left:0cm;"> Realm是一个接口,在接口中定义了根据token获得认证信息的办法。Shiro内容实现了一系列的realm。这些不同Realm实现类提供了不同的功能。AuthenticatingRealm实现了获取身份信息的功能,AuthoringRealm实现了权限信息的功能。通常自定义Realm需要继承AuthoringRealm,这样既可以提供<strong>身份认证</strong>的自由定义方法也可以实现<strong>授权</strong>的自定义方法。</p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">class</span></strong><span style="color:#000000;"> UserRealm </span><strong><span style="color:#7f0055;">extends</span></strong><span style="color:#000000;"> AuthorizingRealm{ </span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#646464;">@Override</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">protected</span></strong><span style="color:#000000;"> AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection </span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated method stub</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> String </span><span style="color:#6a3e3e;">username</span><span style="color:#000000;"> = (String)</span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">.getPrimaryPrincipal();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> String </span><span style="color:#6a3e3e;">pwd</span><span style="color:#000000;"> = </span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">;</span><span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">相当于从数据库中取得</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SimpleAuthenticationInfo </span><span style="color:#6a3e3e;">info</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> SimpleAuthenticationInfo(</span><span style="color:#6a3e3e;">username</span><span style="color:#000000;">,</span><span style="color:#6a3e3e;">pwd</span><span style="color:#000000;">,getName());</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">return</span></strong><span style="color:#000000;"> (AuthorizationInfo) </span><span style="color:#6a3e3e;">info</span><span style="color:#000000;">;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#646464;">@Override</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">protected</span></strong><span style="color:#000000;"> AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken </span><span style="color:#6a3e3e;">arg0</span><span style="color:#000000;">) </span><strong><span style="color:#7f0055;">throws</span></strong><span style="color:#000000;"> AuthenticationException { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated method stub</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">return</span></strong> <strong><span style="color:#7f0055;">null</span></strong><span style="color:#000000;">;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><span style="color:#000000;">}</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">Shiro.ini</span></p> <p style="margin-left:0cm;">[main]</p> <p style="margin-left:0cm;">userRealm=com.pshdhx.UserRealm</p> <p style="margin-left:0cm;">securityManager.realm=$userRealm</p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">注意:使用</span></strong><strong><span style="color:#7f0055;">shiro</span></strong><strong><span style="color:#7f0055;">来完成权限管理,</span></strong><strong><span style="color:#7f0055;">shiro</span></strong><strong><span style="color:#7f0055;">并不会去维护数据。</span></strong><strong><span style="color:#7f0055;">Shiro</span></strong><strong><span style="color:#7f0055;">中使用的数据,需要程序员根据处理业务将数据传递给给</span></strong><strong><span style="color:#7f0055;">shiro</span></strong><strong><span style="color:#7f0055;">的相关接口;</span></strong></p> </td> </tr> </tbody> </table> # 散列算法(加密) # <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">1、在身份认证过程中往往会涉及加密。如果不加密那么数据信息不安全。Shiro内容实现了比较多的散列算法。如MD5加密,SHA等。并且提供了加盐功能。比如1111的md5加密是确定的“b59c67bf196a4758191e42f76670ceba”==直接在navicat中查询select MD5('1111')即可得到。这个md5加密很容易被破解,但是如果1111+姓名,那么找到原密码的难度会增加。</p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">package</span></strong><span style="color:#000000;"> com.pshdhx;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.crypto.hash.Md5Hash;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">class</span></strong><span style="color:#000000;"> MD5Demo { </span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">static</span></strong> <strong><span style="color:#7f0055;">void</span></strong><span style="color:#000000;"> main(String[] </span><span style="color:#6a3e3e;">args</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">普通</span><span style="color:#3f7f5f;">md5</span><span style="color:#3f7f5f;">加密</span><span style="color:#3f7f5f;">1111</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> Md5Hash </span><span style="color:#6a3e3e;">md5</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> Md5Hash(</span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">md5</span><span style="color:#000000;">.toString());</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">盐加密</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">md5</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> Md5Hash(</span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">, </span><span style="color:#2a00ff;">"com.pshdhx"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">md5</span><span style="color:#000000;">.toString());</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">盐加密</span><span style="color:#3f7f5f;">+</span><span style="color:#3f7f5f;">迭代次数</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">md5</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> Md5Hash(</span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">, </span><span style="color:#2a00ff;">"com.pshdhx"</span><span style="color:#000000;">, 2);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">md5</span><span style="color:#000000;">.toString());</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">}</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">b59c67bf196a4758191e42f76670ceba</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">799367c240adf5478aa2c2a1c081b0bd</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">458563a4756175ca15176053ca82711c</span></p> <p style="margin-left:0cm;"> </p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> </p> </td> </tr> </tbody> </table> # 授权 # <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">授权:给通过身份认证的人,授予他可以访问某些资源的权限;</p> <p style="margin-left:0cm;">权限粒度:粗粒度:对表的curd;细粒度:是对记录的操作。如只允许查看id为1的一条记录。Shiro一般管理的是粗粒度的权限。比如:菜单,按钮,url。一般细粒度的权限是通过业务来控制的。</p> <p style="margin-left:0cm;">角色:权限的集合。管理授权方便。</p> <p style="margin-left:0cm;">权限表示规则:资源:操作:实例。可以用通配符表示:</p> <p style="margin-left:0cm;"> 如user:add 表示对user有添加的权限</p> <p style="margin-left:0cm;">User:* 表示对user有所有的权限;</p> <p style="margin-left:0cm;">User:delete:100 表示对user标识为100的实例具有删除的权限;</p> <p style="margin-left:0cm;"> </p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">package</span></strong><span style="color:#000000;"> com.pshdhx;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> java.util.Arrays;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.SecurityUtils;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.AuthenticationException;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.UsernamePasswordToken;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authz.AuthorizationException;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.config.IniSecurityManagerFactory;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.mgt.SecurityManager;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.subject.Subject;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.util.Factory;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">class</span></strong><span style="color:#000000;"> AuthorizationDemo { </span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">static</span></strong> <strong><span style="color:#7f0055;">void</span></strong><span style="color:#000000;"> main(String[] </span><span style="color:#6a3e3e;">args</span><span style="color:#000000;">) </span><strong><span style="color:#7f0055;">throws</span></strong><span style="color:#000000;"> Exception { </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> Factory<SecurityManager> </span><span style="color:#6a3e3e;">factory</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> IniSecurityManagerFactory(</span><span style="color:#2a00ff;">"classpath:shiro.ini"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SecurityManager </span><span style="color:#6a3e3e;">securityManager</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">factory</span><span style="color:#000000;">.getInstance();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SecurityUtils.<em>setSecurityManager</em>(</span><span style="color:#6a3e3e;">securityManager</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> Subject </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;"> = SecurityUtils.<em>getSubject</em>();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> UsernamePasswordToken </span><span style="color:#6a3e3e;">token</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> UsernamePasswordToken(</span><span style="color:#2a00ff;">"zhangsan"</span><span style="color:#000000;">, </span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">try</span></strong><span style="color:#000000;"> { </span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.login(</span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> } </span><strong><span style="color:#7f0055;">catch</span></strong><span style="color:#000000;"> (AuthenticationException </span><span style="color:#6a3e3e;">e</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated catch block</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">e</span><span style="color:#000000;">.printStackTrace();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"</span><span style="color:#2a00ff;">认证不通过</span><span style="color:#2a00ff;">"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">boolean</span></strong> <span style="color:#6a3e3e;">hasRole</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.hasRole(</span><span style="color:#2a00ff;">"role1"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"role1</span><span style="color:#2a00ff;">的存在</span><span style="color:#2a00ff;">==="</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">hasRole</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">try</span></strong><span style="color:#000000;"> { </span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.checkRole(</span><span style="color:#2a00ff;">"role1"</span><span style="color:#000000;">); </span><span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">该角色不存在则抛出</span><u><span style="color:#3f7f5f;">Un</span></u><span style="color:#3f7f5f;"> authorized Exception</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> } </span><strong><span style="color:#7f0055;">catch</span></strong><span style="color:#000000;"> (AuthorizationException </span><span style="color:#6a3e3e;">e</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated catch block</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">e</span><span style="color:#000000;">.printStackTrace();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"</span><span style="color:#2a00ff;">角色不存在</span><span style="color:#2a00ff;">"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">判断有多少个角色</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">boolean</span></strong><span style="color:#000000;">[] </span><u><span style="color:#6a3e3e;">hasRoles</span></u><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.hasRoles(Arrays.<em>asList</em>(</span><span style="color:#2a00ff;">"role1"</span><span style="color:#000000;">,</span><span style="color:#2a00ff;">"role2"</span><span style="color:#000000;">));</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//subject.checkRoles("role1","role2");</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">基于资源的授权</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">boolean</span></strong> <span style="color:#6a3e3e;">userAdd</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.isPermitted(</span><span style="color:#2a00ff;">"user:add"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"subject:zhangsan</span><span style="color:#2a00ff;">具有</span><span style="color:#2a00ff;">userAdd</span><span style="color:#2a00ff;">的权限吗</span><span style="color:#2a00ff;">"</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">userAdd</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">判断具有多个授权</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.isPermittedAll(</span><span style="color:#2a00ff;">"user:add"</span><span style="color:#000000;">,</span><span style="color:#2a00ff;">"user:delete"</span><span style="color:#000000;">); </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">}</span></p> <p style="margin-left:0cm;"> </p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">[users]</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">zhangsan=1111,role1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">lisi=1111,role2</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">[roles]</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">role1=user:add,user:update,user:delete</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">role2=user:*</span></strong></p> </td> </tr> </tbody> </table> Shiro的权限检查方式 <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">方式1:编程式</p> <p style="margin-left:0cm;">方式2:注解式</p> <p style="margin-left:0cm;">方式3::标签式</p> <p style="margin-left:0cm;">If(subject.hasRole(“admin”))</p> <p style="margin-left:0cm;">@RequiresRoles(“admin”):在执行指定的方法时会检查是否具有该权限,不需要了url的拦截 </p> <p style="margin-left:0cm;">Public void list()</p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> </p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">授权流程:</p> <p style="margin-left:0cm;"> 获取subject的主体</p> <p style="margin-left:0cm;"> 判断主体是否通过认证</p> <p style="margin-left:0cm;"> 调用subject.isPermitted()/hasRole()的方法来进行权限的判断</p> <p style="margin-left:0cm;">Subject是由其实现类DelegatingSubject来调用方法的,该类将处理交给了SecurityManager;</p> <p style="margin-left:0cm;">SecurityManager是由其实现类DefaultSecurityManager的isPermitted的方法</p> <p style="margin-left:0cm;">来处理,本质是交给其父类-AuthorazitionSecurityManager来处理的。该类将处理处理交给了authorzer()授权器来处理的</p> <p style="margin-left:0cm;">Authrozer()由其实现类ModularRealmAuthorzer来处理。该类可以调用调用对应的Realm来获取数据,在该类有PermissionResolver对权限字符串进行解析,在对应的Realm中也有对应的PermissionResolver交给WildcardPermissionResolver该类调用WildcardPermission来进行权限字符串的解析。然后返回处理结果。</p> <p style="margin-left:0cm;"> </p> </td> </tr> </tbody> </table> # 自定义Realm实现授权 # <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">通过配置文件指定权限不够灵活,并且不方便。在实际应用中大多数情况下都是将用户信息,角色信息保存到数据库中。所以需要从数据库中获取相关的数据信息。可以使用Shiro提供的jdbcRealm来实现,可以自定义Realm来实现。使用JDBCRealm往往也不够灵活,所以在实际应用中大多数情况下都是自定义Realm来实现。</p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;">自定义Realm需要继承AuthorazingRealm,重写方法</p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">package</span></strong><span style="color:#000000;"> com.pshdhx;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> java.util.ArrayList;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> java.util.List;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.AuthenticationException;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.AuthenticationInfo;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.AuthenticationToken;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.SimpleAuthenticationInfo;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authz.AuthorizationInfo;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authz.SimpleAuthorizationInfo;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.realm.AuthorizingRealm;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.subject.PrincipalCollection;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> <u>org.apache.shiro.util.ByteSource</u>;</span></p> <p style="margin-left:0cm;"><span style="color:#3f5fbf;">/**</span></p> <p style="margin-left:0cm;"><span style="color:#3f5fbf;"> * </span><span style="color:#3f5fbf;">自定义</span><span style="color:#3f5fbf;">Realm</span><span style="color:#3f5fbf;">的实现,该</span><span style="color:#3f5fbf;">Realm</span><span style="color:#3f5fbf;">类提供了两个方法</span></p> <p style="margin-left:0cm;"><span style="color:#3f5fbf;"> * doGetAuthorizationInfo </span><span style="color:#3f5fbf;">获取认证信息</span></p> <p style="margin-left:0cm;"><span style="color:#3f5fbf;"> * doGetAuthenticationInfo </span><span style="color:#3f5fbf;">获取权限信息</span></p> <p style="margin-left:0cm;"><span style="color:#3f5fbf;"> */</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">class</span></strong><span style="color:#000000;"> UserRealm </span><strong><span style="color:#7f0055;">extends</span></strong><span style="color:#000000;"> AuthorizingRealm{ </span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#646464;">@Override</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">public</span></strong><span style="color:#000000;"> String getName(){ </span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">return</span></strong> <span style="color:#2a00ff;">"userRealm"</span><span style="color:#000000;">;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">身份认证</span></p> <p style="margin-left:0cm;"> <span style="color:#646464;">@Override</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">protected</span></strong><span style="color:#000000;"> AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken </span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">) </span><strong><span style="color:#7f0055;">throws</span></strong><span style="color:#000000;"> AuthenticationException { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated method stub</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> String </span><span style="color:#6a3e3e;">username</span><span style="color:#000000;"> = (String)</span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">.getPrincipal(); </span><span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">获取身份信息</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"username===="</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">username</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> String </span><span style="color:#6a3e3e;">pwd</span><span style="color:#000000;"> = </span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">;</span><span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">相当于从数据库中取得</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//<u>pwd</u>="b59c67bf196a4758191e42f76670ceba";</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//String salt = "<u>pshdhx</u>";</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">将数据库中查询到的信息封装到</span><span style="color:#3f7f5f;">SimpleAuthenticationInfo</span><span style="color:#3f7f5f;">中</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SimpleAuthenticationInfo </span><span style="color:#6a3e3e;">info</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> SimpleAuthenticationInfo(</span><span style="color:#6a3e3e;">username</span><span style="color:#000000;">,</span><span style="color:#6a3e3e;">pwd</span><span style="color:#000000;">,getName());</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">return</span></strong> <span style="color:#6a3e3e;">info</span><span style="color:#000000;">;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">授权的信息</span></p> <p style="margin-left:0cm;"> <span style="color:#646464;">@Override</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">protected</span></strong><span style="color:#000000;"> AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection </span><span style="color:#6a3e3e;">principal</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated method stub</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> String </span><u><span style="color:#6a3e3e;">username</span></u><span style="color:#000000;"> = (String)</span><span style="color:#6a3e3e;">principal</span><span style="color:#000000;">.getPrimaryPrincipal();</span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">//</span><span style="color:#3f7f5f;">根据用户名去数据库中查询该用户对应的权限的</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> List<String> </span><span style="color:#6a3e3e;">permission</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> ArrayList<>();</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">permission</span><span style="color:#000000;">.add(</span><span style="color:#2a00ff;">"user:add"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">permission</span><span style="color:#000000;">.add(</span><span style="color:#2a00ff;">"user:delete"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">permission</span><span style="color:#000000;">.add(</span><span style="color:#2a00ff;">"user:find"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">permission</span><span style="color:#000000;">.add(</span><span style="color:#2a00ff;">"user:update"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SimpleAuthorizationInfo </span><span style="color:#6a3e3e;">info</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> SimpleAuthorizationInfo();</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">for</span></strong><span style="color:#000000;">(String </span><span style="color:#6a3e3e;">perms</span><span style="color:#000000;"> : </span><span style="color:#6a3e3e;">permission</span><span style="color:#000000;">){ </span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">info</span><span style="color:#000000;">.addStringPermission(</span><span style="color:#6a3e3e;">perms</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">return</span></strong> <span style="color:#6a3e3e;">info</span><span style="color:#000000;">;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><span style="color:#000000;">}</span></p> <p style="margin-left:0cm;"> </p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">[main]</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">userRealm=com.pshdhx.UserRealm</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">securityManager.realm=$userRealm</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">[users]</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">zhangsan=1111</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">lisi=1111</span></strong></p> </td> </tr> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">package</span></strong><span style="color:#000000;"> com.pshdhx;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> <u>java.lang.reflect.Array</u>;</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.SecurityUtils;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.AuthenticationException;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.authc.UsernamePasswordToken;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.config.IniSecurityManagerFactory;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.mgt.SecurityManager;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.subject.Subject;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">import</span></strong><span style="color:#000000;"> org.apache.shiro.util.Factory;</span></p> <p style="margin-left:0cm;"><strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">class</span></strong><span style="color:#000000;"> UserRealmDemo { </span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">public</span></strong> <strong><span style="color:#7f0055;">static</span></strong> <strong><span style="color:#7f0055;">void</span></strong><span style="color:#000000;"> main(String[] </span><span style="color:#6a3e3e;">args</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> Factory<SecurityManager> </span><span style="color:#6a3e3e;">factory</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> IniSecurityManagerFactory(</span><span style="color:#2a00ff;">"classpath:shiro.ini"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SecurityManager </span><span style="color:#6a3e3e;">securityManager</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">factory</span><span style="color:#000000;">.getInstance();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> SecurityUtils.<em>setSecurityManager</em>(</span><span style="color:#6a3e3e;">securityManager</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> Subject </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;"> = SecurityUtils.<em>getSubject</em>();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> UsernamePasswordToken </span><span style="color:#6a3e3e;">token</span><span style="color:#000000;"> = </span><strong><span style="color:#7f0055;">new</span></strong><span style="color:#000000;"> UsernamePasswordToken(</span><span style="color:#2a00ff;">"zhangsan"</span><span style="color:#000000;">, </span><span style="color:#2a00ff;">"1111"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">try</span></strong><span style="color:#000000;"> { </span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.login(</span><span style="color:#6a3e3e;">token</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">if</span></strong><span style="color:#000000;">(</span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.isAuthenticated()){ </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"</span><span style="color:#2a00ff;">验证通过</span><span style="color:#2a00ff;">"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> } </span><strong><span style="color:#7f0055;">catch</span></strong><span style="color:#000000;"> (AuthenticationException </span><span style="color:#6a3e3e;">e</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0cm;"> <span style="color:#3f7f5f;">// </span><strong><span style="color:#7f9fbf;">TODO</span></strong><span style="color:#3f7f5f;"> Auto-generated catch block</span></p> <p style="margin-left:0cm;"> <span style="color:#6a3e3e;">e</span><span style="color:#000000;">.printStackTrace();</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#2a00ff;">"</span><span style="color:#2a00ff;">验证不通过</span><span style="color:#2a00ff;">"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"> <strong><span style="color:#7f0055;">boolean</span></strong><span style="color:#000000;">[] </span><span style="color:#6a3e3e;">permitted</span><span style="color:#000000;"> = </span><span style="color:#6a3e3e;">subject</span><span style="color:#000000;">.isPermitted(</span><span style="color:#2a00ff;">"user:add"</span><span style="color:#000000;">,</span><span style="color:#2a00ff;">"user:delete"</span><span style="color:#000000;">);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> System.</span><strong><em><span style="color:#0000c0;">out</span></em></strong><span style="color:#000000;">.println(</span><span style="color:#6a3e3e;">permitted</span><span style="color:#000000;">[1]+</span><span style="color:#2a00ff;">"=="</span><span style="color:#000000;">+</span><span style="color:#6a3e3e;">permitted</span><span style="color:#000000;">[0]);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">}</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">验证通过</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">true==true</span></p> </td> </tr> </tbody> </table>
相关 SpringBoot:集成Shiro之自定义Realm实现认证授权 ![09afb847119c8914de23269345baa503.png][] 前言 -------------------- 前面的两篇博客使用了IN 悠悠/ 2023年01月09日 12:38/ 0 赞/ 104 阅读
相关 Shiro验证策略-shiro自定义实现Realm来实现身份验证-shiro散列加密算法-shiro授权-shiro自定义Realm实现授权 Shiro验证策略 Authentication Strategy:认证策略,在shiro中有三种认证策略; <table> <tbody> <tr> 喜欢ヅ旅行/ 2022年11月26日 01:40/ 0 赞/ 1048 阅读
相关 shiro自定义Realm 三、SpringBoot整合Shiro完成权限管理案例—自定义Realm > 使用JdbcRealm可以完成用户权限管理,但是我们必须提供JdbcRealm规定的数据表结 不念不忘少年蓝@/ 2022年11月25日 13:03/ 0 赞/ 164 阅读
相关 【Shiro】Apache Shiro架构之自定义realm 欢迎关注我新搭建的博客:[http://www.itcodai.com/][http_www.itcodai.com] > Shiro系列文章: > [【Shiro】Ap 曾经终败给现在/ 2022年09月22日 11:44/ 0 赞/ 169 阅读
相关 Shiro自定义Realms Shiro 的三个核心组件:Subject, SecurityManager 和 Realms. 来回顾一下这三个组件的关系 ![Shiro 的三个核心组件][Shiro] 墨蓝/ 2022年07月15日 00:59/ 0 赞/ 180 阅读
相关 Shiro 自定义realm授权与认证的实现 ……List…… 1.项目需求 2.shiro核心组件 3.自定义realm认证 4.自定义realm授权 5.思考总结 ……1.项目需求…… 以你之姓@/ 2022年07月12日 16:56/ 0 赞/ 227 阅读
相关 shiro学习笔记(3)--自定义realm、授权 一:自定义Realm 1、继承AuthorizingRealm(因为该类中有认证、授权的抽象方法,实现简单) public class MyRealm1 exte 古城微笑少年丶/ 2022年05月13日 06:50/ 0 赞/ 201 阅读
相关 shiro(七)自定义Realm实现授权 1. 仅仅通过配置文件来指定权限不够灵活,并且不方便。在实际的应用中大多数情况下都是将用户信息,角色信息,权限信息保存到了数据库中。所以需要从数据库中获取相关的数据信息。可以 绝地灬酷狼/ 2021年11月17日 22:08/ 0 赞/ 310 阅读
相关 shiro(四)自定义Realm来实现身份认证 1.jdbcRealm已经实现了从数据库中获取用户的验证信息,但是jdbcRealm灵活性太差。如果要实现自己的一些特殊应用时将不能支持。这个时候就可以通过自定义Realm来实 约定不等于承诺〃/ 2021年11月17日 10:40/ 0 赞/ 342 阅读
相关 Shiro--自定义Realm进行认证和授权 创建Maven项目,引入依赖 <dependencies> <dependency> <grou 迈不过友情╰/ 2021年08月24日 12:52/ 0 赞/ 605 阅读
还没有评论,来说两句吧...