OAuth2.0⼊门(⼆)——spring-security-oauth2⼊门例⼦
JDBC。。。
前⾔:本⽂是OAuth2.0实践篇,阅读之前需要先掌握OAuth2.0基本原理,原理介绍见:
本章将采⽤微服务架构⽅式,将OAuth2-Demo拆分成三个模块:oauth2-authentication-server(作为授权认证中⼼)、oauth2-resource-server(作为资源服务器)、oauth-client(作为第三⽅应⽤,模拟如何获取Token访问资源)。
⼀、项⽬总结构
其中oauth2-demo是其他模块的Parent模块,定义了⼀些通⽤的Jar包。完整的pom⽂件如下:
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
3 xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.1.6.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.scb</groupId>
12 <artifactId>oauth2-demo</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>oauth2-demo</name>
15 <description>Demo project for Spring Boot</description>
16
17 <properties>
18 <java.version>1.8</java.version>
19 </properties>
20
21 <dependencies>
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter</artifactId>
25 </dependency>
26
27 <dependency>
28 <groupId>ch.qos.logback</groupId>
29 <artifactId>logback-classic</artifactId>
30 <version>1.1.11</version>
31 </dependency>
32
33 <dependency>
34 <groupId>org.projectlombok</groupId>
35 <artifactId>lombok</artifactId>
36 <optional>true</optional>
37 </dependency>
38
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-test</artifactId>
42 <scope>test</scope>
43 </dependency>
44 </dependencies>
45
46 <build>
47 <plugins>
48 <plugin>
validation框架49 <groupId>org.springframework.boot</groupId>
50 <artifactId>spring-boot-maven-plugin</artifactId>
51 </plugin>
52 </plugins>
53 </build>
54
55 <modules>
56 <module>oauth2-authentication-server</module>
57 <module>oauth2-resource-server</module>
58 <module>oauth-client</module>
59 </modules>
60
61</project>
⼆、oauth2-authentication-server 模块
oauth2-authentication-server 模块是作为全局的授权认证中⼼,pom⽂件如下:
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
3 xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>com.scb</groupId>
7 <artifactId>oauth2-demo</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11
12 <artifactId>oauth2-authentication-server</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>oauth2-authentication-server</name>
15 <description>Demo project for Spring Boot</description>
16
17 <properties>
18 <java.version>1.8</java.version>
19 </properties>
20
21 <dependencies>
22 <!-- 阿⾥系的Druid依赖包 -->
23 <dependency>
24 <groupId>com.alibaba</groupId>
25 <artifactId>druid-spring-boot-starter</artifactId>
26 <version>1.1.9</version>
27 </dependency>
28 <!-- Druid 依赖 log4j包 -->
28 <!-- Druid 依赖 log4j包 -->
29 <dependency>
30 <groupId>log4j</groupId>
31 <artifactId>log4j</artifactId>
32 <version>1.2.17</version>
33 </dependency>
34 <dependency>
35 <groupId>com.h2database</groupId>
36 <artifactId>h2</artifactId>
37 <scope>runtime</scope>
38 </dependency>
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-data-jpa</artifactId>
42 </dependency>
43
44 <dependency>
45 <groupId>org.springframework.boot</groupId>
46 <artifactId>spring-boot-starter-thymeleaf</artifactId>
47 </dependency>
48
49 <dependency>
50 <groupId>org.springframework.boot</groupId>
51 <artifactId>spring-boot-starter-security</artifactId>
52 </dependency>
53
54 <dependency>
55 <groupId>org.springframework.boot</groupId>
56 <artifactId>spring-boot-starter-web</artifactId>
57 </dependency>
58
59 <dependency>
60 <groupId>org.springframework.security.oauth</groupId>
61 <artifactId>spring-security-oauth2</artifactId>
62 <version>2.3.4.RELEASE</version>
63 </dependency>
64
65 <dependency>
66 <groupId>org.springframework.security</groupId>
67 <artifactId>spring-security-test</artifactId>
68 <scope>test</scope>
69 </dependency>
70 </dependencies>
71
72 <build>
73 <plugins>
74 <plugin>
75 <groupId>org.springframework.boot</groupId>
76 <artifactId>spring-boot-maven-plugin</artifactId>
77 </plugin>
78 </plugins>
79 </build>
80
81</project>
这⾥,除了导⼊spring-boot-starter-security和spring-security-oauth2认证框架外,还需要使⽤H2内存数据库来存储⽤户和⾓⾊信息及OAuth2的表。
先来看看l⽂件:
1spring:
2 h2:
3 console:
4 path: /h2-console
5 enabled: true
6 settings:
7 web-allow-others: true
8 jpa:
9 generate-ddl: false
10 show-sql: true
11 hibernate:
12 ddl-auto: none
13 datasource:
14 platform: h2
15 schema: classpath:schema.sql
16 data: classpath:data.sql
17 url: jdbc:h2:~/auth;AUTO_SERVER=TRUE
18 username: sa
19 password:
20 type: com.alibaba.druid.pool.DruidDataSource
21 druid:
22 min-idle: 2
23 initial-size: 5
24 max-active: 10
25 max-wait: 5000
26 validation-query: select 1
27 resources:
28 static-locations: classpath:/templates/,classpath:/static/
29 thymeleaf:
30 prefix: classpath:/templates/
31 suffix: .html
32 mode: HTML5
33 servlet:
34 content-type: text/html
35 cache: false
36server:
37 port: 8080
38logging:
39 pattern:
40 level: debug
在yml⽂件中,我们定义了datasource为H2,并指定了schema、data⽂件,这样在项⽬运⾏时会执⾏相应的sql。其中schema.sql⽂件如下:
1/* 1、存放⽤户认证信息及权限 */
2drop table if exists authority;
3CREATE TABLE authority (
4 id integer,
5 authority varchar(255),
6 primary key (id)
7);
8
9drop table if exists credentials;
10CREATE TABLE credentials (
11 id integer,
12 enabled boolean not null,
13 name varchar(255) not null,
14 password varchar(255) not null,
15 version integer,
16 primary key (id)
17);
18
19drop table if exists credentials_authorities;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论