从零学springSecurity-项目结构

283人浏览 / 0人评论 | 作者:kite

作者:kite

链接:http://kite.vsftp.cn:8089/article/2.index.html

声明:请尊重原作者的劳动,如需转载请注明出处


项目结构

  kite-security  父工程[聚合管理所有子工程][pom]
    kite-base  基础通用模块[依赖父工程][jar]
    kite-core  核心模块[依赖base子工程&父工程][安全管理,身份认证,验证码/手机认证,用户授权][jar]
    kite-web  WEB业务应用[依赖core子工程][jar]

指定父工程打包类型
在父工程pom文件中添加pom类型为打包方式

  <packaging>pom</packaging>

添加父工程依赖管理

  <!-- Spring Boot 父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/>
    </parent>

    <!-- 依赖版本号 -->
    <properties>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <druid.version>1.1.12</druid.version>
        <oauth2-autoconfigure.version>2.1.3.RELEASE</oauth2-autoconfigure.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <fastjson.version>1.2.8</fastjson.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-collections.version>3.2.2</commons-collections.version>
        <commons-io.version>2.6</commons-io.version>
        <!-- 定义版本号, 子模块直接引用-->
        <kite-security.version>1.0-SNAPSHOT</kite-security.version>
    </properties>

    <!-- 集中式管理依赖版本号,并没有真实依赖 -->
    <dependencyManagement>
        <dependencies>
            <!--mybatis-plus启动器-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!--spring-security-oauth2、spring-security-jwt等-->
            <dependency>
                <groupId>org.springframework.security.oauth.boot</groupId>
                <artifactId>spring-security-oauth2-autoconfigure</artifactId>
                <version>${oauth2-autoconfigure.version}</version>
            </dependency>
            <!-- kaptcha 用于图形验证码 -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- 工具类依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>${commons-lang.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons-collections.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--聚合管理-->
    <modules>
        <module>kite-base</module>
        <module>kite-core</module>
        <module>kite-web</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

向base子工程pom文件内添加真实通用依赖

  <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</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-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

添加日志输出配置文件
在[kite-base/src/main/resources/]路径下新建[logback.xml]文件并添加以下内容

 
xml version="1.0" encoding="UTF-8"?>          conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />  conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />  conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />      name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>      name="stdout" class="ch.qos.logback.core.ConsoleAppender">  class="ch.qos.logback.classic.PatternLayout">  ${CONSOLE_LOG_PATTERN}     level="info">  ref="stdout" />       

向core子工程pom文件内添加真实通用依赖

<dependencies>
        <!--kite-security-base基础模块 -->
        <dependency>
            <groupId>org.kite</groupId>
            <artifactId>kite-base</artifactId>
            <version>${kite-security.version}</version>
        </dependency>

        <!-- spring security 启动器-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-security</artifactId>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
           <version>2.5</version>
       </dependency>

        <!-- application.yml 配置处理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--图形验证码-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</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.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.springframework.boot</groupId>-->
<!--                    <artifactId>spring-boot-starter-logging</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.9</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <!--采用redis来管理session-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

        <!--解决找不到 javax.annotation.meta.When.MAYBE-->
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>annotations</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

向web子工程pom文件内添加真实通用依赖

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

        <dependency>
            <groupId>org.kite</groupId>
            <artifactId>kite-core</artifactId>
            <version>${kite-security.version}</version>
        </dependency>

        <!--mybatis-plus启动器
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        -->
        <!--druid连接池
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        -->


        <!--bootstrap-table-->
        <dependency>
            <groupId>org.webjars.npm</groupId>
            <artifactId>bootstrap-table</artifactId>
            <version>1.14.1</version>
        </dependency>

        <!--jquery-treeGrid-->
        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>jquery-treegrid</artifactId>
            <version>0.3.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>jquery</artifactId>
                    <groupId>org.webjars.bower</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--layer-->
        <dependency>
            <groupId>org.webjars.bowergithub.sentsin</groupId>
            <artifactId>layer</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!-- thymeleaf 模块启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--对Thymeleaf添加Spring Security标签支持
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        -->
        <!-- springboot 单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--热部署 ctrl+f9-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

添加启动类
在[kite-web/src/main/java/org/kite/]路径下新建java文件[KiteApplication.java]并在类名上添加注解[@SpringBootApplication]来标注这是一个springboot程序,然后在类内添加一个main函数,在函数内调用[org.springframework.boot.SpringApplication]包内的[run]函数,并传入当前启动类来使类加载器加载当前类,代码如下:

  ApplicationContext kite = SpringApplication.run(KiteApplication.class);

添加项目配置文件[yml]
在[kite-web/src/main/resources/]路径下添加配置文件[application.yml]并添加以下配置内容:

  server:
  port: 8088
  servlet:
    session:
      timeout: 30m
      cookie:
        name: JSESSIONID #指定浏览器cookie中关于sessionID的保存的名称
kite:
  security:
    authentication:
      loginPage: /login/page
      loginProcessingUrl: /login/form
      usernameParameter: name
      passwordParameter: pwd
      staticPaths:
      - /dist/**
      - /modules/**
      - /plugins/**
      loginType: REDIRECT #认证后响应的类型 JSON/REDIRECT
      # security配置
      imageCodeUrl: /code/image # 获取图像验证码地址
      tokenValiditySeconds: 604800 # 登录状态保持一个礼拜(7天)604800

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  thymeleaf:
    cache: false #关闭缓存
  #配置数据源
  datasource:
    username: 用户名
    password: 密码
    url: jdbc:mysql://IP:3306/study_security
    driver-class-name: com.mysql.cj.jdbc.Driver
  session:
    store-type: none
  redis:
    port: 6379

---
spring:
  profiles:
    active: pro
  thymeleaf:
    cache: false #关闭缓存
  #配置数据源
  datasource:
    username: root
    password: 密码
    url: jdbc:mysql://localhost:3306/study_security
    driver-class-name: com.mysql.cj.jdbc.Driver
  session:
    store-type: none # session存储方式使用redis
  redis:
    port: 6379

原创:笔记


点赞(0) 打赏

全部评论

还没有评论!