Spring Security項目構建(一)

項目構建

image.png

依賴關係
image.pngcss

代碼結構

  • security:主模塊
  • security-core:核心業務邏輯
  • security-browser:瀏覽器安全特定代碼
  • security-app:app相關特定代碼
  • security-demo:樣例程序

包引入

主模塊java

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.guosh.security</groupId>
    <artifactId>guosh-security</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!--聲明變量-->
    <properties>
        <guosh.security.version>1.0-SNAPSHOT</guosh.security.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR16</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</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> <!-- 須要生成的目標class文件的編譯版本 -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>guosh-security-core</module>
        <module>guosh-security-browser</module>
        <module>guosh-security-demo</module>
        <module>guosh-security-app</module>
    </modules>
</project>

core模塊mysql

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

    <artifactId>guosh-security-core</artifactId>
    <dependencies>
        <!--oauth2認證-->
        <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>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </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>
    </dependencies>
</project>

browser模塊web

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

    <artifactId>guosh-security-browser</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.guosh.security</groupId>
            <artifactId>guosh-security-core</artifactId>
            <version>${guosh.security.version}</version>
        </dependency>
        <!--session集羣-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>
    </dependencies>

</project>

app 模塊redis

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

    <artifactId>guosh-security-app</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.guosh.security</groupId>
            <artifactId>guosh-security-core</artifactId>
            <version>${guosh.security.version}</version>
        </dependency>
    </dependencies>
</project>

demo 模塊spring

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

    <artifactId>guosh-security-demo</artifactId>


    <dependencies>
        <dependency>
            <groupId>com.guosh.security</groupId>
            <artifactId>guosh-security-browser</artifactId>
            <version>${guosh.security.version}</version>
        </dependency>
        <!--Spring boot測試框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    </dependencies>


    <build>
        <!--demo是web程序須要運行打完包的名字-->
        <finalName>guoshsecurity</finalName>
        <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>
    </build>
</project>

Hello程序

如今須要把項目啓動起來因此作了一個Demosql

1.在Demo下面新建yml文件並鏈接數據庫數據庫

server:
  port: 8080
  context-path: /guoshsecurity

spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/guosecurity?useUnicode=true&characterEncoding=utf8&useSSL=true
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
      #鏈接池初始化大小最小最大
      initial-size: 2
      min-idle: 2
      max-active: 2
      # 配置獲取鏈接等待超時的時間
      maxWait: 60000
      # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一個鏈接在池中最小生存的時間,單位是毫秒
      minEvictableIdleTimeMillis: 60000
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打開PSCache,而且指定每一個鏈接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
      filters: stat,wall,log4j
      #屬性來打開mergeSql功能;慢SQL記錄
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      #驗證鏈接是否可用,使用的SQL語句
      validation-query: SELECT 1
      #合併多個DruidDataSource的監控數據
      useGlobalDataSourceStat: true
      #監控配置
      web-stat-filter:
        url-pattern: /*
        exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico
      stat-view-servlet:
        url-pattern: /druid/*
        login-username: guoadmin
        login-password: guoadmin
        #是否能按重製按鍵
        reset-enable: true

  #關閉spring session
  session:
    store-type: none

#先關閉security默認配置
security:
  basic:
    enabled: false

2.新建Spring boot的啓動文件,添加一個/hello測試apache

image.png

3.執行main函數查看
image.png瀏覽器

4.打包測試

使用maven打包命令生成jar包以後能夠使用

java  -jar  ./guoshsecurity.jar 啓動
相關文章
相關標籤/搜索