[SrpingSecurity]之一:框架搭建

版权声明:无需授权即可转载,甚至无需保留以上版权声明... ... https://blog.csdn.net/qq_28296925/article/details/82021092

前言:项目使用gradle构建,利用springboot+springSecurity 搭建起项目

1、步骤:

1、新建一个gradle项目
2、重写build.gradle文件,和任务功能(创建目录),导入项目的依赖(spring-boot,spring security)
3、新建一个springboot启动类
4、创建application.properties 配置文件(springboot项目的不可或缺的配置文件)
5、启动项目

2、 演示


一、新建项目,并重写build.gradle依赖,导入依赖完成。

项目结构构建完成
图中操作,以上步骤:1,2基本已完成。除了2中的执行初始化目录操作。

二、创建项目初始化目录,创建启动类,配置文件,并启动,登录。

执行build.gradle中自定义的任务:makeJavaDir,makeResourcesDir;进行初始化项目目录结构。

项目启动登录构建完成

图中操作,以上步骤:2,3,4,5全部完成。

备注:在本步演示过程中,创建application.properties,创建位置错误(我在项目的根目录下创建的)。应该将application.properties放到resource包下。

最终项目的结构图如下:
项目结构图

注意:

  1. 启动springboot启动类时,springsecurity自动创建了随机密码,在控制台输出出来。
  2. 启动后,localhost:8080/默认定位到springsecurity的登录界面。密码就是控制台输出的密码。用户名默认是user。出来下面的界面,便意味着登录成功了。

    这里写图片描述


清单一:build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.3.RELEASE'
        projectJdk = '1.8'
        projectGroup = 'com.security'
        projectVersion = '0.0.1'
        projectName = 'securitydemo'
        // 我自己的私服  ,你们可以注释掉
        mavenUrl =  "http://192.168.0.14/nexus/repository/maven-public/";
    }
    repositories {
        mavenLocal()
        maven { url = mavenUrl }
        mavenCentral();

        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'

group = projectGroup
version = projectVersion
sourceCompatibility = projectJdk
repositories {
    mavenLocal()
    maven { url = mavenUrl }
    maven { url "http://repo.spring.io/milestone" }
}
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile("org.apache.tomcat.embed:tomcat-embed-jasper")
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    // 导入spring security的依赖
    compile('org.springframework.boot:spring-boot-starter-security')
    compile group: 'org.thymeleaf.extras',  
            name: 'thymeleaf-extras-springsecurity4',
            version: '3.0.2.RELEASE'


    compile('com.alibaba:druid-spring-boot-starter:1.1.10')
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    compile("mysql:mysql-connector-java:8.0.11")
    compile("com.github.pagehelper:pagehelper-spring-boot-starter:1.2.5")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("tk.mybatis:mapper-spring-boot-starter:2.0.3")
    compile("org.mybatis.generator:mybatis-generator-core:1.3.7")
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile 'com.github.joschi.jackson:jackson-datatype-threetenbp:2.6.4'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
    compile ('org.springframework.social:spring-social-web:1.1.0.RELEASE')
}



// 以下内容 ,是创建的任务task,在gradle中 other中有这些任务。直接点击相应的任务进行创建目录
def creatDir={
    path ->
        File dir =new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
}

// --> 创建源码目录
//  src:main[java,resources] ;test [java,resources]
task makeJavaDir() {
    def paths = ['src/main/java', 'src/main/resources', 'src/test/java', 'src/test/resources'];
// 在创建任务之前 先遍历 路径
    doFirst {
        paths.forEach(creatDir);
    }
}


//  --> 创建resources目录下的内容
//   src/main/resources:[templates,static,mappers,views] 项目目录结构
task makeResourcesDir(){
    dependsOn 'makeJavaDir';
    def paths=['src/main/resources/templates',
    'src/main/resources/static','src/main/resources/mappers','src/main/resources/views'];
    doLast {
        paths.forEach(creatDir)
    }
}


// --> 创建 web目录
// web 项目目录结构 包含了java项目目录结构
task makeWebDir(){
    dependsOn 'makeJavaDir';
    def paths=['src/main/webapp','src/test/webapp'];
    doLast {
        paths.forEach(creatDir)
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28296925/article/details/82021092
今日推荐