// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/core/util/ReturnT.java
// public class ReturnT<T> {
// 
// 	public static final int SUCCESS_CODE = 200;
// 	public static final int FAIL_CODE = 500;
// 
// 	public static final ReturnT<String> SUCCESS = new ReturnT<String>(null);
// 	public static final ReturnT<String> FAIL = new ReturnT<String>(FAIL_CODE, null);
// 
// 	private int code;
// 	private String msg;
// 	private T data;
// 
// 	public ReturnT(int code, String msg) {
// 		this.code = code;
// 		this.msg = msg;
// 	}
// 	public ReturnT(T data) {
// 		this.code = SUCCESS_CODE;
// 		this.data = data;
// 	}
// 
// 
// 	public int getCode() {
// 		return code;
// 	}
// 
// 	public void setCode(int code) {
// 		this.code = code;
// 	}
// 
// 	public String getMsg() {
// 		return msg;
// 	}
// 
// 	public void setMsg(String msg) {
// 		this.msg = msg;
// 	}
// 
// 	public T getData() {
// 		return data;
// 	}
// 
// 	public void setData(T data) {
// 		this.data = data;
// 	}
// 
// }
// 
// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/service/impl/LoginService.java
// @Configuration
// public class LoginService {
// 
//     public static final String LOGIN_IDENTITY = "XXL_CONF_LOGIN_IDENTITY";
// 
//     @Resource
//     private XxlConfUserDao xxlConfUserDao;
// 
//     private String makeToken(XxlConfUser xxlConfUser){
//         String tokenJson = JacksonUtil.writeValueAsString(xxlConfUser);
//         String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
//         return tokenHex;
//     }
//     private XxlConfUser parseToken(String tokenHex){
//         XxlConfUser xxlConfUser = null;
//         if (tokenHex != null) {
//             String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray());      // username_password(md5)
//             xxlConfUser = JacksonUtil.readValue(tokenJson, XxlConfUser.class);
//         }
//         return xxlConfUser;
//     }
// 
//     /**
//      * login
//      *
//      * @param response
//      * @param usernameParam
//      * @param passwordParam
//      * @param ifRemember
//      * @return
//      */
//     public ReturnT<String> login(HttpServletResponse response, String usernameParam, String passwordParam, boolean ifRemember){
// 
//         XxlConfUser xxlConfUser = xxlConfUserDao.load(usernameParam);
//         if (xxlConfUser == null) {
//             return new ReturnT<String>(500, "账号或密码错误");
//         }
// 
//         String passwordParamMd5 = DigestUtils.md5DigestAsHex(passwordParam.getBytes());
//         if (!xxlConfUser.getPassword().equals(passwordParamMd5)) {
//             return new ReturnT<String>(500, "账号或密码错误");
//         }
// 
//         String loginToken = makeToken(xxlConfUser);
// 
//         // do login
//         CookieUtil.set(response, LOGIN_IDENTITY, loginToken, ifRemember);
//         return ReturnT.SUCCESS;
//     }
// 
//     /**
//      * logout
//      *
//      * @param request
//      * @param response
//      */
//     public void logout(HttpServletRequest request, HttpServletResponse response){
//         CookieUtil.remove(request, response, LOGIN_IDENTITY);
//     }
// 
//     /**
//      * logout
//      *
//      * @param request
//      * @return
//      */
//     public XxlConfUser ifLogin(HttpServletRequest request){
//         String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY);
//         if (cookieToken != null) {
//             XxlConfUser cookieUser = parseToken(cookieToken);
//             if (cookieUser != null) {
//                 XxlConfUser dbUser = xxlConfUserDao.load(cookieUser.getUsername());
//                 if (dbUser != null) {
//                     if (cookieUser.getPassword().equals(dbUser.getPassword())) {
//                         return dbUser;
//                     }
//                 }
//             }
//         }
//         return null;
//     }
// 
// }


import com.xxl.conf.admin.controller.annotation.PermessionLimit;
import com.xxl.conf.admin.core.util.ReturnT;
import com.xxl.conf.admin.service.impl.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
package com.xxl.conf.admin.controller;





/**
 * Created by xuxueli on 16/7/30.
 */

