All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.monitor.console.ui.UiServletController Maven / Gradle / Ivy

package org.zodiac.monitor.console.ui;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zodiac.monitor.console.constants.MonitorConsoleConstants;
import org.zodiac.monitor.console.model.UserInfo;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

/**
 * @author zodiac
 */
@Controller
public class UiServletController {

    private final String contextPath;
    private UserInfo userInfo;

    public UiServletController(final String contextPath) {
        this.contextPath = StrUtil.trimToEmpty(contextPath);
    }

    public UiServletController(final String contextPath, UserInfo userInfo) {
        this.contextPath = StrUtil.trimToEmpty(contextPath);
        this.userInfo = userInfo;
    }

    @GetMapping(path = {"/monitor", "/_monitor"}, produces = MediaType.TEXT_HTML_VALUE)
    public String index(@CookieValue(name = MonitorConsoleConstants.CONSOLE_MONITOR_ID_COOKIE_NAME, required = false) String monitorId,
        Model model) {
        model.addAttribute("baseUrl", contextPath);
        /*不设置用户名密码直接返回成功。*/
        if (userInfo.getUsername() == null || userInfo.getPassword() == null) {
            return "redirect:/monitor/index.html";
        }
        if (monitorId != null && monitorId.equals(userInfo.getUsernameToken())) {
            return "redirect:/monitor/index.html";
        }
        return "redirect:/monitor/login.html";
    }

    @ResponseBody
    @GetMapping(path = {"/monitor/context", "/_monitor/_context"})
    public String context(HttpServletRequest request) {
        String uri = request.getContextPath();

        if (!uri.endsWith("/")) {
            uri = uri + "/";
        }
        return uri;
    }

    @RequestMapping(value = {"/monitor/login", "/_monitor/_login"})
    public String setCookies(@RequestParam(name = "username", required = false) String reqUserName,
        @RequestParam(name = "password", required = false) String reqPassword, HttpServletResponse response) {
        /*不设置用户名密码直接返回成功。*/
        if (userInfo.getUsername() == null || userInfo.getPassword() == null) {
            return "redirect:/monitor/index.html";
        }
        if (reqUserName == null || reqPassword == null) {
            return "redirect:/monitor/login.html";
        }
        if (StrUtil.isNotEmpty(reqUserName) && StrUtil.isNotEmpty(reqPassword)) {
            if (reqUserName.equals(userInfo.getUsername()) && reqPassword.equals(userInfo.getPassword())) {
                Cookie cookie = new Cookie(MonitorConsoleConstants.CONSOLE_MONITOR_ID_COOKIE_NAME, userInfo.getUsernameToken());
                response.addCookie(cookie);
                return "redirect:/monitor/index.html";
            }
        }
        return "redirect:/monitor/login.html";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy