Official: https://spring.io/projects
Video: https://www.bilibili.com/video/BV1sE411w7NM?p=3
Construction of the project
Module relationship:
artifactId | groupId | Types of | Remark |
---|---|---|---|
v-security | cn.vshop.security | pom | The main module |
v-security-core | cn.vshop.security | jar | The core business logic |
v-security-browser | cn.vshop.security | jar | Browser security-specific module |
v-security-app | cn.vshop.security | jar | app-related specific module |
v-security-demo | cn.vshop.security | jar | Sample Program |
idea to create a multi-module Maven project
# v-security
Parent module, provides an environment for other modules
pom.xml
platform-bom
spring-cloud-dependencies
GA version of the selected dependent on:
GA
: General Availability, officially released version,The official recommended to use this version. In foreign countries are used to illustrate the GArelease
version.PRE
.: Preview, internal test version is mainly for developers and testers to test and find BUG use is not recommended;SNAPSHOT
: Snapshots, you can use stable and continued improved version.
<dependencyManagement>
<!--替我们管理maven依赖版本,避免版本不兼容-->
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
maven compiler plugin specified JDK version 1.8
<build>
<plugins>
<!--指定编译版本,指定为JDK1.8-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!-- 源代码使用的JDK版本 -->
<source>1.8</source>
<!-- 需要生成的目标class文件的编译版本 -->
<target>1.8</target>
<!-- 字符集编码 -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Check for automatic building blocks father and son relationship, if there is no need to manually build
<!-- v-security 的 pom 文件中需要有 -->
<modules>
<module>v-security-core</module>
<module>v-security-browser</module>
<module>v-security-app</module>
<module>v-security-demo</module>
</modules>
<!-- v-security-app、v-security-core、v-security-browser、v-security-demo 的 pom 文件中需要有 -->
<parent>
<artifactId>v-security</artifactId>
<groupId>cn.vshop.security</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
# v-security-core
pom.xml
Spring cloud oauth2 study - the first DEMO
<!--作为验证服务包含了security 和 oauth2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
(Above) this will depend on the introduction of a large number of jar package for us
Including: spring-security-related dependency, and spring-security-oauth2
(important)
And then introduced into the two storage related dependencies: jdbc
andredis
<!--和存储相关的依赖:jdbc/redis,会帮我们做redis和数据库的相关配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Note that there is no need to write versions, because the father of the project: spring-plaform
with spring-cloud
At work。
Introducing spring-social dependence
Log in to do a third party, such as qq, weixin, github.
(Later used to speak in detail)
Spring Social Reference - Getting Spring Social
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
</dependency>
Finally, add three apache commons kit provided
Apache commons (Java common toolkit) Introduction
<!--apache commons 工具包-->
<!--提供了许多许多通用的工具类集,提供了一些java.lang中类的扩展功能-->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<!--提供一个类包来扩展和增加标准的 Java Collection框架-->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<!--提供对 Java 反射和自省API的包装-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
# v-security-app
Reference core module as dependent
Notice that you specify the version
<!--其他块:v-security-app、v-security-browser中添加-->
<dependencies>
<dependency>
<groupId>cn.vshop.security</groupId>
<artifactId>v-security-core</artifactId>
<version>${v.security.version}</version>
</dependency>
</dependencies>
Version specified in the parent module
<!--父模块:v-security中添加-->
<properties>
<v.security.version>1.0-SNAPSHOT</v.security.version>
</properties>
# v-security-browser
<!--其他块:v-security-app、v-security-browser中添加-->
<dependencies>
<dependency>
<groupId>cn.vshop.security</groupId>
<artifactId>v-security-core</artifactId>
<version>${v.security.version}</version>
</dependency>
</dependencies>
The introduction of spring-session
Here the browser security is based on the session.
thereforeNeed spring-session, to help us in the 集群
environment, do session management
<!-- core 模块中配置,所以不需要
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
# v-security-demo
Recalling this figure, demo, and are dependent browser app
Talk about the browser, therefore,The first add-dependent browser
<dependencies>
<dependency>
<groupId>cn.vshop.security</groupId>
<artifactId>v-security-browser</artifactId>
<version>${v.security.version}</version>
</dependency>
</dependencies>
Test: Hello World
domain name | ip | port | Remark |
---|---|---|---|
vdb.cn | 192.168.64.33 | 3306 | database |
vshop.cn、localhost | 192.168.64.1、127.0.0.1 | 8080 | Client |
Start writing class:
package cn.vshop.security;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author alan smith
* @version 1.0
* @date 2020/3/25 0:27
*/
@SpringBootApplication
@RestController
public class DemoApplication {
/**
* 启动类
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "hello spring security";
}
}
Configuring the dataSource (data source):
application.yml added
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://vdb.cn:3306/vsdb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
username: root
password: root
否则,报错:Cannot determine embedded database driver class for database type NONE
Spring Session configuration memory (Store)
We first spring to close the session management (use back on again)
spring:
session:
store-type: none
# dataSource ...
Otherwise, an error (below): No Spring Session store is configured : set the 'spring.session.store-type' property
the source of the error is: spring-session dependent on v-security.browser in.
Do behind the cluster of session management
(Click to enlarge view)