ssm搭建shrio安全框架(動態加載)

ssm搭建shrio安全框架(動態加載)

描述:
下面實現例子,實現簡單,權限只控制到模塊,模塊勾選,模塊下的功能全部通過。
其實要控制到方法,只需要添加一些標記字符即可。

一、 shrio介紹
二、導入jar包
三、Web.Xml配置
四、Shrio.xml 配置
五、重點實現shiro配置的內容(必須)
六、根據需求實現shiro配置
七、java文件和配置下載


一、shrio簡單介紹

Apache Shiro是一個強大且易用的Java安全框架,執行身份驗證、授權、密碼學和會話管理。使用Shiro的易於理解的API,您可以快速、輕鬆地獲得任何應用程序,從最小的移動應用程序到最大的網絡和企業應用程序。
框架圖:
這裏寫圖片描述

1、Subject:當前操作用戶
2、SecurityManager :安全管理器,Shiro框架的核心組件,管理所有Subject。
內部組成:

      Authenticator(登錄認證)。
      Authorizer(訪問授權)。
      Session Manager(創建和管理用戶Session生命週期)。
      Session Dao(操作session數據,可重寫AbstractSessionDAO對 
                   session實現增刪查改操作)。
      CacheManager(緩存Shiro使用的鑑權數據)。

3、Realms:領域,用於進行權限信息的驗證(實現Authenticator和Authorizer)

詳細解析參考其他博客。

二、導入jar包

        版本號
        <shiro.version>1.3.2</shiro.version>
<!—shiro相關jar包 -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro.version}</version>
        </dependency>

三、Web.Xml配置

<filter>
        <!--與shiro.xml裏org.apache.shiro.spring.web.ShiroFilterFactoryBean的bean 
            name相同 -->
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

四、Shrio.xml 配置

不過多解析,代碼內註釋得很清楚

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
    default-lazy-init="true">


    <!-- Shiro 攔截 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- shiro的核心安全接口 -->
        <property name="securityManager" ref="securityManager" />
        <!-- 登錄時的鏈接 -->
        <property name="loginUrl" value="/jsp/login.jsp" />
        <!-- 登錄成功時,跳轉的鏈接 -->
        <property name="successUrl" value="/jsp/Main.jsp" />
        <!-- 未認證時跳轉的鏈接 -->
        <!-- <property name="unauthorizedUrl" value="/jsp/login.jsp" /> -->


        <property name="filters">
            <!-- 添加鏈接key規則,系統默認key有:anon(無需權限),authc(登錄後可訪問),perms(需要指定的權限),roles(需要指定角色)等,還可以自定義key -->
            <map>
                 <!--系統默認key-->
                <entry key="authc">
                    <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
                </entry>
                <!--重寫perms-->
                <entry key="perms">
                     <!--繼承PermissionsAuthorizationFilter進行重寫-->
                    <bean class="重寫權限類"></bean>
                </entry>
                <!--自定義key
                <entry key="填寫自定義key">
                    <bean class="指向指定類"></bean>
                </entry>
                -->
            </map>
        </property>
        <!--配置攔截鏈接-->
        <property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource" />
    </bean>
     <!--加載攔截鏈接-->
    <bean id="chainDefinitionSectionMetaSource" class="攔截連接實現類"></bean>

    <!-- 安全接口 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userAuthorization" />
    </bean>

    <!-- 登錄認證、授權 -->
    <bean id="userAuthorization" class="登錄授權認證實現類">

    </bean>

    <!-- 使用springmvc HandlerExceptionResolver接口處理shiro異常 -->
    <bean class="異常攔截"></bean>
</beans>

五、重點實現shiro.xml配置的內容(必須)

(1)實現AuthorizingRealm類(登錄授權認證實現類)

實現shiro.xml中配置

    <!-- 登錄認證、授權 -->
    <bean id="userAuthorization" class="登錄授權認證實現類">

doGetAuthorizationInfo 訪問需要權限鏈接時進入(授權)

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pCollection) {
        //用戶授權 
String userName = SecurityUtils.getSubject().getPrincipal().toString();
        if (!StringUtils.isEmpty(userName)) {
              //獲取用戶權限字符串
            String[] actionList ={};
            for (String str : actionList) {
                //設置用戶權限
                info.addStringPermission(str);
            }
            //返回權限信息
            return info;
        }
        return null;
}

doGetAuthenticationInfo 用戶登錄時進入(驗證)

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //用戶認證
UsernamePasswordToken uPasswordToken = (UsernamePasswordToken) token;
String userName = uPasswordToken.getUsername();
        char[] c = uPasswordToken.getPassword();
        if (userName == null || userName.equals("") || c == null ||c.length==0) { 
//用戶名或密碼爲空,直接退出
            return null;
}
        //這裏查詢用戶,處理業務需求 
UserAuthority userAuthority=new UserAuthority();
         userAuthority.setUserName(“aaa”);
         SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userAuthority, passwork,getName());
        return info;
}

UserAuthority 類解釋
定義所需保存的數據(根據自己需求定義一個類即可)

如何doGetAuthenticationInfo登陸驗證

public Object login(String userName, String passwork) {
           //獲取主體
        Subject subject=SecurityUtils.getSubject();
        AuthenticationToken token=new UsernamePasswordToken(username, password);
        //開始認證
        subject.login(token);
        //獲取用戶認證
        boolean bool=subject.isAuthenticated();
        System.out.println("認證成功:"+bool);
        ModelAndView mv = new ModelAndView();
        if (bool) {
            return "redirect:/jsp/Main.jsp";
        } else {
            return "redirect:/jsp/login.jsp";
        }
 }

