Search in sources :

Example 1 with UserService

use of io.jpom.service.user.UserService in project Jpom by dromara.

the class LoadJsonConfigToDb method loadUserInfo.

public void loadUserInfo() {
    File backupOldData = FileUtil.file(ConfigBean.getInstance().getDataPath(), "backup_old_data");
    // 读取 USER 文件内容
    File file = FileUtil.file(ConfigBean.getInstance().getDataPath(), ServerConfigBean.USER);
    if (!FileUtil.exist(file)) {
        return;
    }
    try {
        JSON json = JsonFileUtil.readJson(file.getAbsolutePath());
        JSONArray jsonArray = JsonFileUtil.formatToArray((JSONObject) json);
        List<UserModel> userModels = jsonArray.toJavaList(UserModel.class);
        if (userModels == null) {
            return;
        }
        UserService userService = SpringUtil.getBean(UserService.class);
        userModels = userModels.stream().peek(userModel -> {
            // userModel.setRoles((Set<String>) null);
            userModel.setSystemUser(UserModel.SYSTEM_ADMIN.equals(userModel.getParent()) ? 1 : 0);
            // 
            String salt = userService.generateSalt();
            userModel.setSalt(salt);
            userModel.setPassword(SecureUtil.sha1(userModel.getPassword() + salt));
        }).collect(Collectors.toList());
        userService.insert(userModels);
        // 将 json 文件转移到备份目录
        FileUtil.move(file, FileUtil.mkdir(backupOldData), true);
        DefaultSystemLog.getLog().info("{} mv to {}", FileUtil.getAbsolutePath(file), FileUtil.getAbsolutePath(backupOldData));
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("load user info error ", e);
    }
}
Also used : UserService(io.jpom.service.user.UserService) JSONArray(com.alibaba.fastjson.JSONArray) JSON(com.alibaba.fastjson.JSON) File(java.io.File)

Example 2 with UserService

use of io.jpom.service.user.UserService in project Jpom by dromara.

the class LoginInterceptor method tryGetHeaderUser.

/**
 * 尝试获取 header 中的信息
 *
 * @param session ses
 * @param request req
 * @return true 获取成功
 */
private boolean tryGetHeaderUser(HttpServletRequest request, HttpSession session) {
    String header = request.getHeader(ServerOpenApi.USER_TOKEN_HEAD);
    if (StrUtil.isEmpty(header)) {
        // 兼容就版本 登录状态
        UserModel user = (UserModel) session.getAttribute(SESSION_NAME);
        return user != null;
    }
    UserService userService = SpringUtil.getBean(UserService.class);
    UserModel userModel = userService.checkUser(header);
    if (userModel == null) {
        return false;
    }
    session.setAttribute(LoginInterceptor.SESSION_NAME, userModel);
    return true;
}
Also used : UserModel(io.jpom.model.data.UserModel) UserService(io.jpom.service.user.UserService)

Example 3 with UserService

use of io.jpom.service.user.UserService in project Jpom by dromara.

the class LoginInterceptor method checkHeaderUser.

/**
 * 尝试获取 header 中的信息
 *
 * @param session ses
 * @param request req
 * @return true 获取成功
 */
private int checkHeaderUser(HttpServletRequest request, HttpSession session) {
    String token = request.getHeader(ServerOpenApi.HTTP_HEAD_AUTHORIZATION);
    if (StrUtil.isEmpty(token)) {
        return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
    }
    JWT jwt = JwtUtil.readBody(token);
    if (JwtUtil.expired(jwt, 0)) {
        int renewal = ServerExtConfigBean.getInstance().getAuthorizeRenewal();
        if (jwt == null || renewal <= 0 || JwtUtil.expired(jwt, TimeUnit.MINUTES.toSeconds(renewal))) {
            return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
        }
        return ServerConfigBean.RENEWAL_AUTHORIZE_CODE;
    }
    UserModel user = (UserModel) session.getAttribute(SESSION_NAME);
    UserService userService = SpringUtil.getBean(UserService.class);
    String id = JwtUtil.getId(jwt);
    UserModel newUser = userService.checkUser(id);
    if (newUser == null) {
        return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
    }
    if (null != user) {
        String tokenUserId = JwtUtil.readUserId(jwt);
        boolean b = user.getId().equals(tokenUserId);
        if (!b) {
            return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
        }
    }
    session.setAttribute(LoginInterceptor.SESSION_NAME, newUser);
    return 0;
}
Also used : UserModel(io.jpom.model.data.UserModel) UserService(io.jpom.service.user.UserService) JWT(cn.hutool.jwt.JWT)

Example 4 with UserService

use of io.jpom.service.user.UserService in project Jpom by dromara.

the class JpomServerApplication method main.

/**
 * 启动执行
 * --rest:ip_config 重置 IP 白名单配置
 * --rest:load_init_db 重新加载数据库初始化操作
 * --rest:super_user_pwd 重置超级管理员密码
 * --recover:h2db 当 h2 数据出现奔溃无法启动需要执行恢复逻辑
 * --close:super_user_mfa 重置超级管理员 mfa
 *
 * @param args 参数
 * @throws Exception 异常
 */
