天天看點

SpringSecurity超詳細入門介紹

權限管理是我們項目中必不可少的一環,實際項目中我們可以自己設計權限管理子產品,也可以使用市面上成熟的權限管理架構,比如 shiro或者 SpringSecurity等,前面已經詳細的介紹過了 shiro 的使用,本文開始就給大家詳細的來介紹下SpringSecurity的使用。

内容包括

spring+springmvc基于配置的方式詳細介紹SpringSecurity

springboot整合SpringSecurity

OAuth2的使用

一、權限管理概念

 權限管理,一般指根據系統設定的安全規則或者安全政策,使用者可以通路而且隻能通路自己被授權的資源。權限管理幾乎出現在任何系統裡面,前提是需要有使用者和密碼認證的系統。

在權限管理的概念中,有兩個非常重要的名詞:

認證:通過使用者名和密碼成功登陸系統後,讓系統得到目前使用者的角色身份。

授權:系統根據目前使用者的角色,給其授予對應可以操作的權限資源。

二、完成權限管理需要三個對象

使用者:主要包含使用者名,密碼和目前使用者的角色資訊,可實作認證操作。

角色:主要包含角色名稱,角色描述和目前角色擁有的權限資訊,可實作授權操作。

權限:權限也可以稱為菜單,主要包含目前權限名稱,url位址等資訊,可實作動态展示菜單。

注:這三個對象中,使用者與角色是多對多的關系,角色與權限是多對多的關系,使用者與權限沒有直接關系,二者是通過角色來建立關聯關系的。

三、初識Spring Security

1 Spring Security概念

 Spring Security是spring采用AOP思想,基于servlet過濾器實作的安全架構。它提供了完善的認證機制和方法級的授權功能。是一款非常優秀的權限管理架構。

2 快速入門案例

 入門案例我們是通過spring+springmvc環境來搭建的。是以需要先準備項目環境

2.1環境準備

2.1.1 建立web項目

 通過idea工具建立一個基于maven的web項目

SpringSecurity超詳細入門介紹
2.1.2 導入相關的依賴

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
</dependencies>      

2.1.3 建立相關配置檔案

 相關的配置檔案有spring的,springmvc的和log4j.properties

spring:

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

    <!-- 配置掃描路徑
    <context:component-scan base-package="com.dpb.security"
                            use-default-filters="true">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>-->

</beans>      

springmvc:

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

    <!-- 配置掃描路徑-->
    <context:component-scan base-package="com.dpb.security.controller"
                            use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>      

log4j.properties

log4j.rootCategory=INFO, stdout
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n      

2.1.4 web.xml設定

 在web.xml檔案中配置spring和SpringMVC容器的加載

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 初始化spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- post亂碼過濾器 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 前端控制器 -->
  <servlet>
    <servlet-name>dispatcherServletb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置檔案預設在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletb</servlet-name>
    <!-- 攔截所有請求jsp除外 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>      

2.2 SpringSecurity整合

準備好Spring+SpringMVC的環境後我們就可以整合SpringSecurity了

2.2.1.相關jar作用介紹

spring-security-core.jar:核心包,任何SpringSecurity功能都需要此包

spring-security-web.jar:web工程必須,包含過濾器和相關的web安全基礎結構代碼

spring-security-config.jar:用于解析xml配置檔案,用到SpringSecurity的xml配置檔案的就要用到此包

spring-security-taglibs.jar:SpringSecurity提供的動态标簽庫,jsp頁面可以用

因為maven項目的依賴傳遞性,我們在項目中隻需要設定 config和taglibs這兩個依賴即可

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-taglibs</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>      
SpringSecurity超詳細入門介紹

2.2.2.過濾器配置

 我們需要在容器啟動的時候加載相關的過濾器,是以需要在web.xml中添加如下配置

<!-- 配置過濾器鍊 springSecurityFilterChain名稱固定-->
<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>      

2.2.3.SpringSecurity配置檔案設定

 單獨添加一個SpringSecurity的配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd">

    <!--
        auto-config="true" 表示自動加載SpringSecurity的配置檔案
        use-expressions="true" 使用Spring的EL表達式
     -->
    <security:http auto-config="true" use-expressions="true">
        <!--
            攔截資源
            pattern="/**" 攔截所有的資源
            access="hasAnyRole('role1')" 表示隻有role1這個角色可以通路資源
         -->
        <security:intercept-url pattern="/**" access="hasAnyRole(ROLE_USER)"></security:intercept-url>
    </security:http>

    <!-- 設定認證使用者來源  noop:SpringSecurity中預設 密碼驗證是要加密的  noop表示不加密 -->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="zhang" password="{noop}123" authorities="ROLE_USER"></security:user>
                <security:user name="lisi" password="{noop}123" authorities="ROLE_ADMIN"></security:user>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>      

2.2.4.導入SpringSecurity的配置檔案

SpringSecurity的配置檔案需要加載到Spring容器中,是以可以通過import來導入

<import resource="spring-security.xml"></import>      

2.2.5.啟動項目測試

 我們通過tomcat插件來啟動項目

<plugins>
  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <port>8080</port> <!-- 通路端口-->
      <path>/</path>    <!-- 通路路徑-->
    </configuration>
  </plugin>      
SpringSecurity超詳細入門介紹

搞定~