當前臺點擊登錄時,訪問login方法,獲取主體,並設置用戶密碼令牌,當代碼執行到
subject.login(token)時觸發doGetAuthenticationInfo方法,並得到的令牌AuthenticationToken ,因爲得到的令牌是登錄時設置的用戶密碼令牌,因此將該令牌token轉回UsernamePasswordToken。接着查詢數據庫獲取用戶,並將查詢到的用戶信息設置到SimpleAuthenticationInfo,並返回SimpleAuthenticationInfo。緊接着繼續執行subject.login(token)後的方法。

(2)設置整個系統的所有權限

實現shrio.xml中的

<!--加載攔截鏈接-->
<bean id="chainDefinitionSectionMetaSource" class="攔截連接實現類"></bean>
利用spring的FactoryBean<Ini.Section>動態獲取數據庫保存的鏈接,
實現方法getObject(),該方法會在spring容器加載時觸發。
private StringBuffer filter = new StringBuffer();

public static final String PREMISSION_STRING = " = authc,perms[\"{0}\"]\n";

public Section getObject() {
        //添加字符串格式:url=key,如url=authc,perms["權限字符串"]
        filter.append("/jsp/login.jsp=anon\n");//這裏\n必須寫上,防止拼接成一行,導致Ini.Section解釋成一條鏈接
        //這裏完成權限字符串獲取,拼接(數據庫查詢)
        filter.append("");
        Ini ini = new Ini();
        ini.load(fiter);
        Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
        return section;
}

爲什麼使用類Ini.Section?

從org.apache.shiro.spring.web.ShiroFilterFactoryBean類中,
可以看出filterChainDefinitionMap屬性的類型是Map<String,String>,
因此繼承FactoryBean<>的泛型必須爲Map<String,String>或者它的子類,
Ini.Section是shrio對Map<String,String>的封裝,
也可以寫成FactoryBean<Map<String,String>>,
key爲url,value爲權限字符(authc,perms["權限字符串"])

ShiroFilterFactoryBean類
這裏寫圖片描述
Ini.Section類
這裏寫圖片描述

六、根據需求實現shiro配置

剩下Shrio.xml中沒實現的就是按自己需求來配置

(1)PermissionsAuthorizationFilter重寫(權限字符串重寫)

重寫shrio.xml中的

引入重寫內容,在shiro攔截器中加入filters屬性
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="filters">
        <map>
            <entry key="perms">
                <bean class="重寫權限類"></bean>
            </entry>
        </map>
    </property>
</bean>

原來的攔截方式:
(如果長度爲1,判斷是否有改權限字符串,長度不爲1判斷是否擁有所有的權限字符串,
如:/url= authc,perms[“12”,”21”],當訪問/url時,就必須擁有”12”,”21”,這兩個字符串)

public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
        Subject subject = getSubject(request, response);
        String[] perms = (String[]) mappedValue;
        boolean isPermitted = true;
        if (perms != null && perms.length > 0) {
            if (perms.length == 1) {
                if (!subject.isPermitted(perms[0])) {
                    isPermitted = false;
                }
            } else {
                if (!subject.isPermittedAll(perms)) {
                    isPermitted = false;
                }
            }
        }
        return isPermitted;
}

重寫後的攔截方式(只需判斷是否擁有其中一個權限字符串就可以訪問,
如/url= authc,perms[“12”,”21”],當訪問/url時,擁有”12”或”21”就可以訪問 )

 public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
        Subject subject = getSubject(request, response);
        String[] perms = (String[]) mappedValue;
        if (perms != null && perms.length > 0) {
            boolean[] bs = subject.isPermitted(perms); //返回判斷數組
             for (boolean b : bs) {
                if(b){ 
                    //說明有權限,直接返回                    
return true;
                }
         }
        }
        return false;
}

(2)異常攔截

重寫shiro.xml配置文件

引入重寫內容,在shiro.xml文件,直接追加bean
<!-- 使用springmvc HandlerExceptionResolver接口處理shiro異常 -->
<bean class="異常攔截"></bean>
   當訪問鏈接時出現異常如何處理

實現springmvc的HandlerExceptionResolver異常處理

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object,Exception ex){
    ex.printStackTrace();
    if(ex instanceof UnauthorizedException) {  
        //沒有認證
        System.out.println("沒有認證");
        ModelAndView mv = new ModelAndView("redirect:/jsp/login.jsp");
         return mv;  
     }else if(ex instanceof UnknownAccountException||ex instanceof AuthenticationException) {  
            //登錄過期沒有權限  跳回登錄頁面
            System.out.println("登錄過期");
            ModelAndView mv = new ModelAndView("redirect:/jsp/login.jsp");
            mv.addObject("Result", "您需要登錄");
        return mv;
     } else if(ex instanceof HostUnauthorizedException){
             System.out.println("沒有接口權限");
          return new ModelAndView("exception/noPermission");  
     }
        return null;  
}

標題1中介紹的SecurityManager 剩下內容

  Session Manager(創建和管理用戶Session生命週期)。
  CacheManager(緩存Shiro使用的鑑權數據)。

如有需要繼續往下面配置中添加即可。

    <!-- 安全接口 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userAuthorization" />
        <!--需要添加-->
    </bean>

這裏就不配置了。

七、java文件和配置下載

文件下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章