SpringSecurity前傳--與springboot2.0初步集成

??相信大家都知道登錄功能,初級程序員的腦海里面就是賬號,密碼,登錄按鈕然后到后臺數(shù)據(jù)庫查詢一下信息就OK了。但其實登錄沒有這么簡單,因為:

1.我們所面臨的登錄認證模式很復(fù)雜

  • 1.1短信的登錄
  • 1.2微信的登錄
  • 1.3QQ的登錄

2.我們要支持多種前端渠道

  • 2.1網(wǎng)頁登錄
  • 2.2APP登錄

3.支持集群環(huán)境,跨應(yīng)用工作,Session控制,控制用戶權(quán)限,說防護與身份認證相關(guān)的攻擊

??認證和授權(quán)如此復(fù)雜,因此也誕生了作者對SpringSecurity系列的學(xué)習(xí)。本系列將主要從SpringSecurity,Spring Social,Spring Security OAuth來給大家進行講解。其中SpringSecurity作為底層的實現(xiàn),我將在后面的文章中講解它是如何實現(xiàn)用戶名+密碼認證和手機號+短信認證的。Spring Social主要用于第三方認證,比如QQ,微信的接入。APP和后端服務(wù)器通訊的時候無法存儲Session的問題,所以我們采用token的方式來存儲登錄用戶的認證信息。使用Spring Security OAuth來創(chuàng)建、分發(fā)、管理Token信息。


4.開始開發(fā)

4.1代碼架構(gòu)

代碼結(jié)構(gòu)

4.1.1工程圖:

4.1.2imooc-security

??主模塊,只有pom文件,用于打包和發(fā)布

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Cairo-SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <modules>
        <module>../imooc-security-app</module>
        <module>../imooc-security-browser</module>
        <module>../imooc-security-core</module>
        <module>../imooc-security-demo</module>
    </modules>

??這里引入了spring-cloud以及io.spring.platform的相關(guān)東西,io.spring.platform的好處是可以幫助我們在引入類似hibernate和spring的包的時候可以不用考慮版本不兼容的問題。最后的modules,引入了圖中的其他4個模塊。

4.1.3imooc-security-core

??存放核心登錄邏輯,在本篇文章中,我們主要是先讓我們的工程跑起來,還不會把具體的代碼放進去,我會在后續(xù)的文章中漸進式的將相關(guān)代碼邏輯放進去。

4.1.3.1pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-web</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
    </dependencies>
    <parent>
        <groupId>com.imooc.security</groupId>
        <artifactId>imooc-security</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../imooc-security</relativePath>
    </parent>

這里面我們放入了對redis操作的相關(guān)包,更重要的是放入了spring-social,spring-security的相關(guān)依賴。

4.1.4imooc-security-app

依賴了imooc-security-core,所以它的pom.xml很簡單

    <parent>
        <groupId>com.imooc.security</groupId>
        <artifactId>imooc-security</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../imooc-security</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.imooc.security</groupId>
            <artifactId>imooc-security-core</artifactId>
            <version>${imooc.security.version}</version>
        </dependency>
    </dependencies>

4.1.4imooc-security-browser

<parent>
        <groupId>com.imooc.security</groupId>
        <artifactId>imooc-security</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../imooc-security</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.imooc.security</groupId>
            <artifactId>imooc-security-core</artifactId>
            <version>${imooc.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        
    </dependencies>

??browser除了依賴core還依賴了spring-session-data-redis,這樣我們就可以把session存放到redis中,并在redis中對session里面的內(nèi)容作操作。為什么app不放這個引用呢。前面我們說了,app和后臺交互的時候,是沒有session的,是通過token來管理的。

4.1.5imooc-security-demo

4.1.5.1pom.xml

    <parent>
        <groupId>com.imooc.security</groupId>
        <artifactId>imooc-security</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../imooc-security</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.imooc.security</groupId>
            <artifactId>imooc-security-browser</artifactId>
            <version>${imooc.security.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.3.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>demo</finalName>
    </build>

這里我們暫時先引入browser,后期在講token方面的東西的時候會引入app,另外我們這里還加入了spring-boot-maven-plugin,讓我們打包成可運行的jar包,如果說大家不加后面的build,可以看到你打出來的jar包是沒有把相關(guān)的依賴放進去的,是沒法直接執(zhí)行的。

4.1.5.2DemoApplication

@SpringBootApplication
@RestController
@EnableAutoConfiguration(exclude = {
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
public class DemoApplication {
    public static void main(String[] args) {
        try {
            SpringApplication.run(DemoApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        } catch(Error e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/helloword")
    public String helloword() {
        return "hello spring security";

    }
}

用過SpringBoot的同學(xué)應(yīng)該很熟悉這段配置了,這里我主要講一下@EnableAutoConfiguration這個注解為什么要加在頭上,如果不加,我們直接運行我們的工程,然后訪問helloword,我們會看到下面的示意圖:


默認配置

這樣會讓我們輸入用戶名和密碼,加上了這段注解后,就會看到瀏覽器上現(xiàn)實hello spring security,如圖所示:


不驗證

現(xiàn)在我們使用的是spring-boot2.0之后的版本,以前的版本中,我們可以在application.properties中寫上security.basic.enable=false來達到同樣的效果。

4.1.5.3application.properties

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/test?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = 
spring.datasource.password = 
spring.session.store-type=none
#security.basic.enabled=false

在這里我們加入了對數(shù)據(jù)庫的配置,如果不加,我們的springBoot會因為引入了spring-boot-starter-jdbc報錯,為什么引入spring.session.store-type=none,這是因為我們引入了spring-boot-starter-data-redis,spring-session-data-redis,我們要把session放入redis中,但是現(xiàn)在我們還沒有搭建redis,為了讓我們的工程跑起來,所以我們這里設(shè)置為none。
??本篇文章,我們主要是使用spring-boot2.0先把基本框架搭建好,后面的文章中,我們將深入講解其他內(nèi)容。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容