1. 程式人生 > >idea使用maven搭建多模組專案+整合spring security安全框架

idea使用maven搭建多模組專案+整合spring security安全框架

我的專案結構:

1.使用idea的maven的quickstart構建父專案

接下來輸入:groupId填com.imooc.security        ArtifactId填imooc-security-parent

刪除它自建的src目錄,並將pom.xml改成如下:(將packing寫成pom)

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>imooc-security-parent</artifactId>
        <groupId>com.imooc.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>imooc-security-demo</artifactId>
    <packaging>jar</packaging>

    <name>imooc-security-demo Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

   <dependencies>
       <dependency>
           <groupId>com.imooc.security</groupId>
           <artifactId>imooc-security-browser</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
   </dependencies>
</project>

然後右擊專案,--》new Mudel建立imooc-security-core模組

 修改其pom.xml如下:(一下packing裡都是jar的形式)

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>imooc-security-parent</artifactId>
        <groupId>com.imooc.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>imooc-security-core</artifactId>

    <name>imooc-security-core</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
<packaging>jar</packaging>


    <!--核心程式碼,引入的第三方專案都在這裡-->

    <dependencies>
        <!--這裡不用寫版本號version,因為父專案的springio,cloud和dependencemanager會統一版本,並且保證相容性-->
        <!--它會引入大量的jar包,主要引入springsecurity   jar包和springsecurity oauth2-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <!-- 儲存token,繫結qq,微信等-->
        <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>
        <!--  通過spring-social來整合第三方服務,如qq-->
        <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-beautils</groupId>
            <artifactId>commons-beautils</artifactId>
        </dependency>

    </dependencies>

</project>

app,browser模組都是按core方式建立即可,它們都父專案都是security-parent,並依賴於core模組,demo模組建立maven時選擇webapp建立:(別的都一樣)

app模組的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>imooc-security-parent</artifactId>
        <groupId>com.imooc.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>imooc-security-app</artifactId>

    <name>imooc-security-app</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
<packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.imooc.security</groupId>
            <artifactId>imooc-security-core</artifactId>

        </dependency>
    </dependencies>
</project>

 browser模組的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>imooc-security-parent</artifactId>
        <groupId>com.imooc.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>imooc-security-browser</artifactId>

    <name>imooc-security-browser</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
<packaging>jar</packaging>
 <dependencies>
     <!-- 叢集環境下的session管理-->
     <dependency>
         <groupId>org.springframework.session</groupId>
         <artifactId>spring-session</artifactId>
     </dependency>
     <dependency>
         <groupId>com.imooc.security</groupId>
         <artifactId>imooc-security-core</artifactId>
     </dependency>
 </dependencies>
</project>

demo模組的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>imooc-security-parent</artifactId>
        <groupId>com.imooc.security</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>imooc-security-demo</artifactId>
    <packaging>jar</packaging>

    <name>imooc-security-demo Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

   <dependencies>
       <dependency>
           <groupId>com.imooc.security</groupId>
           <artifactId>imooc-security-browser</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
   </dependencies>
</project>

demo的測試程式碼如下:

package imooc.security;

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 Demo {
 public static void main( String[] args )
 {
  SpringApplication.run(Demo.class,args);
 }
 @GetMapping("/hello")
 public String hello(){
  return "你好,世界";
 }

}

訪問localhost:8080/hello  即可看到執行成功,環境搭建完成。

後續可以打包專案成.jar格式通過java命令啟動(進入jar包所在的目錄,此時可以看到springboot專案啟動了==idea使用main函式啟動一樣,它也和啟動tomcat,執行web一樣,其實springboot的專案不需要tomcat伺服器,因為它有內嵌的tomcat外掛,所以只需要打包成jar檔案,丟到伺服器,然後進入可執行的jar所在的目錄,用命令 java -jar demo.jar  執行jar檔案,我們的web專案就像tomcat釋出專案一樣跑起來了)     java -jar demo.jar