shiro认证策略,授权

有具体问题的可以参考之前的关于shiro的博文,关于shiro的博文均是一次工程的内容 !

认证策略:

修改认证策略:
applicationContext.xml
     <!-- 认证器 -->
         <bean id="autheniicator"
          class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
            <property name="realms">
                 <list>
                      <ref bean="jdbcRealm"/>
                      <ref bean="SecondRealm"/>
                 </list>
            </property>
            
            <!-- 认证策略 -->
            <property name="authenticationStrategy">
                 <bean  class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean>
            </property>
         </bean>
         

 授权:

 授权之前的小说明:

 

此时的访问并没有问题:

 注:在做授权的时候需要在securityManager 中读取realms

 <bean id="securityManager"  class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="authenticator"  ref="autheniicator"></property>
        
        <property name="realms">
                 <list>
                      <ref bean="jdbcRealm"/>
                      <ref bean="SecondRealm"/>
                 </list>
        </property>
    </bean>

 

 

代码开始
新建一个类

list.sjp页面中

<body>
    list.
    <br>
    <a href="admin.jsp">TO Admin</a>
    <br>
    <a href="user.jsp">TO User</a>
    <br>
    <a href="shiro/logout">Logout</a>
  </body>

 applicationContext.jsp

<!--
            配置那些页面需要受保护,以及访问这些页面需要的的权限
            1)anon 可以被匿名访问
            2)authc 必须认证即登陆后才可以访问的页面
            3).logout登出
            4)roles 角色过滤器
        -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                  /shiro/login = anon
                  /shiro/logout = logout
                  
                  /user.jsp = roles[user]
                  /admin.jsp = roles[admin]
                  
                # everything else requires authentication:
                /** = authc
            </value>
        </property>

 此时点击访问user.jsp/admin.jsp的超链接都会去没有权限访问的页面

 授权流程:

需要实现AuthorizingRealm的.....
public abstract class AuthorizingRealm extends  AuthenticatingRealm
        implements Authorizer, Initializable,  PermissionResolverAware, RolePermissionResolverAware
AuthorizingRealm继承 AuthenticatingRealm,但是没有实现 AuthenticatingRealmdoGetAuthenticationInfo的方法
所以认证和授权只需要继承 AuthorizingRealm就可以了,同时实现它的两个抽象方法
public class ShiroReamlTest extends AuthorizingRealm{
     
     //授权
     protected AuthorizationInfo  doGetAuthorizationInfo(PrincipalCollection principals) {
           // TODO Auto-generated method stub
           return null;
     }
     
     //加密
     protected AuthenticationInfo  doGetAuthenticationInfo(AuthenticationToken token) throws  AuthenticationException {
           // TODO Auto-generated method stub
           return null;
     }
}

授权需要继承的类以及实现的方法

 实现:

public class ShiroRealm extends AuthorizingRealm {
     @Override
     protected AuthenticationInfo  doGetAuthenticationInfo(AuthenticationToken arg0) throws  AuthenticationException {
        。。。。。。。
     }
     //授权
     @Override
     protected AuthorizationInfo  doGetAuthorizationInfo(PrincipalCollection principals) {
           
           System.out.println(principals);
           //1.PrincipalCollection获取登陆的用户信息
           Object principal = principals.getPrimaryPrincipal();
           System.out.println(principal);
           //2.利用登陆的用户信息获取当前用户角色的权限
           Set<String> roles = new HashSet<String>();
           roles.add("user");
           if("admin".equals(principal)){
                roles.add("admin");
           }
           //3.创建SimpleAuthorizationInfo,并且设置其reles属性
           SimpleAuthorizationInfo info = new  SimpleAuthorizationInfo(roles);
           //4.
           return info;
     }
     
     
}

 

 此时的设置之后 是可以成功访问的!

猜你喜欢

转载自www.cnblogs.com/Mrchengs/p/9987618.html