2018/03/17

Shiro讲课笔记

什么是shiro

Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码学和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。

主要功能

三个核心组件:Subject, SecurityManager 和 Realms.

Subject:即“当前操作用户”。但是,在Shiro中,Subject这一概念并不仅仅指人,也可以是第三方进程、后台帐户(Daemon Account)或其他类似事物。它仅仅意味着“当前跟软件交互的东西”。但考虑到大多数目的和用途,你可以把它认为是Shiro的“用户”概念。
  Subject代表了当前用户的安全操作,SecurityManager则管理所有用户的安全操作。
  SecurityManager:它是Shiro框架的核心,典型的Facade模式,Shiro通过SecurityManager来管理内部组件实例,并通过它来提供安全管理的各种服务。
  Realm: Realm充当了Shiro与应用安全数据间的“桥梁”或者“连接器”。也就是说,当对用户执行认证(登录)和授权(访问控制)验证时,Shiro会从应用配置的Realm中查找用户及其权限信息。

示例配置过程

1,创建项目,引入JAR

http://mvnrepository.com/artifact/org.apache.shiro/shiro-all/1.3.2

  <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-all -->

<dependency>

    <groupId>org.apache.shiro</groupId>

    <artifactId>shiro-all</artifactId>

    <version>1.3.2</version>

</dependency>

复制配置到pom.xml里面,加入jar

2,web.xml里面加入shiro过滤器

<!-- shiro过滤器 开始-->

<filter>  

    <filter-name>shiroFilter</filter-name>  

    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  

   <!--  <init-param>  

        <param-name>targetFilterLifecycle</param-name>  

        <param-value>true</param-value>  

    </init-param>  -->

</filter>  

<filter-mapping>  

    <filter-name>shiroFilter</filter-name>  

    <url-pattern>/*</url-pattern>  

</filter-mapping>

<!-- shiro过滤器 结束-->

3,配置shiro配置文件

 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  

        <property name="realm" ref="myShiroRealm" />  

        <property name="cacheManager" ref="cacheManager" />  

    </bean>  

 

    <!-- 項目自定义的Realm -->  

    <bean id="myShiroRealm" class="com.aaa.shiro.service.impl.MyShiroRealm">  

        <property name="cacheManager" ref="cacheManager" />  

    </bean>  

 

    <!-- Shiro Filter -->  

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

       <!-- 必须的配置 -->

        <property name="securityManager" ref="securityManager" />  

        <!--如果认证失败,用户名或者密码错误,就会执行这个配置  -->

        <property name="loginUrl" value="/userControll/toLogin" />

        <!-- 认证成功之后跳转的位置 -->

        <property name="successUrl" value="/userControll/list" />  

         <!-- 访问了无权访问的页面时,跳转该页面 -->

        <property name="unauthorizedUrl" value="/error.jhtml" />  

        <property name="filterChainDefinitions">  

            <value>  

            

               /userControll/toLogin = anon  <!-- anon免拦截   authc需要拦截的资源  **代表拦截所有的-->

                /userControll/login = anon  <!-- anon免拦截   authc需要拦截的资源  **代表拦截所有的-->

               /** = authc

            </value>  

        </property>  

    </bean>  

 

    <!-- 用户授权信息Cache -->  

<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />  

 

4,编写认证授权类

package com.aaa.shiro.service.impl;

 

 

import java.util.ArrayList;

import java.util.List;

 

import org.apache.shiro.authc.AuthenticationException;

import org.apache.shiro.authc.AuthenticationInfo;

import org.apache.shiro.authc.AuthenticationToken;

import org.apache.shiro.authc.SimpleAuthenticationInfo;

import org.apache.shiro.authc.UsernamePasswordToken;

import org.apache.shiro.authz.AuthorizationInfo;

import org.apache.shiro.authz.SimpleAuthorizationInfo;

import org.apache.shiro.realm.AuthorizingRealm;

import org.apache.shiro.subject.PrincipalCollection;

 

/**

 *@className:MyShiroRealm.java

 *@discription:

 *@author:zhz

 *@createTime:2018-3-17下午4:59:19

 *@version:1.0.0

 **/

public  class MyShiroRealm extends AuthorizingRealm {

 

/**

 * 授权方法

 */

@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {

// TODO Auto-generated method stub

   //获取登录时输入的用户名    

        String loginName=(String) arg0.fromRealm(getName()).iterator().next();  

     //   arg0.getPrimaryPrincipal();

        //到数据库查是否有此对象    

      //  User user=userService.findByName(loginName);

        System.out.println(loginName);

        if("zhangsan"!=null){    

            //权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)    

            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();    

            //用户的角色集合    

           // info.setRoles(user.getRolesName());    

            //用户的角色对应的所有权限,如果只使用角色定义访问权限,下面的四行可以不要    

            List<String> roleList= new ArrayList();   

            roleList.add("zhuGuan");

                info.addStringPermissions(roleList);    

            //}    

            return info;    

        }    

        return null;    

}

 

/**

 * 认证方法

 */

@Override

protected AuthenticationInfo doGetAuthenticationInfo(

AuthenticationToken arg0) throws AuthenticationException {

// TODO Auto-generated method stub

UsernamePasswordToken token = (UsernamePasswordToken) arg0;

//从数据库查询相关的用户名和密码

        if(token.getUsername().equals("zhangsan")){

            return new SimpleAuthenticationInfo("zhangsan","admin", getName());  

        }else{

           return null;  

        }

}

 

 

}

4,授权测试

 

 

posted on 2018-03-17 21:33  时间似流水  阅读(149)  评论(0)    收藏  举报