1 创建项目
目录结构如下

父项目:wuhongyu-security
pom.xml如下:
<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.wuhongyu</groupId>
<artifactId>wuhongyu-security</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<!--指定该项目的版本,以便升级-->
<wuhongyu.security.version>1.0</wuhongyu.security.version>
</properties>
<dependencyManagement>
<!--
spring io和spring cloud 项目的引入
可以帮助我们管理jar包的版本
-->
<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>
<!--
maven插件: 可以帮助我们确定jdk的版本 并且使用UTF-8编码
-->
<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>../wuhongyu-security-app</module>
<module>../wuhongyu-security-browser</module>
<module>../wuhongyu-security-core</module>
<module>../wuhongyu-security-demo</module>
</modules>
</project>
用于处理手机app请求的安全项目:wuhongyu-security-app
pom.xml 如下:
<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>
<artifactId>wuhongyu-security-app</artifactId>
<parent>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security</artifactId>
<version>1.0</version>
<relativePath>../wuhongyu-security</relativePath>
</parent>
<dependencies>
<!--引入core 核心依赖-->
<dependency>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security-core</artifactId>
<version>${wuhongyu.security.version}</version>
</dependency>
</dependencies>
</project>
用于处理浏览器请求的安全项目:wuhongyu-security-browser
pom.xml如下
<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>
<artifactId>wuhongyu-security-browser</artifactId>
<parent>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security</artifactId>
<version>1.0</version>
<relativePath>../wuhongyu-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security-core</artifactId>
<version>${wuhongyu.security.version}</version>
</dependency>
<!--
因为暂时不需要session管理 先注释起来 之后再用
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.2</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
</project>
封装spring security的核心项目:wuhongyu-security-core
pom.xml如下:
<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>
<artifactId>wuhongyu-security-core</artifactId>
<parent>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security</artifactId>
<version>1.0</version>
<relativePath>../wuhongyu-security</relativePath>
</parent>
<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>
<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>
<!--
阿帕奇的 common工具包
-->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
用于效果演示的项目:wuhongyu-security-demo
<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>
<artifactId>wuhongyu-security-demo</artifactId>
<parent>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security</artifactId>
<version>1.0</version>
<relativePath>../wuhongyu-security</relativePath>
</parent>
<dependencies>
<!--引入spring security封装浏览器请求的依赖-->
<dependency>
<groupId>com.wuhongyu</groupId>
<artifactId>wuhongyu-security-browser</artifactId>
<version>${wuhongyu.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
</dependency>
<!--
引入 thymeleaf 模板引擎 和spring security对 thymeleaf的支持
-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--
maven插件:
能够在打包的时候将jar包全都导入进去
可以实现直接在java环境下运行
-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>demo</finalName>
</build>
</project>
2 编写demo里的测试代码
学到了一句话: 功能未写,测试先行
测试代码:
package com.wuhongyu.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRestFul {
@Autowired
private WebApplicationContext wac;
/**
* 用户模拟mvc模式的类
* */
private MockMvc mockMvc;
/**
* 初始化这个模拟器
*/
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenQuerySuccess() throws Exception {
//发送一个/user请求
mockMvc.perform(get("/user").contentType(MediaType.APPLICATION_JSON_UTF8))
//期望返回的状态码是200
.andExpect(status().isOk())
//期望返回的json的大小是3
.andExpect(jsonPath("$.length()").value(3));
}
}
然后编写主程序中的User类
package com.wuhongyu.entity;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 4023183014825837154L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
编写UserController进行测试
package com.wuhongyu.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuhongyu.entity.User;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping
public List<User> query(){
List<User> list = new ArrayList<>();
list.add(new User());
list.add(new User());
list.add(new User());
return list;
}
}
暂时先把spring security关闭
application.properties配置如下
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.thymeleaf.cache=false
security.basic.enabled=false
server.port= 8086
运行测试用例 显示通过

网友评论