@Controller
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger(IndexController.class.getName());

    @Resource
    private LoginService loginService;

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        return "redirect:/conf";
    }

    @RequestMapping("/toLogin")
    @PermessionLimit(limit=false)
    public String toLogin(Model model, HttpServletRequest request) {
        if (loginService.ifLogin(request) != null) {
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping(value="login", method= RequestMethod.POST)
    @ResponseBody
    @PermessionLimit(limit=false)
// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/core/util/ReturnT.java
// public class ReturnT<T> {
// 
// 	public static final int SUCCESS_CODE = 200;
// 	public static final int FAIL_CODE = 500;
// 
// 	public static final ReturnT<String> SUCCESS = new ReturnT<String>(null);
// 	public static final ReturnT<String> FAIL = new ReturnT<String>(FAIL_CODE, null);
// 
// 	private int code;
// 	private String msg;
// 	private T data;
// 
// 	public ReturnT(int code, String msg) {
// 		this.code = code;
// 		this.msg = msg;
// 	}
// 	public ReturnT(T data) {
// 		this.code = SUCCESS_CODE;
// 		this.data = data;
// 	}
// 
// 
// 	public int getCode() {
// 		return code;
// 	}
// 
// 	public void setCode(int code) {
// 		this.code = code;
// 	}
// 
// 	public String getMsg() {
// 		return msg;
// 	}
// 
// 	public void setMsg(String msg) {
// 		this.msg = msg;
// 	}
// 
// 	public T getData() {
// 		return data;
// 	}
// 
// 	public void setData(T data) {
// 		this.data = data;
// 	}
// 
// }
// 
// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/service/impl/LoginService.java
// @Configuration
// public class LoginService {
// 
//     public static final String LOGIN_IDENTITY = "XXL_CONF_LOGIN_IDENTITY";
// 
//     @Resource
//     private XxlConfUserDao xxlConfUserDao;
// 
//     private String makeToken(XxlConfUser xxlConfUser){
//         String tokenJson = JacksonUtil.writeValueAsString(xxlConfUser);
//         String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
//         return tokenHex;
//     }
//     private XxlConfUser parseToken(String tokenHex){
//         XxlConfUser xxlConfUser = null;
//         if (tokenHex != null) {
//             String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray());      // username_password(md5)
//             xxlConfUser = JacksonUtil.readValue(tokenJson, XxlConfUser.class);
//         }
//         return xxlConfUser;
//     }
// 
//     /**
//      * login
//      *
//      * @param response
//      * @param usernameParam
//      * @param passwordParam
//      * @param ifRemember
//      * @return
//      */
//     public ReturnT<String> login(HttpServletResponse response, String usernameParam, String passwordParam, boolean ifRemember){
// 
//         XxlConfUser xxlConfUser = xxlConfUserDao.load(usernameParam);
//         if (xxlConfUser == null) {
//             return new ReturnT<String>(500, "账号或密码错误");
//         }
// 
//         String passwordParamMd5 = DigestUtils.md5DigestAsHex(passwordParam.getBytes());
//         if (!xxlConfUser.getPassword().equals(passwordParamMd5)) {
//             return new ReturnT<String>(500, "账号或密码错误");
//         }
// 
//         String loginToken = makeToken(xxlConfUser);
// 
//         // do login
//         CookieUtil.set(response, LOGIN_IDENTITY, loginToken, ifRemember);
//         return ReturnT.SUCCESS;
//     }
// 
//     /**
//      * logout
//      *
//      * @param request
//      * @param response
//      */
//     public void logout(HttpServletRequest request, HttpServletResponse response){
//         CookieUtil.remove(request, response, LOGIN_IDENTITY);
//     }
// 
//     /**
//      * logout
//      *
//      * @param request
//      * @return
//      */
//     public XxlConfUser ifLogin(HttpServletRequest request){
//         String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY);
//         if (cookieToken != null) {
//             XxlConfUser cookieUser = parseToken(cookieToken);
//             if (cookieUser != null) {
//                 XxlConfUser dbUser = xxlConfUserDao.load(cookieUser.getUsername());
//                 if (dbUser != null) {
//                     if (cookieUser.getPassword().equals(dbUser.getPassword())) {
//                         return dbUser;
//                     }
//                 }
//             }
//         }
//         return null;
//     }
// 
// }


// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/controller/IndexController.java
import com.xxl.conf.admin.controller.annotation.PermessionLimit;
import com.xxl.conf.admin.core.util.ReturnT;
import com.xxl.conf.admin.service.impl.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

package com.xxl.conf.admin.controller;





/**
 * Created by xuxueli on 16/7/30.
 */

@Controller
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger(IndexController.class.getName());

    @Resource
    private LoginService loginService;

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        return "redirect:/conf";
    }

    @RequestMapping("/toLogin")
    @PermessionLimit(limit=false)
    public String toLogin(Model model, HttpServletRequest request) {
        if (loginService.ifLogin(request) != null) {
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping(value="login", method= RequestMethod.POST)
    @ResponseBody
    @PermessionLimit(limit=false)
    public ReturnT<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember){