Init Commit
This commit is contained in:
@@ -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