commit 66c710c034a049327c9168b78d52f55d542465e5 Author: 松岳 陈 Date: Tue Jan 2 10:16:15 2024 +0800 Init Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d7d497 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# SSFY系统 README + +## 项目文件夹结构说明: +目前整体分为两大板块:**configuration**和**server** + +### Configuration +所有业务的配置,包括切面、拦截器、中间件、消息处理等配置全部放置于此处。 +任何带@Configuration注解的配置均放置于此 + +### server +server主要存放所有的业务逻辑以及Mapper接口。业务逻辑和Mapper需要按照业务进行划分进行分开放置 + +--- +除了项目文件夹外,还有静态资源**resources**部分,主要存放**config**、**mappers**和静态网页资源以及下载文件。 + +**mappers**文件夹主要存放所有的Mybatis Mappers,以便于MyBatis进行扫描。 +**config**文件夹目前仅存放application.yml文件,用于进行SpringBoot和MyBatis基础配置。 \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..94f59e9 --- /dev/null +++ b/build.gradle @@ -0,0 +1,44 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.2.1' + id 'io.spring.dependency-management' version '1.1.4' +} + +group = 'com.realtime' +version = '0.0.1-SNAPSHOT' + +java { + sourceCompatibility = '17' +} + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' + compileOnly 'org.projectlombok:lombok' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + runtimeOnly 'com.mysql:mysql-connector-j' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3' + + // Sa-Token 权限认证,在线文档:https://sa-token.cc + implementation 'cn.dev33:sa-token-spring-boot3-starter:1.37.0' + + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' + +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..d64cd49 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..1af9e09 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1aa94a4 --- /dev/null +++ b/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..6689b85 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..7880f5e --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'protection' diff --git a/src/main/java/com/realtime/protection/ProtectionApplication.java b/src/main/java/com/realtime/protection/ProtectionApplication.java new file mode 100644 index 0000000..4effcda --- /dev/null +++ b/src/main/java/com/realtime/protection/ProtectionApplication.java @@ -0,0 +1,13 @@ +package com.realtime.protection; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ProtectionApplication { + + public static void main(String[] args) { + SpringApplication.run(ProtectionApplication.class, args); + } + +} diff --git a/src/main/java/com/realtime/protection/configuration/entity/user/User.java b/src/main/java/com/realtime/protection/configuration/entity/user/User.java new file mode 100644 index 0000000..9f2dd19 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/entity/user/User.java @@ -0,0 +1,14 @@ +package com.realtime.protection.configuration.entity.user; + +import lombok.Data; + +@Data +public class User { + private int userID; + + private String username; + + private String password; + + private String userDepart; +} diff --git a/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java b/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java new file mode 100644 index 0000000..cc6e85f --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java @@ -0,0 +1,33 @@ +package com.realtime.protection.configuration.entity.whitelist; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Data +public class WhiteListObject { + @JsonProperty("whiteobj_id") + private int whiteListId; + + @NotNull + @JsonProperty("whiteobj_name") + private String whiteListName; + + @JsonProperty("whiteobj_system_name") + private String whiteListSystemName; + + @JsonProperty("whiteobj_ip_address") + private String whiteListIP; + + @JsonProperty("whiteobj_port") + private int whiteListPort; + + @JsonProperty("whiteobj_url") + private String whiteListUrl; + + @JsonProperty("whiteobj_protocol") + private String whiteListProtocol; + + @JsonProperty("audit_status") + private String whiteListAuditStatus; +} diff --git a/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java b/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..3dc03ce --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/exception/GlobalExceptionHandler.java @@ -0,0 +1,18 @@ +package com.realtime.protection.configuration.exception; + +import com.realtime.protection.configuration.response.ResponseResult; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler + public ResponseResult handleGlobalException(Exception e) { + + + + + return ResponseResult.error().setMessage(e.getMessage()); + } +} diff --git a/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java b/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java new file mode 100644 index 0000000..ed0f742 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java @@ -0,0 +1,67 @@ +package com.realtime.protection.configuration.response; + +import lombok.Data; + +import java.io.Serializable; +import java.util.LinkedHashMap; + +@Data +public class ResponseResult implements Serializable { + + private int code; + private String message; + private LinkedHashMap data; + + public ResponseResult(int code, String message, LinkedHashMap data) { + this.code = code; + this.message = message; + this.data = data; + } + + public ResponseResult(int code) { + this.code = code; + this.data = new LinkedHashMap<>(); + } + + public ResponseResult(int code, String message) { + this.code = code; + this.message = message; + this.data = new LinkedHashMap<>(); + } + + public static ResponseResult ok() { + return new ResponseResult(200, "request succeeded"); + } + + public static ResponseResult ok(String message) { + return new ResponseResult(200, message); + } + + public static ResponseResult error() { + return new ResponseResult(500, "request failed"); + } + + public static ResponseResult error(String message) { + return new ResponseResult(500, message); + } + + public ResponseResult setCode(int code) { + this.code = code; + return this; + } + + public ResponseResult setMessage(String message) { + this.message = message; + return this; + } + + public ResponseResult setData(String key, Object value) { + this.data.put(key, value); + return this; + } + + public ResponseResult setDataMap(LinkedHashMap data) { + this.data = data; + return this; + } +} diff --git a/src/main/java/com/realtime/protection/configuration/satoken/SaTokenConfigure.java b/src/main/java/com/realtime/protection/configuration/satoken/SaTokenConfigure.java new file mode 100644 index 0000000..a619da5 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/SaTokenConfigure.java @@ -0,0 +1,34 @@ +package com.realtime.protection.configuration.satoken; + +import cn.dev33.satoken.interceptor.SaInterceptor; +import cn.dev33.satoken.router.SaRouter; +import cn.dev33.satoken.stp.StpUtil; +import com.realtime.protection.configuration.satoken.permission.Permission; +import com.realtime.protection.configuration.satoken.permission.SystemConfiguration; +import com.realtime.protection.configuration.satoken.permission.WhiteList; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class SaTokenConfigure implements WebMvcConfigurer { + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new SaInterceptor(handler -> { + SaRouter.match("/whiteobj/new", r -> + this.checkPermissions(SystemConfiguration.NEW, WhiteList.NEW)); + SaRouter.match("/whiteobj/update", r -> + this.checkPermissions(SystemConfiguration.UPDATE, WhiteList.UPDATE)); + })) + .addPathPatterns("/**") + .excludePathPatterns("/user/doLogin"); + } + + void checkPermissions(Permission... permissions) { + for (Permission permission : permissions) { + StpUtil.checkPermission(permission.getName()); + } + } + +} + diff --git a/src/main/java/com/realtime/protection/configuration/satoken/StpInterfaceImpl.java b/src/main/java/com/realtime/protection/configuration/satoken/StpInterfaceImpl.java new file mode 100644 index 0000000..527a4ca --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/StpInterfaceImpl.java @@ -0,0 +1,36 @@ +package com.realtime.protection.configuration.satoken; + +import cn.dev33.satoken.stp.StpInterface; +import com.realtime.protection.configuration.satoken.permission.SystemConfiguration; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class StpInterfaceImpl implements StpInterface { + + /** + * 返回一个账号所拥有的权限码集合 + */ + @Override + public List getPermissionList(Object loginId, String loginType) { + ArrayList list = new ArrayList<>(); + + if (loginId.equals("endera")) { + list.add(SystemConfiguration.NEW.getName()); + } + + return list; + } + + /** + * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验) + */ + @Override + public List getRoleList(Object loginId, String loginType) { + return null; + } + +} + diff --git a/src/main/java/com/realtime/protection/configuration/satoken/permission/Nameable.java b/src/main/java/com/realtime/protection/configuration/satoken/permission/Nameable.java new file mode 100644 index 0000000..0c4e0d3 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/permission/Nameable.java @@ -0,0 +1,5 @@ +package com.realtime.protection.configuration.satoken.permission; + +public interface Nameable { + String name(); +} diff --git a/src/main/java/com/realtime/protection/configuration/satoken/permission/Permission.java b/src/main/java/com/realtime/protection/configuration/satoken/permission/Permission.java new file mode 100644 index 0000000..763eeaa --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/permission/Permission.java @@ -0,0 +1,7 @@ +package com.realtime.protection.configuration.satoken.permission; + +public interface Permission extends Nameable { + default String getName() { + return this.getClass().getSimpleName() + ":" + this.name(); + } +} diff --git a/src/main/java/com/realtime/protection/configuration/satoken/permission/SystemConfiguration.java b/src/main/java/com/realtime/protection/configuration/satoken/permission/SystemConfiguration.java new file mode 100644 index 0000000..a34e047 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/permission/SystemConfiguration.java @@ -0,0 +1,8 @@ +package com.realtime.protection.configuration.satoken.permission; + +public enum SystemConfiguration implements Permission { + NEW, + QUERY, + UPDATE, + DELETE +} diff --git a/src/main/java/com/realtime/protection/configuration/satoken/permission/WhiteList.java b/src/main/java/com/realtime/protection/configuration/satoken/permission/WhiteList.java new file mode 100644 index 0000000..1966c8a --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/satoken/permission/WhiteList.java @@ -0,0 +1,8 @@ +package com.realtime.protection.configuration.satoken.permission; + +public enum WhiteList implements Permission { + NEW, + QUERY, + UPDATE, + DELETE +} diff --git a/src/main/java/com/realtime/protection/server/user/login/LoginController.java b/src/main/java/com/realtime/protection/server/user/login/LoginController.java new file mode 100644 index 0000000..1693e74 --- /dev/null +++ b/src/main/java/com/realtime/protection/server/user/login/LoginController.java @@ -0,0 +1,39 @@ +package com.realtime.protection.server.user.login; + +import com.realtime.protection.configuration.entity.user.User; +import com.realtime.protection.configuration.response.ResponseResult; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.security.auth.login.LoginException; + +// Just for example, not in production environment +@RestController +@RequestMapping("/user") +public class LoginController { + + private final LoginService loginService; + + public LoginController(LoginService loginService) { + this.loginService = loginService; + } + + @PostMapping("/doLogin") + public ResponseResult doLogin(@RequestBody User user) { + Integer userId; + + try { + userId = loginService.login(user); + } catch (LoginException e) { + return ResponseResult.error().setMessage("User not valid") + .setData("userId", null) + .setData("success", false); + } + + return ResponseResult.ok().setMessage("success") + .setData("userId", userId) + .setData("success", true); + } +} diff --git a/src/main/java/com/realtime/protection/server/user/login/LoginMapper.java b/src/main/java/com/realtime/protection/server/user/login/LoginMapper.java new file mode 100644 index 0000000..4a446db --- /dev/null +++ b/src/main/java/com/realtime/protection/server/user/login/LoginMapper.java @@ -0,0 +1,10 @@ +package com.realtime.protection.server.user.login; + +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +@Mapper +// just for example, not for production environment +public interface LoginMapper { + Integer login(@Param("username") String username, @Param("password") String password); +} diff --git a/src/main/java/com/realtime/protection/server/user/login/LoginService.java b/src/main/java/com/realtime/protection/server/user/login/LoginService.java new file mode 100644 index 0000000..e36afb4 --- /dev/null +++ b/src/main/java/com/realtime/protection/server/user/login/LoginService.java @@ -0,0 +1,31 @@ +package com.realtime.protection.server.user.login; + +import cn.dev33.satoken.stp.StpUtil; +import com.realtime.protection.configuration.entity.user.User; +import org.springframework.stereotype.Service; + +import javax.security.auth.login.LoginException; + +@Service +// just for example, not for production environment +public class LoginService { + + private final LoginMapper loginMapper; + + public LoginService(LoginMapper loginMapper) { + this.loginMapper = loginMapper; + } + + public Integer login(User user) throws LoginException { + String username = user.getUsername(); + String password = user.getPassword(); + + Integer userId = loginMapper.login(username, password); + if (userId == null) { + throw new LoginException(); + } + + StpUtil.login(userId); + return userId; + } +} diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java new file mode 100644 index 0000000..4031de8 --- /dev/null +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java @@ -0,0 +1,28 @@ +package com.realtime.protection.server.whitelist; + +import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import com.realtime.protection.configuration.response.ResponseResult; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/whiteobj") +public class WhiteListController { + + private final WhiteListService whiteListService; + + public WhiteListController(WhiteListService whiteListService) { + this.whiteListService = whiteListService; + } + + @RequestMapping("/new") + public ResponseResult newWhitelistObject(@RequestBody WhiteListObject object) { + Integer whiteListObjectId = whiteListService.newWhiteListObject(object); + + return ResponseResult.ok() + .setData("whiteobj_name", object.getWhiteListName()) + .setData("whiteobj_id", whiteListObjectId) + .setData("success", true); + } +} diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java new file mode 100644 index 0000000..f94c10e --- /dev/null +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java @@ -0,0 +1,11 @@ +package com.realtime.protection.server.whitelist; + +import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +@Mapper +public interface WhiteListMapper { + + void newWhiteListObject(@Param("object") WhiteListObject object); +} diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java new file mode 100644 index 0000000..ebceacc --- /dev/null +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java @@ -0,0 +1,21 @@ +package com.realtime.protection.server.whitelist; + +import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import org.springframework.stereotype.Service; + +@Service +public class WhiteListService { + + private final WhiteListMapper whiteListMapper; + + public WhiteListService(WhiteListMapper whiteListMapper) { + this.whiteListMapper = whiteListMapper; + } + + public Integer newWhiteListObject(WhiteListObject object) { + + whiteListMapper.newWhiteListObject(object); + + return object.getWhiteListId(); + } +} diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml new file mode 100644 index 0000000..19be35d --- /dev/null +++ b/src/main/resources/config/application.yml @@ -0,0 +1,15 @@ +server: + port: 8080 + +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + username: root + password: aiihhbfcsy123!@# + url: jdbc:mysql://localhost:3306/realtime_protection + mvc: + servlet: + path: /api/v1 + +mybatis: + mapper-locations: classpath:mappers/*.xml \ No newline at end of file diff --git a/src/main/resources/mappers/LoginMapper.xml b/src/main/resources/mappers/LoginMapper.xml new file mode 100644 index 0000000..52628be --- /dev/null +++ b/src/main/resources/mappers/LoginMapper.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/mappers/WhiteListMapper.xml b/src/main/resources/mappers/WhiteListMapper.xml new file mode 100644 index 0000000..ae70902 --- /dev/null +++ b/src/main/resources/mappers/WhiteListMapper.xml @@ -0,0 +1,17 @@ + + + + + insert into t_white_list(white_list_name, white_list_system_name, + white_list_ip, white_list_port, + white_list_url, white_list_protocol, + white_list_audit_status) + values (#{object.whiteListName}, #{object.whiteListSystemName}, + INET_ATON(#{object.whiteListIP}), #{object.whiteListPort}, + #{object.whiteListUrl}, #{object.whiteListProtocol}, + 0) + + \ No newline at end of file diff --git a/src/test/java/com/realtime/protection/ProtectionApplicationTests.java b/src/test/java/com/realtime/protection/ProtectionApplicationTests.java new file mode 100644 index 0000000..1bc42e9 --- /dev/null +++ b/src/test/java/com/realtime/protection/ProtectionApplicationTests.java @@ -0,0 +1,13 @@ +package com.realtime.protection; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ProtectionApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/src/test/java/com/realtime/protection/server/user/login/LoginServiceTest.java b/src/test/java/com/realtime/protection/server/user/login/LoginServiceTest.java new file mode 100644 index 0000000..4dc8c04 --- /dev/null +++ b/src/test/java/com/realtime/protection/server/user/login/LoginServiceTest.java @@ -0,0 +1,43 @@ +package com.realtime.protection.server.user.login; + +import com.realtime.protection.configuration.entity.user.User; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import javax.security.auth.login.LoginException; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class LoginServiceTest { + + private final LoginService loginService; + + @Autowired + LoginServiceTest(LoginService loginService) { + this.loginService = loginService; + } + + @Test + void testLoginFail() { + User user = new User(); + user.setPassword("12345"); + user.setUsername("endera"); + + assertThrows(LoginException.class, () -> loginService.login(user)); + + user.setUsername(""); + user.setPassword(""); + assertThrows(LoginException.class, () -> loginService.login(user)); + } + + @Test + void testLoginSuccess() { + User user = new User(); + user.setUsername("endera"); + user.setPassword("123456"); + + assertDoesNotThrow(() -> assertEquals(1, loginService.login(user))); + } +} \ No newline at end of file diff --git a/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java b/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java new file mode 100644 index 0000000..4bcc859 --- /dev/null +++ b/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java @@ -0,0 +1,28 @@ +package com.realtime.protection.server.whitelist; + +import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class WhiteListServiceTest { + + private final WhiteListService whiteListService; + + @Autowired + WhiteListServiceTest(WhiteListService whiteListService) { + this.whiteListService = whiteListService; + } + + @Test + void testNewWhiteList() { + WhiteListObject object = new WhiteListObject(); + object.setWhiteListName("test"); + + Integer objectId = whiteListService.newWhiteListObject(object); + assertTrue(objectId > 0); + } +} \ No newline at end of file