Init Commit
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.realtime.protection.configuration.satoken.permission;
|
||||
|
||||
public interface Nameable {
|
||||
String name();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.realtime.protection.configuration.satoken.permission;
|
||||
|
||||
public enum SystemConfiguration implements Permission {
|
||||
NEW,
|
||||
QUERY,
|
||||
UPDATE,
|
||||
DELETE
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.realtime.protection.configuration.satoken.permission;
|
||||
|
||||
public enum WhiteList implements Permission {
|
||||
NEW,
|
||||
QUERY,
|
||||
UPDATE,
|
||||
DELETE
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user