Init Commit

This commit is contained in:
松岳 陈
2024-01-02 10:16:15 +08:00
commit 66c710c034
31 changed files with 956 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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());
}
}

View File

@@ -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<String, Object> data;
public ResponseResult(int code, String message, LinkedHashMap<String, Object> 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<String, Object> data) {
this.data = data;
return this;
}
}

View File

@@ -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());
}
}
}

View File

@@ -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<String> getPermissionList(Object loginId, String loginType) {
ArrayList<String> list = new ArrayList<>();
if (loginId.equals("endera")) {
list.add(SystemConfiguration.NEW.getName());
}
return list;
}
/**
* 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
*/
@Override
public List<String> getRoleList(Object loginId, String loginType) {
return null;
}
}

View File

@@ -0,0 +1,5 @@
package com.realtime.protection.configuration.satoken.permission;
public interface Nameable {
String name();
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,8 @@
package com.realtime.protection.configuration.satoken.permission;
public enum SystemConfiguration implements Permission {
NEW,
QUERY,
UPDATE,
DELETE
}

View File

@@ -0,0 +1,8 @@
package com.realtime.protection.configuration.satoken.permission;
public enum WhiteList implements Permission {
NEW,
QUERY,
UPDATE,
DELETE
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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();
}
}

View File

@@ -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

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.realtime.protection.server.user.login.LoginMapper">
<select id="login" resultType="java.lang.Integer">
select test_id from t_test
where username = #{username} and passwd = #{password}
</select>
</mapper>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.realtime.protection.server.whitelist.WhiteListMapper">
<insert id="newWhiteListObject" useGeneratedKeys="true" keyProperty="whiteListId"
parameterType="com.realtime.protection.configuration.entity.whitelist.WhiteListObject">
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)
</insert>
</mapper>

View File

@@ -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() {
}
}

View File

@@ -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)));
}
}

View File

@@ -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);
}
}