public static void main(String[] args) throws Exception {
    long time = SystemClock.now();
    if (ArrayUtil.containsIgnoreCase(args, "--rest:load_init_db")) {
        loadInitDb = true;
    }
    if (ArrayUtil.containsIgnoreCase(args, "--recover:h2db")) {
        recoverH2Db = true;
    }
    // 
    JpomApplication jpomApplication = new JpomApplication(Type.Server, JpomServerApplication.class, args);
    jpomApplication.addInterceptor(IpInterceptor.class).addInterceptor(LoginInterceptor.class).addInterceptor(OpenApiInterceptor.class).addInterceptor(PermissionInterceptor.class).run(args);
    // 重置 ip 白名单配置
    if (ArrayUtil.containsIgnoreCase(args, "--rest:ip_config")) {
        SystemParametersServer parametersServer = SpringUtil.getBean(SystemParametersServer.class);
        parametersServer.delByKey(SystemIpConfigModel.ID);
        Console.log("Clear IP whitelist configuration successfully");
    }
    // 重置超级管理员密码
    if (ArrayUtil.containsIgnoreCase(args, "--rest:super_user_pwd")) {
        UserService userService = SpringUtil.getBean(UserService.class);
        String restResult = userService.restSuperUserPwd();
        if (restResult != null) {
            Console.log(restResult);
        } else {
            Console.log("There is no super administrator account in the system");
        }
    }
    // 关闭超级管理员 mfa
    if (ArrayUtil.containsIgnoreCase(args, "--close:super_user_mfa")) {
        UserService userService = SpringUtil.getBean(UserService.class);
        String restResult = userService.closeSuperUserMfa();
        if (restResult != null) {
            Console.log(restResult);
        } else {
            Console.log("There is no super administrator account in the system");
        }
    }
    Console.log("Time-consuming to start this time:{}", DateUtil.formatBetween(SystemClock.now() - time, BetweenFormatter.Level.MILLISECOND));
}
Also used : LoginInterceptor(io.jpom.common.interceptor.LoginInterceptor) UserService(io.jpom.service.user.UserService) PermissionInterceptor(io.jpom.common.interceptor.PermissionInterceptor) SystemParametersServer(io.jpom.service.system.SystemParametersServer)

Example 5 with UserService

use of io.jpom.service.user.UserService in project Jpom by dromara.

the class ServerWebSocketInterceptor method beforeHandshake.

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    if (request instanceof ServletServerHttpRequest) {
        ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
        HttpServletRequest httpServletRequest = serverHttpRequest.getServletRequest();
        // 判断用户
        String userId = httpServletRequest.getParameter("userId");
        UserService userService = SpringUtil.getBean(UserService.class);
        UserModel userModel = userService.checkUser(userId);
        if (userModel == null) {
            return false;
        }
        boolean checkNode = this.checkNode(httpServletRequest, attributes, userModel);
        HandlerType handlerType = this.fromType(httpServletRequest);
        if (!checkNode || handlerType == null) {
            return false;
        }
        switch(handlerType) {
            case console:
                {
                    // 控制台
                    Object dataItem = this.checkData(handlerType, userModel, httpServletRequest);
                    if (dataItem == null) {
                        return false;
                    }
                    attributes.put("copyId", httpServletRequest.getParameter("copyId"));
                    attributes.put("projectId", BeanUtil.getProperty(dataItem, "projectId"));
                    attributes.put("dataItem", dataItem);
                    break;
                }
            case nodeScript:
                {
                    // 节点脚本模板
                    Object dataItem = this.checkData(handlerType, userModel, httpServletRequest);
                    if (dataItem == null) {
                        return false;
                    }
                    attributes.put("dataItem", dataItem);
                    attributes.put("scriptId", BeanUtil.getProperty(dataItem, "scriptId"));
                    break;
                }
            case script:
                {
                    // 脚本模板
                    Object dataItem = this.checkData(handlerType, userModel, httpServletRequest);
                    if (dataItem == null) {
                        return false;
                    }
                    attributes.put("dataItem", dataItem);
                    attributes.put("scriptId", BeanUtil.getProperty(dataItem, "id"));
                    break;
                }
            case tomcat:
                String tomcatId = httpServletRequest.getParameter("tomcatId");
                attributes.put("tomcatId", tomcatId);
                break;
            case dockerLog:
            case ssh:
                {
                    Object dataItem = this.checkData(handlerType, userModel, httpServletRequest);
                    if (dataItem == null) {
                        return false;
                    }
                    attributes.put("dataItem", dataItem);
                    break;
                }
            case docker:
                Object dataItem = this.checkData(handlerType, userModel, httpServletRequest);
                if (dataItem == null) {
                    return false;
                }
                attributes.put("containerId", httpServletRequest.getParameter("containerId"));
                attributes.put("dataItem", dataItem);
                break;
            case nodeUpdate:
                break;
            default:
                return false;
        }
        String permissionMsg = this.checkPermission(userModel, attributes, handlerType);
        attributes.put("permissionMsg", permissionMsg);
        // 
        String ip = ServletUtil.getClientIP(httpServletRequest);
        attributes.put("ip", ip);
        // 
        String userAgent = ServletUtil.getHeaderIgnoreCase(httpServletRequest, HttpHeaders.USER_AGENT);
        attributes.put(HttpHeaders.USER_AGENT, userAgent);
        attributes.put("userInfo", userModel);
        return true;
    }
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserModel(io.jpom.model.data.UserModel) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UserService(io.jpom.service.user.UserService)

Aggregations

UserService (io.jpom.service.user.UserService)5 UserModel (io.jpom.model.data.UserModel)3 JWT (cn.hutool.jwt.JWT)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1 LoginInterceptor (io.jpom.common.interceptor.LoginInterceptor)1 PermissionInterceptor (io.jpom.common.interceptor.PermissionInterceptor)1 SystemParametersServer (io.jpom.service.system.SystemParametersServer)1 File (java.io.File)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1