2.初始工程搭建

startshineye

前言

搭建可运行的spring-security多模块工程项目

  1. maven多模块工程目录
  2. 模块搭建
  3. 运行测试

内容

1.maven多模块工程目录

工程目录结构:
spring-security
├── spring-security-app-- app模块代码
├── spring-security-core-- 核心业务逻辑
├── spring-security-demo-- 样例模块逻辑
└── spring-security-web-- web模块代码

2.模块搭建

2.1 创建工程

  1. 创建父工程
    使用idea创建spring-security父工程
    选择maven不勾选archetype
    1.png
    输入maven坐标
    2.png
    本地存储位置
    3.png
  2. 创建其他字工程
    右键父工程:module选择maven工程
    输入子工程的maven坐标
    4.png
    选择本地存储路径在父工程存储路径下

2.2引入依赖

  1. 父工程依赖
<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>
  1. spring-security-core依赖
<!--引入依赖:不用写version;dependencyManagement中的io.spring.platform;org.springframework.cloud做了统一管理\--> 
<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>  
  <!--processor-->  
  <dependency>  
 <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-configuration-processor</artifactId>  
 </dependency>  
</dependencies>
  1. spring-security-app依赖
<dependencies>  
 <dependency> <groupId>com.yxm.security</groupId>  
 <artifactId>spring-security-core</artifactId>  
 <version>${project.version}</version>  
 </dependency></dependencies>
  1. spring-security-demo依赖
<dependencies>  
 <dependency> <artifactId>spring-security-web</artifactId>  
 <groupId>com.yxm.security</groupId>  
 <version>${project.version}</version>  
 </dependency></dependencies>
  1. spring-security-web依赖
<!--core中已经引入相关依赖了,我们直接引入core-->  
<dependencies>  
 <dependency> <groupId>com.yxm.security</groupId>  
 <artifactId>spring-security-core</artifactId>  
 <version>${project.version}</version>  
 </dependency>  
 <dependency> <groupId>org.springframework.session</groupId>  
 <artifactId>spring-session</artifactId>  
 </dependency></dependencies>

3.运行项目

在spring-security-demo中
新建包:com.yxm.security
新建类:DemoApplication
添加注解:@SpringBootApplication、@RestController
添加main方法和测试方法:如下:

@SpringBootApplication  
@RestController  
public class DemoApplication {    
  public static void main(String\[\] args) {  
        SpringApplication.run(DemoApplication.class, args);  
  }  
  @RequestMapping("/hello")  
    public String hello(){  
        return "hello spring security";  
  }  
}

运行报错:Cannot determine embedded database driver class for database type NONE

spring:  
  datasource:  
    driver-class-name: com.mysql.jdbc.Driver  
  url: jdbc:mysql://127.0.0.1:3306/oa?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai  
    data-username: root  
    data-password: root

运行报错:
关闭spring.session

spring:  
 #关闭spring session不然会报错  
  session:  
   store-type: none

然后正常启用demo项目:访问:http://127.0.0.1:8088/hello
出现如下:
8.png

springBoot下spring-security默认配置下 访问接口需要要求验证:我们关闭认证

security:  
  basic:  
    enabled: false

访问:http://127.0.0.1:8088/hello
输出:hello spring security

我们此时执行父工程maven命令:mvn clean package
然后我们发现工程大小为3kb不可执行

为了让demo可以打成运行的jar包,我们在demo的pom.xml文件里面添加配置:

<build>  
     <finalName>demo</finalName>  
     <plugins> 
     <plugin> 
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-maven-plugin</artifactId>  
        <version>1.3.3.RELEASE</version>  
        <executions> 
         <execution> 
          <goals>  
           <!--可以把项目按照SpringBoot特有的方式来进行打包为fileNmae的包-->  
         <goal>repackage</goal>  
         </goals> 
        </execution> 
       </executions> 
     </plugin> 
     </plugins>
 </build>

7.png

以上工程搭建完毕。

阅读 899

我在规定的时间内,做到了我计划的事情;我自己也变得自信了,对于外界的人跟困难也更加从容了,我已经很强...

54 声望
13 粉丝
0 条评论

我在规定的时间内,做到了我计划的事情;我自己也变得自信了,对于外界的人跟困难也更加从容了,我已经很强...

54 声望
13 粉丝
文章目录
宣传栏