項目環境:IntelliJ IDEA、Maven
涉及技術:
Spring Security 實現 (用戶名+密碼認證)+(手機號+短信認證) —-基於瀏覽器session
Spring Social 實現第三方認證 ——-基於瀏覽器session
Spring Security OAuth提供這三種方式的在app上的實現 —-基於token、作用:創建、管理、分發token用的
項目背景:
企業級認證和授權需求:
同時支持多種認證方式(用戶名/密碼,短信,QQ,微信)
同時支持多種前端渠道(瀏覽器,App)
支持集羣環境,跨應用工作(SSO),session的數量,控制用戶的權限,防護與身份認證相關的攻擊
架構介紹:
- rz-security: 主模塊 (pom類型、包含下面四個子模塊、目的:統一執行命令:打包、測試)
- rz-security-core:核心業務邏輯(基本的安全認證方式:表單登錄,手機驗證碼登錄,第三方登錄)
- rz-security-browser 、rz-security-app:(瀏覽器安全特定代碼、app安全特定代碼)
- rz-security-demo: 樣例程序
項目初始化:
1. rz-security的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rz.security</groupId>
<artifactId>rz-security</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!--當改變項目的版本時候,只需要改變該屬性-->
<properties>
<rz-security-version>1.0-SNAPSHOT</rz-security-version>
</properties>
<dependencyManagement>
<dependencies>
<!--maven管理spring依賴的版本-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!--maven的編譯插件-->
<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>../rz-security-core</module>
<module>../rz-security-browser</module>
<module>../rz-security-app</module>
<module>../rz-security-demo</module>
</modules>
</project>
2 . rz-security-core的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>rz-security</artifactId>
<groupId>com.rz.security</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../rz-security/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rz-security-core</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- app安全認證 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<!-- 存儲token,系統用戶和第三方用戶做一個綁定,存儲綁定關係 -->
<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>
<!-- spring social實現第三方登錄相關 -->
<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>
<!-- java操作工具包 -->
<!--字符串操作-->
<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>
</dependencies>
</project>
3 . rz-security-bowser的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>rz-security</artifactId>
<groupId>com.rz.security</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../rz-security/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rz-security-browser</artifactId>
<dependencies>
<dependency>
<artifactId>rz-security-core</artifactId>
<groupId>com.rz.security</groupId>
<version>${rz-security-version}</version>
</dependency>
<!-- 集羣環境下的session管理 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
</dependencies>
</project>
4 . rz-security-app的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>rz-security</artifactId>
<groupId>com.rz.security</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../rz-security/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rz-security-app</artifactId>
<dependencies>
<dependency>
<artifactId>rz-security-core</artifactId>
<groupId>com.rz.security</groupId>
<version>${rz-security-version}</version>
</dependency>
</dependencies>
</project>
5 . rz-security-demo的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>rz-security</artifactId>
<groupId>com.rz.security</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../rz-security/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rz-security-demo</artifactId>
<dependencies>
<dependency>
<groupId>com.rz.security</groupId>
<artifactId>rz-security-browser</artifactId>
<version>${rz-security-version}</version>
</dependency>
</dependencies>
</project>
項目框架初步搭建完成,接下來在rz-security-demo中編寫RESTFul API