Added| 新增migration設定
This commit is contained in:
51
build.gradle
51
build.gradle
@@ -1,20 +1,30 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
classpath 'org.jooq:jooq-codegen:3.6.1'
|
||||||
|
classpath 'org.flywaydb:flyway-core'
|
||||||
|
classpath "org.flywaydb:flyway-mysql:9.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'org.springframework.boot' version '2.7.5'
|
id 'org.springframework.boot' version '2.7.5'
|
||||||
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
|
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
|
||||||
|
id "org.flywaydb.flyway" version "8.5.13"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
allprojects{
|
allprojects{
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'org.springframework.boot'
|
apply plugin: 'org.springframework.boot'
|
||||||
apply plugin: 'io.spring.dependency-management'
|
apply plugin: 'io.spring.dependency-management'
|
||||||
group = 'org.fycd'
|
group = 'org.fycd'
|
||||||
version = '0.0.1-SNAPSHOT'
|
version = '0.0.1'
|
||||||
|
|
||||||
sourceCompatibility = 17
|
sourceCompatibility = 11
|
||||||
targetCompatibility = 17
|
targetCompatibility = 11
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
@@ -28,8 +38,6 @@ allprojects{
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-jooq'
|
implementation 'org.springframework.boot:spring-boot-starter-jooq'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
implementation 'org.flywaydb:flyway-core'
|
|
||||||
implementation 'org.flywaydb:flyway-mysql'
|
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||||
runtimeOnly 'com.h2database:h2'
|
runtimeOnly 'com.h2database:h2'
|
||||||
@@ -48,3 +56,32 @@ dependencies {
|
|||||||
implementation project(':security-core')
|
implementation project(':security-core')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
task migrateDB(type: org.flywaydb.gradle.task.FlywayMigrateTask) {
|
||||||
|
driver = 'org.mariadb.jdbc.Driver'
|
||||||
|
url = 'jdbc:mariadb://localhost:3306/fycd?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Taipei'
|
||||||
|
user = 'root'
|
||||||
|
password = 'roottoor'
|
||||||
|
locations = ['filesystem:migrations']
|
||||||
|
target = 'latest'
|
||||||
|
baselineVersion = '0.0.1.0.1'
|
||||||
|
baselineOnMigrate = true
|
||||||
|
validateOnMigrate = true
|
||||||
|
outOfOrder = true
|
||||||
|
placeholderReplacement = false
|
||||||
|
}
|
||||||
|
|
||||||
|
task cleanDB(type: org.flywaydb.gradle.task.FlywayCleanTask) {
|
||||||
|
cleanDisabled = false
|
||||||
|
driver = 'org.mariadb.jdbc.Driver'
|
||||||
|
url = 'jdbc:mariadb://localhost:3306/fycd?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Taipei'
|
||||||
|
user = 'root'
|
||||||
|
password = 'roottoor'
|
||||||
|
locations = ['filesystem:migrations']
|
||||||
|
target = 'latest'
|
||||||
|
baselineVersion = '0.0.1.0.1'
|
||||||
|
baselineOnMigrate = true
|
||||||
|
validateOnMigrate = true
|
||||||
|
outOfOrder = true
|
||||||
|
placeholderReplacement = false
|
||||||
|
}
|
||||||
|
|||||||
7
migrations/V0.0.1.0.1__demo.sql
Normal file
7
migrations/V0.0.1.0.1__demo.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE demo_migration (
|
||||||
|
value VARCHAR(25) NOT NULL,
|
||||||
|
PRIMARY KEY(value)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO demo_migration (value) VALUES ('AC demo');
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.fycd.bigdata.advice;
|
||||||
|
|
||||||
|
import org.fycd.bigdata.exception.TokenRefreshException;
|
||||||
|
import org.fycd.bigdata.pojo.ErrorMessage;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class AuthControllerAdvice {
|
||||||
|
@ExceptionHandler(value = TokenRefreshException.class)
|
||||||
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
|
public ErrorMessage handleTokenRefreshException(TokenRefreshException ex, WebRequest request) {
|
||||||
|
return new ErrorMessage(
|
||||||
|
HttpStatus.FORBIDDEN.value(),
|
||||||
|
LocalDateTime.now(),
|
||||||
|
ex.getMessage(),
|
||||||
|
request.getDescription(false));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.fycd.bigdata.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ErrorMessage {
|
||||||
|
private int statusCode;
|
||||||
|
private LocalDateTime timestamp;
|
||||||
|
private String message;
|
||||||
|
private String description;
|
||||||
|
public ErrorMessage (int statusCode, LocalDateTime timestamp, String message, String description) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
this.message = message;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ import java.util.UUID;
|
|||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RefreshTokenService {
|
public class RefreshTokenService {
|
||||||
@Value("${app.security.jwtExpiration")
|
@Value("${app.security.jwtExpiration}")
|
||||||
private Long refreshTokenDuration;
|
private Long refreshTokenDuration;
|
||||||
|
|
||||||
private final UserDaoSub userDao;
|
private final UserDaoSub userDao;
|
||||||
|
|||||||
@@ -3,11 +3,9 @@ package org.fycd.bigdata.utils;
|
|||||||
import io.jsonwebtoken.*;
|
import io.jsonwebtoken.*;
|
||||||
import io.jsonwebtoken.security.Keys;
|
import io.jsonwebtoken.security.Keys;
|
||||||
import io.jsonwebtoken.security.SignatureException;
|
import io.jsonwebtoken.security.SignatureException;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.fycd.bigdata.service.UserDetailsImpl;
|
import org.fycd.bigdata.service.UserDetailsImpl;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
@@ -19,8 +17,8 @@ public class JwtUtils {
|
|||||||
@Value("${app.security.jwtSecret}")
|
@Value("${app.security.jwtSecret}")
|
||||||
private String jwtSecret;
|
private String jwtSecret;
|
||||||
|
|
||||||
@Value("${app.security.jwtExpirationMs}")
|
@Value("${app.security.jwtExpiration}")
|
||||||
private int jwtExpirationMs;
|
private Long jwtExpiration;
|
||||||
|
|
||||||
|
|
||||||
public String generateJwtToken(UserDetailsImpl userPrincipal) {
|
public String generateJwtToken(UserDetailsImpl userPrincipal) {
|
||||||
@@ -29,13 +27,14 @@ public class JwtUtils {
|
|||||||
|
|
||||||
public String generateTokenFromUsername(String username) {
|
public String generateTokenFromUsername(String username) {
|
||||||
return Jwts.builder().setSubject(username).setIssuedAt(new Date())
|
return Jwts.builder().setSubject(username).setIssuedAt(new Date())
|
||||||
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)).signWith(SignatureAlgorithm.HS512, jwtSecret)
|
.setExpiration(new Date((new Date()).getTime() + jwtExpiration))
|
||||||
|
.signWith(Keys.hmacShaKeyFor(DatatypeConverter.parseBase64Binary(jwtSecret)))
|
||||||
.compact();
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean validateJwtToken(String authToken) {
|
public boolean validateJwtToken(String authToken) {
|
||||||
try {
|
try {
|
||||||
Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
|
getUserNameFromJwtToken(authToken);
|
||||||
return true;
|
return true;
|
||||||
} catch (SignatureException e) {
|
} catch (SignatureException e) {
|
||||||
log.error("Invalid JWT signature: {}", e.getMessage());
|
log.error("Invalid JWT signature: {}", e.getMessage());
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
app:
|
app:
|
||||||
security:
|
security:
|
||||||
jwtSecret: asdnkqldwk;l!@NLKASd12inkasldlxv.,xcvmkasldkqwe
|
jwtSecret: asdnkqldwk;l!@NLKASd12inkasldlxv.,xcvmkasldkqwe
|
||||||
jwtExpiration: 86400000
|
jwtExpiration: 86400000
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
driver-class-name: org.mariadb.jdbc.Driver
|
||||||
|
url: jdbc:mariadb://localhost:3306/fycd?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Taipei
|
||||||
|
username: root
|
||||||
|
password: roottoor
|
||||||
|
jooq:
|
||||||
|
sql-dialect: mariadb
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
app:
|
app:
|
||||||
security:
|
security:
|
||||||
jwtSecret: asdnkqldwk;l!@NLKASd12inkasldlxv.,xcvmkasldkqwe
|
jwtSecret: asdnkqldwk;l!@NLKASd12inkasldlxv.,xcvmkasldkqwe
|
||||||
jwtExpiration: 86400000
|
jwtExpiration: 86400000
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
driver-class-name: org.mariadb.jdbc.Driver
|
||||||
|
url: jdbc:mariadb://localhost:3306/fycd?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Taipei
|
||||||
|
username: root
|
||||||
|
password: roottoor
|
||||||
|
jooq:
|
||||||
|
sql-dialect: mariadb
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
active: dev
|
active: dev
|
||||||
|
|||||||
Reference in New Issue
Block a user