开发环境:
安装JDK8
安装STS
http://spring.io/tools/sts/
安装MySql
服务器 https://www.mysql.com/downloads/
客户端 Navicat Premium 12
代码结构:
项目:Maven多模块项目
wtrover-security:主模块
wtrover-security-core:核心业务逻辑
表单登陆,手机验证码登陆,第三方登陆
wtrover-security-browser
浏览器安全特定代码
wtrover-security-app
app安全特定代码
wtrover-security-demo
样例程序
创建项目
Eclipse
1.父项目wtrover-security,注意Packaging选pom,因为作为父项目本事并不包含代码,它只是用来打包的。
再把其他几个项目都建起来,注意剩下的四个项目Packaging都选择jar。
2.挨个配置pom文件
wtrover-security添加依赖Spring IO平台,作用是替我们管理maven依赖的版本,写依赖时不用写版本。除了IO还需要添加Spring Cloud。有了这两个我们整个项目的版本就应该被控制起来了。因为整个项目会在JDK8下编写,所以要指定一下编译的版本。
最后我们要把那四个项目加到wtrover-security里,作为它的子模块。在Overview-Modules点Add,把四个项目选上,注意勾选上"Update POM parent section in selected projects"。
<properties> <wtrover.security.version>1.0.0-SNAPSHOT</wtrover.security.version> </properties> <dependencyManagement> <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> <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>../wtrover-security-app</module> <module>../wtrover-security-browser</module> <module>../wtrover-security-core</module> <module>../wtrover-security-demo</module> </modules>
wtrover-security-core
<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> <!-- spring social --> <!-- 提供Java 配置 --> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-config</artifactId> </dependency> <!-- 提供社交连接框架和OAuth 客户端支持 --> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-core</artifactId> </dependency> <!-- 提供社交安全支持 --> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-security</artifactId> </dependency> <!-- 管理web应用程序的连接 --> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-web</artifactId> </dependency> <!-- commons工具包 --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> </dependencies>
wtrover-security-app,其中${wtrover.security.version}是声明在主模块wtrover-security pom中的变量。
<dependencies> <dependency> <groupId>com.wtrover.security</groupId> <artifactId>wtrover-security-core</artifactId> <version>${wtrover.security.version}</version> </dependency> </dependencies>
wtrover-security-browser
<dependencies> <dependency> <groupId>com.wtrover.security</groupId> <artifactId>wtrover-security-core</artifactId> <version>${wtrover.security.version}</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> </dependency> </dependencies>
wtrover-security-demo,先从浏览器安全开始讲,所以先去引用wtrover-security-browser这个项目。
<dependencies> <dependency> <groupId>com.wtrover.security</groupId> <artifactId>wtrover-security-browser</artifactId> <version>${wtrover.security.version}</version> </dependency> </dependencies>
3.写个Hello World
DemoApplication
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; @SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/hello") public String hello() { return "hello spring security"; } }
在配置文件配置jdbc连接,session,security
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/spring?characterEncoding=utf8&useSSL=false username: root password: root
server:
port: 8065
spring:
session:
store-type: none
security:
basic:
enabled: false
运行即可。浏览器输入http://localhost:8065/hello,页面显示"hello spring security"