module.controller.SystemCtrl.java Source code

Java tutorial

Introduction

Here is the source code for module.controller.SystemCtrl.java

Source

/**
 * Copyright (c) 2015-2016, Javen Zhou  (javenlife@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package module.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import frame.plugin.collerbind.Coller;
import module.validator.SigninValidator;

/**
 * @author hadong
 */
@Coller(value = { "/system" }, path = "system")
public class SystemCtrl extends Controller {
    public static String SYSTEM_LOGIN_PAGE = "/system/login";
    public static String SYSTEM_LOGIN_SUCCESS = "/config/index";
    public static String SYSTEM_NOT_FOUND = "/system/err404";
    public static String SYSTEM_ERROR = "/system/err500";

    // ?
    public void index() {
        render("login.htm");
    }

    // ?
    public void login() {
        index();
    }

    // Action
    @Before(SigninValidator.class)
    public void signin() {
        if ("GET".equalsIgnoreCase(this.getRequest().getMethod().toUpperCase())) {
            forwardAction(SYSTEM_LOGIN_PAGE);
        } else if ("POST".equalsIgnoreCase(this.getRequest().getMethod().toUpperCase())) {
            String username = getPara("username");
            String password = getPara("password");
            String rememberMe = getPara("rememberMe");
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password,
                    "on".equalsIgnoreCase(rememberMe));
            try {
                currentUser.login(token);
                redirect(getCookie("_redrictUrl", SYSTEM_LOGIN_SUCCESS));
            } catch (Exception e) {
                // 
                String esn = e.getClass().getSimpleName();
                if ("IncorrectCredentialsException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "?????");
                } else if ("UnknownAccountException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "???");
                } else if ("LockedAccountException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "??");
                } else if ("AuthenticationException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "??");
                } else if ("ExcessiveAttemptsException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "10??");
                } else if ("DisabledAccountException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "??");
                } else if ("ExpiredCredentialsException".equalsIgnoreCase(esn)) {
                    setAttr("errorMsg", "?");
                } else {
                    setAttr("errorMsg", "?");
                }
                setAttr("username", username);
                setAttr("rememberMe", rememberMe);
                forwardAction(SYSTEM_LOGIN_PAGE);
            }
        }

    }

    // Action
    public void signout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        redirect(SYSTEM_LOGIN_PAGE);
    }

    public void err401() {
        setAttr("msg", "401 Unauthorized");
        setAttr("success", false);
        renderJson();
    }

    public void err403() {
        setAttr("msg", "403 Forbidden");
        setAttr("success", false);
        renderJson();
    }

    public void err404() {
        render("error/404.htm");
    }

    public void err500() {
        render("error/500.htm");
    }
}