ApiBoot零代碼整合Spring Security的JDBC方式獲取AccessToken

ApiBoot Security內部提供了兩種方式進行讀取須要認證的用戶信息,在以前的文章中講到過ApiBoot Security使用內存方式(memory)不寫一行代碼就能夠實現用戶的認證並獲取AccessToken,那咱們使用JDBC方式是否是也是這麼的簡單呢?html

若是你還對ApiBoot不瞭解,能夠經過如下的途徑來獲取幫助。java

ApiBoot Security的認證方式

有一些同窗可能對ApiBoot Security的兩種認證方式還不太瞭解,下面介紹下這兩種認證方式的區別。mysql

內存方式

內存方式(memory)是將用戶信息(用戶名、密碼、角色列表)在application.yml文件內配置,可配置多個用戶,項目啓動後將用戶信息加載到內存中,用於獲取AccessToken時的認證。git

數據庫方式

數據庫方式(jdbc)是將用戶信息保存到數據庫內,ApiBoot Security定義了一個默認表結構的用戶信息數據表,咱們能夠從官網找到建表語句直接在本身的數據庫內建立便可,固然若是不使用默認的表結構能夠進行自定義讀取用戶信息。web

注意:在數據庫內存放用戶的密碼必須是經過BCryptPasswordEncoder加密後的密文字符串。spring

建立項目

ApiBoot Security的兩種認證方式概念明白後,咱們開始說下怎麼才能使用JDBC方式進行用戶認證,咱們先來使用IDEA開發工具建立一個SpringBoot項目。sql

添加ApiBoot統一版本

在使用ApiBoot內提供的組件依賴時,首先咱們須要在pom.xml文件內添加ApiBoot統一版本,以下所示:數據庫

<properties>
    <java.version>1.8</java.version>
    <!--ApiBoot版本號-->
    <apiboot.version>2.1.5.RELEASE</apiboot.version>
</properties>
<dependencyManagement>
    <dependencies>
        <!--ApiBoot版本依賴-->
        <dependency>
            <groupId>org.minbox.framework</groupId>
            <artifactId>api-boot-dependencies</artifactId>
            <version>${apiboot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>複製代碼

添加ApiBoot Security依賴

在項目pom.xml文件添加ApiBoot Security依賴,以下所示:api

<!--ApiBoot Security OAuth-->
<dependency>
  <groupId>org.minbox.framework</groupId>
  <artifactId>api-boot-starter-security-oauth-jwt</artifactId>
</dependency>複製代碼

添加JDBC相關依賴

咱們本章使用MySQL數據庫作演示,咱們須要添加相關的數據庫依賴以及數據庫鏈接池依賴,因爲ApiBoot Security讀取內置的默認用戶表結構使用的是DataSource,因此咱們還須要添加一個能夠實例化DataSource的依賴,能夠選擇api-boot-starter-mybatis-enhance或者spring-boot-starter-jdbc,在pom.xml添加依賴以下所示:mybatis

<!--SpringBoot Web-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MySQL-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

<!--Hikari-->
<dependency>
  <groupId>com.zaxxer</groupId>
  <artifactId>HikariCP</artifactId>
</dependency>
<!--SpringBoot JDBC-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>複製代碼

注意:spring-boot-starter-web這個依賴不可少,在ApiBoot AutoConfiguration內須要一些Web的依賴類。

建立默認用戶表結構

本章使用ApiBoot Security提供的默認用戶表結構,訪問官方文檔查看3.3 使用內置表結構的用戶,將建表語句在本身數據庫內執行建立表信息,建立後添加一條用戶信息,以下所示:

INSERT INTO `api_boot_user_info` VALUES (1,'admin','暱稱','$2a$10$RbJGpi.v3PwkjrYENzOzTuMxazuanX3Qa2hwI/f55cYsZhFT/nX3.',NULL,NULL,NULL,'N','Y','O','2019-11-29 06:14:44');複製代碼

配置數據源

依賴添加完成後咱們在application.yml配置文件內進行配置數據源,以下所示:

spring:
  application:
    name: apiboot-security-customize-select-user
  # 數據源配置
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
server:
  port: 9090複製代碼

配置ApiBootSecurity JDBC方式

因爲ApiBoot Security默認使用memory用戶認證讀取方式,咱們須要在application.yml文件內進行修改,以下所示:

# ApiBoot相關配置
api:
  boot:
    # 啓用ApiBoot Security 的JDBC方式
    security:
      away: jdbc複製代碼

運行測試

項目配置完成,下面咱們經過XxxApplication方式啓動項目。

在獲取AccessToken以前咱們要知道的一點,ApiBoot Security內部默認集成了OAuth2,並且還默認配置了clientIdclientSecret客戶端基本信息,默認值分別是ApiBootApiBootSecret

clientId = ApiBoot
clientSecret = ApiBootSecret複製代碼

若是你對ApiBoot OAuth其餘功能有興趣能夠查看ApiBoot OAuth文檔瞭解詳情。

獲取AccessToken

因爲學習者的本機環境不一樣,下面採用兩種方式進行獲取AccessToken

CURL方式

執行以下命令獲取AccessToken

➜ ~ curl -X POST ApiBoot:ApiBootSecret@localhost:9090/oauth/token\?grant_type\=password\&username\=admin\&password\=123456
{"access_token":"d9cb97ee-d1bf-42e1-a7a0-c1002df48c52","token_type":"bearer","refresh_token":"db9e9d52-cbe3-4379-a5f2-ffaa34681c01","expires_in":2884,"scope":"api"}複製代碼

PostMan方式

注意:獲取AccessToken的請求方式爲POST.

敲黑板,劃重點

ApiBoot Security不只內存方式能夠實現零代碼的方式進行集成Spring SecurityOAuth2JDBC方式一樣也能夠,不過要根據ApiBoot的約定建立用戶表。

代碼示例

本篇文章示例源碼能夠經過如下途徑獲取,目錄爲SpringBoot2.x/apiboot-security-customize-select-user

做者我的 博客

使用開源框架 ApiBoot 助你成爲Api接口服務架構師

相關文章
相關標籤/搜索