本次环境搭建基于Maven项目,采用五个Maven工程
gcxzflgl-security------------------------父工程
gcxzflgl-security-core------------------核心工程
gcxzflgl-security-app-------------------App工程
gcxzflgl-security-browser--------------浏览器工程
gcxzflgl-security-demo-----------------测试demo
他们之间的关系为 核心工程和依赖于父工程,App工程和浏览器工程依赖于核心工程,测试工程依赖于App和浏览器工程
首先创建五个工程,父工程gcxzflgl-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.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<gcxzflgl.security.version>1.0.0-SNAPSHOT</gcxzflgl.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>../gcxzflgl-security-app</module>
<module>../gcxzflgl-security-browser</module>
<module>../gcxzflgl-security-core</module>
<module>../gcxzflgl-security-demo</module>
</modules>
</project>
注意modules在添加的时候
先是打开pom.xml,跳转到overview
注意这个update POM parent section in selected projects必须勾上,否则后面出现一系列错误,此时其他四个项目都会报错,只需要选中这四个项目,右键maven --> update project就可以了。
添加核心工程依赖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>gcxzflgl-security-core</artifactId>
<parent>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../gcxzflgl-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.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
添加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>gcxzflgl-security-app</artifactId>
<parent>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../gcxzflgl-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security-core</artifactId>
<version>${gcxzflgl.security.version}</version>
</dependency>
</dependencies>
</project>
添加浏览器工程:
<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>gcxzflgl-security-browser</artifactId>
<parent>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../gcxzflgl-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security-core</artifactId>
<version>${gcxzflgl.security.version}</version>
</dependency>
<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>
</dependency>
</dependencies>
</project>
添加测试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>gcxzflgl-security-demo</artifactId>
<parent>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../gcxzflgl-security</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.gcxzflgl.security</groupId>
<artifactId>gcxzflgl-security-browser</artifactId>
<version>${gcxzflgl.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>
</dependencies>
<build>
<plugins>
<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>gcxzflgl</finalName>
</build>
</project>
此时在demo工程里添加主程序运行,会先提示没有数据源:
package com.gcxzflgl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping("/user/home")
public String hello() {
return "hello springsecurity!!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我们在src/main/resources/ 添加application.properties,配置下数据源:用户名 密码 数据库名换成自己相应的
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/db_springsecurity?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
这时再次运行主程序报错告诉我们缺少 spring-session-store
我们在application.properties添加一行,先暂时关闭
spring.session.store-type = none
这时再次运行发现不报错了,我们访问这个Controller localhost:8080/user/home
springsecurity会提示一个安全框,让我们输入用户和密码在进行访问
我们先关掉它,在application.properties添加一行
security.basic.enabled = false
再次重启项目,就可以直接访问
如果好奇springsecurity输入框输入什么用户名默认是user,密码在控制台上,输入后就可以正常访问了。