Commit dfc9ab82 by WeiCong

后台验证码支持集群模式

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