前言:springboot是1.5,这个系列的文章是学习的慕课网上的Spring Security视频https://coding.imooc.com/class/134.html但是前2章是关于工作中的一些小功能。可以直接从第三章看起。
![](https://img.haomeiwen.com/i13135046/a4fd1bed972f997f.png)
pom依赖
fuiou-security
<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.fuiou.security</groupId>
<artifactId>fuiou-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<fuiou.secutity.version>1.0.0-SNAPSHOT</fuiou.secutity.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>../fuiou-security-app</module>
<module>../fuiou-security-browser</module>
<module>../fuiou-security-core</module>
<module>../fuiou-security-demo</module>
</modules>
</project>
fuiou-security-core
<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>fuiou-security-core</artifactId>
<parent>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../fuiou-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>
<!-- 工具类 -->
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
fuiou-security-app
<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>fuiou-security-app</artifactId>
<parent>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../fuiou-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security-core</artifactId>
<version>${fuiou.secutity.version}</version>
</dependency>
</dependencies>
</project>
fuiou-security-browser
<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>fuiou-security-browser</artifactId>
<parent>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../fuiou-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security-core</artifactId>
<version>${fuiou.secutity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
</dependencies>
</project>
fuiou-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>fuiou-security-demo</artifactId>
<parent>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../fuiou-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.fuiou.security</groupId>
<artifactId>fuiou-security-browser</artifactId>
<version>${fuiou.secutity.version}</version>
</dependency>
</dependencies>
</project>
hello world
fuiou-security-demo
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/file?characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=111111
spring.session.store-type=none
security.basic.enabled=false
编写测试用例
package com.fuiou.web.controller;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc=MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenQuerySuccess() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user")
.param("username", "jojo")
.param("size", "10")
.param("page", "1")
.param("sort", "age,desc")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3));
}
}
@RestController
public class UserController {
@RequestMapping(value = "/user",method = RequestMethod.GET)
public List<User> query(@RequestParam(required = false) String username,@PageableDefault(page = 1,size = 10,sort = "username,asc") Pageable pageable){
System.out.println(pageable.getPageSize()+"---"+pageable.getPageNumber()+"---"+pageable.getSort());
List<User> users=new ArrayList<User>(3);
users.add(new User());
users.add(new User());
users.add(new User());
return users;
}
}
注意:使用Pageable 对象来接收关于分页的参数
网友评论