登录apiToken的task

一、创建项目,对项目进行初始化

git init
添加.gitignore
创建目录
在src/main/java目录下创建包,新建service,api,dto,respository,entity,exception,util这几个文件夹

二、启动springboot项目

添加springboot依赖
在build.gradle文件dependencies里添加

 compile("org.springframework.boot:spring-boot-starter-web")
 testCompile('org.springframework.boot:spring-boot-starter-test')

在buildscript的dependencies中添加插件

  classpath("org.springframework.boot:spring-boot-gradle-  plugin:1.5.7.RELEASE")           
   apply plugin: 'org.springframework.boot'

创建Application.java文件

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
}

三、用户注册

1写测试

添加junit5依赖
依赖:

testCompile('org.junit.jupiter:junit-jupiter-api:5.0.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.0.0')
testCompile('org.junit.jupiter:junit-jupiter-params:5.0.0')
testCompile('org.junit.platform:junit-platform-launcher:1.0.0')
testCompile('org.junit.platform:junit-platform-runner:1.0.0')
testCompile('com.github.sbrannen:spring-test-junit5:1.0.0')

插件:

apply plugin: 'org.junit.platform.gradle.plugin'
classpath('org.junit.platform:junit-platform-gradle-plugin:1.0.0')

其他:

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

编写baseControllerTest
编写UserCreateTest

2登录实现

使用mysql和flyway
添加jpa+mysql+flyway依赖

compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile 'mysql:mysql-connector-java:6.0.6'
compile ('org.flywaydb:flyway-core:4.2.0')

插件:

classpath("org.flywaydb:flyway-gradle-plugin:4.0.3")
apply plugin: 'org.flywaydb.flyway'

使用lombok

需要在idea下载lombok插件

 compileOnly ('org.projectlombok:lombok:1.16.18')

在entity中添加user实体,用户有用户名密码和id
编写userRespository
用户注册service
用户注册controller

四、用户登录

1.测试

创建登录实体loginRequestBody
编写loginTest

2.实现

添加jwt
依赖

compile('org.springframework.boot:spring-boot-starter-security')
compile ('io.jsonwebtoken:jjwt:0.7.0')

配置类
WebsecurityConfig
JWTAuthenticationFilter:判断用户发来的token是否有效

编写TokenAuthRepository以及实现JWTAuthRepositoryImpl:生成token和根据token提取payload

编写loginService:
登录:验证用户信息、获取payload,生成token置于响应头中
判断请求是否含有token
从请求中提取token
从请求的token中生成用户信息
isTokenInBlackList

编写loadUserByUsername方法

编写loginController

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容