Commit dfc9ab82 by WeiCong

后台验证码支持集群模式

parent 9c731c0d
...@@ -33,94 +33,93 @@ import java.util.*; ...@@ -33,94 +33,93 @@ import java.util.*;
@Controller @Controller
public class LoginController { public class LoginController {
private static final Log log = LogFactory.getLog(LoginController.class); public static final String ERROR = "error";
public static final String ERROR="error"; private static final Log log = LogFactory.getLog(LoginController.class);
@Autowired
@Autowired private NoUiVersion noUiVersion;
private NoUiVersion noUiVersion;
@ResponseBody
@ResponseBody @RequestMapping(value = "/login", method = RequestMethod.POST)
@RequestMapping(value = "/login", method = RequestMethod.POST) public Object login(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
public Object login(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) { NoUiContext context=null;
// TODO??? try {
//System.out.println(dataMap); NoUiRequest noUiRequest = new NoUiRequest(request, "", dataMap);
NoUiRequest noUiRequest = new NoUiRequest(request, "", dataMap); Map<String, Object> map = new HashMap<>();//此map会存储返回错误码
Map<String, Object> map = new HashMap<>();//此map会存储返回错误码 String userId = noUiRequest.getDataMap().get(Constants.USERNAME).toString();
String userId = noUiRequest.getDataMap().get(Constants.USERNAME).toString(); String password = noUiRequest.getDataMap().get(Constants.PASSWORD).toString();
String password = noUiRequest.getDataMap().get(Constants.PASSWORD).toString(); String dncode = noUiRequest.getDataMap().get(Constants.DNCODE) == null ? null : noUiRequest.getDataMap().get(Constants.DNCODE).toString();
String dncode = noUiRequest.getDataMap().get(Constants.DNCODE)==null?null:noUiRequest.getDataMap().get(Constants.DNCODE).toString(); String sendcode = noUiRequest.getDataMap().get(Constants.SENDCODE) == null ? null : noUiRequest.getDataMap().get(Constants.SENDCODE).toString();
String sendcode = noUiRequest.getDataMap().get(Constants.SENDCODE)==null?null:noUiRequest.getDataMap().get(Constants.SENDCODE).toString(); // String verifycode = request.getSession().getAttribute(Constants.VERIFYCODE)==null?null:request.getSession().getAttribute(Constants.VERIFYCODE).toString();
String verifycode = request.getSession().getAttribute(Constants.VERIFYCODE)==null?null:request.getSession().getAttribute(Constants.VERIFYCODE).toString(); Object verifycodeobj = RedisUtil.get(request.getRequestedSessionId());
map.put("j_username", userId); String verifycode = verifycodeobj == null ? null : verifycodeobj.toString();
map.put("j_password", password); map.put("j_username", userId);
map.put("j_dncode", dncode); map.put("j_password", password);
map.put("j_sendcode", sendcode); map.put("j_dncode", dncode);
map.put("j_verifycode", verifycode); map.put("j_sendcode", sendcode);
NoUiContext context = NoUiContextManager.createNoUiContext(noUiRequest); map.put("j_verifycode", verifycode);
try { context = NoUiContextManager.createNoUiContext(noUiRequest);
if (context.getSession().login(map)) {
if (context.getSession().login(map)) {
JwtLogin login = new JwtLogin();
login.setUserId(userId); JwtLogin login = new JwtLogin();
login.setPassword(password); login.setUserId(userId);
// 给用户jwt加密生成token login.setPassword(password);
String token = JWT.sign(login, NumericUtil.sessionTimeOut()); // 给用户jwt加密生成token
String token = JWT.sign(login, NumericUtil.sessionTimeOut());
// 登陆成功后获取菜单并跳转到office交易
Map<String, Object> authMap = new HashMap<>(); // 登陆成功后获取菜单并跳转到office交易
authMap.put(ILoginContext.REQUEST, request); Map<String, Object> authMap = new HashMap<>();
context.getSession().auth(authMap); authMap.put(ILoginContext.REQUEST, request);
IMenuItem menuItem = context.getAuth().getMenu(); context.getSession().auth(authMap);
// List<Map<String, String>> menuList = new IMenuItem menuItem = context.getAuth().getMenu();
// ArrayList<Map<String, String>>(); // List<Map<String, String>> menuList = new
// getMenu(menuItem, menuList); // ArrayList<Map<String, String>>();
List<Menu> menuList = new ArrayList<>(); // getMenu(menuItem, menuList);
getMenus(menuItem, menuList); List<Menu> menuList = new ArrayList<>();
getMenus(menuItem, menuList);
Map<String, Object> retDatamap = new HashMap<String, Object>();
retDatamap.put("token", token); Map<String, Object> retDatamap = new HashMap<String, Object>();
retDatamap.put("menu", menuList); retDatamap.put("token", token);
retDatamap.put("salt", UUID.randomUUID().toString()); retDatamap.put("menu", menuList);
context.getSession().chain(true, "office"); retDatamap.put("salt", UUID.randomUUID().toString());
byte[] sysmodBytes = NoUiPresentationUtil.sysmodToBytes(context); context.getSession().chain(true, "office");
// redis中存储用户相关信息 byte[] sysmodBytes = NoUiPresentationUtil.sysmodToBytes(context);
RedisLoginInfo redisLoginInfo = new RedisLoginInfo(userId, token, NumericUtil.sessionTimeOut(), sysmodBytes,noUiRequest.getTerminalType()); // redis中存储用户相关信息
RedisUtil.set(StringUtil.userUniqueId(noUiRequest), redisLoginInfo); RedisLoginInfo redisLoginInfo = new RedisLoginInfo(userId, token, NumericUtil.sessionTimeOut(), sysmodBytes, noUiRequest.getTerminalType());
RedisUtil.set(StringUtil.userUniqueId(noUiRequest), redisLoginInfo);
//解决初次登陆,超期限登陆
final Object o = map.get(ERROR); //解决初次登陆,超期限登陆
if (Objects.isNull(o)) { final Object o = map.get(ERROR);
return ResultUtil.result(ErrorCodes.SUCCESS, ErrorCodes.SUCCESS_INFO, retDatamap, noUiVersion.getVersion()); if (Objects.isNull(o)) {
}else { return ResultUtil.result(ErrorCodes.SUCCESS, ErrorCodes.SUCCESS_INFO, retDatamap, noUiVersion.getVersion());
ErrorCode errorCode = (ErrorCode) o; } else {
return ResultUtil.result(errorCode.getCode(), errorCode.getMessage(), retDatamap, noUiVersion.getVersion()); ErrorCode errorCode = (ErrorCode) o;
} return ResultUtil.result(errorCode.getCode(), errorCode.getMessage(), retDatamap, noUiVersion.getVersion());
}else { }
final ErrorCode errorCode = (ErrorCode) map.get(ERROR); } else {
return ResultUtil.result(errorCode.getCode(), errorCode.getMessage(), null, noUiVersion.getVersion()); final ErrorCode errorCode = (ErrorCode) map.get(ERROR);
} return ResultUtil.result(errorCode.getCode(), errorCode.getMessage(), null, noUiVersion.getVersion());
} catch (Exception e) { }
log.error("Login command error", e); } catch (Exception e) {
//throw new NoUiException("Login command error", e); log.error("Login command error", e);
//throw new NoUiException("Login command error", e);
// return ResultUtil.result(ErrorCodes.ERROR, "login failed with exception", null); // return ResultUtil.result(ErrorCodes.ERROR, "login failed with exception", null);
return ResultUtil.result(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage(), null, noUiVersion.getVersion()); return ResultUtil.result(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMessage(), null, noUiVersion.getVersion());
} } finally {
finally{ if (context != null) {
if (context != null) //context.getSupport().disconnect();
{ context.dispose();
//context.getSupport().disconnect(); }
context.dispose(); }
}
}
// log.debug("Login command is finished"); // log.debug("Login command is finished");
// return ResultUtil.result(ErrorCodes.ERROR, "login failed", null); // return ResultUtil.result(ErrorCodes.ERROR, "login failed", null);
} }
/* /*
* private void getMenu(IMenuItem menuItem, List<Map<String, String>> * private void getMenu(IMenuItem menuItem, List<Map<String, String>>
* menuList) { Iterator<IMenuItem> iterator = menuItem.iterator(); while * menuList) { Iterator<IMenuItem> iterator = menuItem.iterator(); while
* (iterator.hasNext()) { IMenuItem item = iterator.next(); if * (iterator.hasNext()) { IMenuItem item = iterator.next(); if
* (item.iterator().hasNext()) { getMenu(item, menuList); } else { * (item.iterator().hasNext()) { getMenu(item, menuList); } else {
...@@ -129,26 +128,25 @@ public class LoginController { ...@@ -129,26 +128,25 @@ public class LoginController {
* } * }
*/ */
private void getMenus(IMenuItem menuItem, List<Menu> menuList) { private void getMenus(IMenuItem menuItem, List<Menu> menuList) {
Iterator<IMenuItem> iterator = menuItem.iterator(); Iterator<IMenuItem> iterator = menuItem.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
IMenuItem item = iterator.next(); IMenuItem item = iterator.next();
Menu menu = new Menu(); Menu menu = new Menu();
menu.setValue(item.getValue()); menu.setValue(item.getValue());
menu.setLabel(item.getLabel()); menu.setLabel(item.getLabel());
menuList.add(menu); menuList.add(menu);
List<Menu> subMenuList = menu.getSubMenu(); List<Menu> subMenuList = menu.getSubMenu();
getMenus(item, subMenuList); getMenus(item, subMenuList);
} }
} }
@ResponseBody @ResponseBody
@RequestMapping(value = "/logout", method = RequestMethod.POST) @RequestMapping(value = "/logout", method = RequestMethod.POST)
public Object logout(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) { public Object logout(@RequestBody Map<String, Object> dataMap, HttpServletRequest request, HttpSession session) {
return "ok";
return "ok"; }
}
} }
...@@ -10,15 +10,14 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -10,15 +10,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.sss.presentation.noui.api.response.ErrorCodes; import org.sss.presentation.noui.api.response.ErrorCodes;
import org.sss.presentation.noui.api.response.NoUiVersion; import org.sss.presentation.noui.api.response.NoUiVersion;
import org.sss.presentation.noui.api.response.ResultUtil; import org.sss.presentation.noui.api.response.ResultUtil;
import org.sss.presentation.noui.common.Constants;
import org.sss.presentation.noui.util.NoUiUtils; import org.sss.presentation.noui.util.NoUiUtils;
import org.sss.presentation.noui.util.RedisUtil;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
...@@ -67,9 +66,11 @@ public class VerifyCodeCreateController { ...@@ -67,9 +66,11 @@ public class VerifyCodeCreateController {
g.drawString(code, (i * 18) + 10, 30); g.drawString(code, (i * 18) + 10, 30);
codes.append(code); codes.append(code);
} }
request.getSession().setAttribute(Constants.VERIFYCODE, codes.toString()); RedisUtil.set(request.getRequestedSessionId(), codes.toString());
// request.getSession().setAttribute(Constants.VERIFYCODE, codes.toString());
ImageIO.write(bi, "JPG", response.getOutputStream()); ImageIO.write(bi, "JPG", response.getOutputStream());
} catch (IOException e) { } catch (Exception e) {
log.error("绘制登陆验证码异常", e); log.error("绘制登陆验证码异常", e);
} finally { } finally {
if(g!=null){ if(g!=null){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment