// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/core/model/XxlConfUser.java
// public class XxlConfUser {
// 
//     private String username;
//     private String password;
//     private int permission;             // 权限：0-普通用户、1-管理员
//     private String permissionData;      // 权限配置数据, 格式 "appname#env,appname#env02"
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public int getPermission() {
//         return permission;
//     }
// 
//     public void setPermission(int permission) {
//         this.permission = permission;
//     }
// 
//     public String getPermissionData() {
//         return permissionData;
//     }
// 
//     public void setPermissionData(String permissionData) {
//         this.permissionData = permissionData;
//     }
// 
// }
// 
// 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.model.XxlConfUser;
import com.xxl.conf.admin.service.impl.LoginService;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
package com.xxl.conf.admin.controller.interceptor;



/**
 * 权限拦截, 简易版
 * @author xuxueli 2015-12-12 18:09:04
 */
@Component
public class PermissionInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private LoginService loginService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		if (!(handler instanceof HandlerMethod)) {
			return super.preHandle(request, response, handler);
		}

		// if need login
		boolean needLogin = true;
		boolean needAdminuser = false;
		HandlerMethod method = (HandlerMethod)handler;
		PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class);
		if (permission!=null) {
			needLogin = permission.limit();
			needAdminuser = permission.adminuser();
		}

		if (needLogin) {
			XxlConfUser loginUser = loginService.ifLogin(request);
			if (loginUser == null) {
				response.sendRedirect(request.getContextPath() + "/toLogin");
				//request.getRequestDispatcher("/toLogin").forward(request, response);
				return false;
			}
			if (needAdminuser && loginUser.getPermission()!=1) {
				throw new RuntimeException("权限拦截");
			}
// Path: xxl-conf-admin/src/main/java/com/xxl/conf/admin/core/model/XxlConfUser.java
// public class XxlConfUser {
// 
//     private String username;
//     private String password;
//     private int permission;             // 权限：0-普通用户、1-管理员
//     private String permissionData;      // 权限配置数据, 格式 "appname#env,appname#env02"
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public int getPermission() {
//         return permission;
//     }
// 
//     public void setPermission(int permission) {
//         this.permission = permission;
//     }
// 
//     public String getPermissionData() {
//         return permissionData;
//     }
// 
//     public void setPermissionData(String permissionData) {
//         this.permissionData = permissionData;
//     }
// 
// }
// 
// 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/interceptor/PermissionInterceptor.java
import com.xxl.conf.admin.controller.annotation.PermessionLimit;
import com.xxl.conf.admin.core.model.XxlConfUser;
import com.xxl.conf.admin.service.impl.LoginService;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

package com.xxl.conf.admin.controller.interceptor;



/**
 * 权限拦截, 简易版
 * @author xuxueli 2015-12-12 18:09:04
 */
@Component
public class PermissionInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private LoginService loginService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		if (!(handler instanceof HandlerMethod)) {
			return super.preHandle(request, response, handler);
		}

		// if need login
		boolean needLogin = true;
		boolean needAdminuser = false;
		HandlerMethod method = (HandlerMethod)handler;
		PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class);
		if (permission!=null) {
			needLogin = permission.limit();
			needAdminuser = permission.adminuser();
		}

		if (needLogin) {
			XxlConfUser loginUser = loginService.ifLogin(request);
			if (loginUser == null) {
				response.sendRedirect(request.getContextPath() + "/toLogin");
				//request.getRequestDispatcher("/toLogin").forward(request, response);
				return false;
			}
			if (needAdminuser && loginUser.getPermission()!=1) {
				throw new RuntimeException("权限拦截");
			}
			request.setAttribute(LoginService.LOGIN_IDENTITY, loginUser);