开发环境:
安装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-SNAPSHOTwtrover.security.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platformgroupId> <artifactId>platform-bomartifactId> <version>Brussels-SR4version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>Dalston.SR2version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>2.3.2version> <configuration> <source>1.8source> <target>1.8target> <encoding>UTF-8encoding> configuration> plugin> plugins> build> <modules> <module>../wtrover-security-appmodule> <module>../wtrover-security-browsermodule> <module>../wtrover-security-coremodule> <module>../wtrover-security-demomodule> modules>
wtrover-security-core
<dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-oauth2artifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-jdbcartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> dependency> <dependency> <groupId>org.springframework.socialgroupId> <artifactId>spring-social-configartifactId> dependency> <dependency> <groupId>org.springframework.socialgroupId> <artifactId>spring-social-coreartifactId> dependency> <dependency> <groupId>org.springframework.socialgroupId> <artifactId>spring-social-securityartifactId> dependency> <dependency> <groupId>org.springframework.socialgroupId> <artifactId>spring-social-webartifactId> dependency> <dependency> <groupId>commons-langgroupId> <artifactId>commons-langartifactId> dependency> <dependency> <groupId>commons-collectionsgroupId> <artifactId>commons-collectionsartifactId> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> dependency> <dependency> <groupId>commons-beanutilsgroupId> <artifactId>commons-beanutilsartifactId> dependency> dependencies>
wtrover-security-app,其中${wtrover.security.version}是声明在主模块wtrover-security pom中的变量。
<dependencies> <dependency> <groupId>com.wtrover.securitygroupId> <artifactId>wtrover-security-coreartifactId> <version>${wtrover.security.version}version> dependency> dependencies>
wtrover-security-browser
<dependencies> <dependency> <groupId>com.wtrover.securitygroupId> <artifactId>wtrover-security-coreartifactId> <version>${wtrover.security.version}version> dependency> <dependency> <groupId>org.springframework.sessiongroupId> <artifactId>spring-sessionartifactId> dependency> dependencies>
wtrover-security-demo,先从浏览器安全开始讲,所以先去引用wtrover-security-browser这个项目。
<dependencies> <dependency> <groupId>com.wtrover.securitygroupId> <artifactId>wtrover-security-browserartifactId> <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"