Commit 03a3c3ba by zhanghj

V1.0.0--增加用户单端登录

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@18444 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent db4c2e68
...@@ -2,6 +2,7 @@ package com.glsx.usercenter.controller; ...@@ -2,6 +2,7 @@ package com.glsx.usercenter.controller;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -10,13 +11,6 @@ import javax.servlet.http.HttpServletRequest; ...@@ -10,13 +11,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.validation.groups.Default; import javax.validation.groups.Default;
import com.alibaba.fastjson.JSONArray;
import com.glsx.common.annotation.Log;
import com.glsx.common.feign.client.OrderClient;
import com.glsx.usercenter.mapper.SysOrganizationMapper;
import com.glsx.usercenter.pojo.menu.SysOrganization;
import com.glsx.usercenter.service.ISysOrganizationService;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException; import org.apache.shiro.authc.ExcessiveAttemptsException;
...@@ -24,7 +18,9 @@ import org.apache.shiro.authc.IncorrectCredentialsException; ...@@ -24,7 +18,9 @@ import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -37,18 +33,17 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -37,18 +33,17 @@ import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil; import com.github.pagehelper.util.StringUtil;
import com.glsx.common.annotation.Log;
import com.glsx.common.constants.ResultObject; import com.glsx.common.constants.ResultObject;
import com.glsx.common.feign.client.CommonClient; import com.glsx.common.feign.client.CommonClient;
import com.glsx.common.utils.DateUtil; import com.glsx.common.utils.DateUtil;
import com.glsx.common.utils.Page; import com.glsx.common.utils.Page;
import com.glsx.common.utils.StringUtils; import com.glsx.common.utils.StringUtils;
import com.glsx.common.utils.UserUtils;
import com.glsx.usercenter.constants.Constants; import com.glsx.usercenter.constants.Constants;
import com.glsx.usercenter.constants.ResultCodeEnum; import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.dto.MerchantInfo; import com.glsx.usercenter.dto.MerchantInfo;
import com.glsx.usercenter.mapper.SysRoleMapper; import com.glsx.usercenter.mapper.SysRoleMapper;
import com.glsx.usercenter.mapper.SysUserImageMapper; import com.glsx.usercenter.mapper.SysUserImageMapper;
import com.glsx.usercenter.pojo.Test;
import com.glsx.usercenter.pojo.user.SysManager; import com.glsx.usercenter.pojo.user.SysManager;
import com.glsx.usercenter.pojo.user.SysMechanic; import com.glsx.usercenter.pojo.user.SysMechanic;
import com.glsx.usercenter.pojo.user.SysMerchant; import com.glsx.usercenter.pojo.user.SysMerchant;
...@@ -58,10 +53,10 @@ import com.glsx.usercenter.pojo.user.SysUserImage; ...@@ -58,10 +53,10 @@ import com.glsx.usercenter.pojo.user.SysUserImage;
import com.glsx.usercenter.pojo.user.SysUserImageExample; import com.glsx.usercenter.pojo.user.SysUserImageExample;
import com.glsx.usercenter.pojo.vo.UserQueryVo; import com.glsx.usercenter.pojo.vo.UserQueryVo;
import com.glsx.usercenter.service.IAreaService; import com.glsx.usercenter.service.IAreaService;
import com.glsx.usercenter.service.ISysOrganizationService;
import com.glsx.usercenter.service.SysUserService; import com.glsx.usercenter.service.SysUserService;
import com.glsx.usercenter.utils.AccountValidatorUtil; import com.glsx.usercenter.utils.AccountValidatorUtil;
import com.glsx.usercenter.utils.QRCodeUtil; import com.glsx.usercenter.utils.QRCodeUtil;
import com.glsx.usercenter.utils.RedisCacheUtil;
import com.glsx.usercenter.utils.ShiroUtil; import com.glsx.usercenter.utils.ShiroUtil;
/** /**
...@@ -98,6 +93,8 @@ public class UserController extends BaseController { ...@@ -98,6 +93,8 @@ public class UserController extends BaseController {
@Autowired @Autowired
private OrderClient orderClient; private OrderClient orderClient;
@Autowired
private RedisSessionDAO redisSessionDAO;
/** /**
* @param userQueryVo * @param userQueryVo
...@@ -667,36 +664,63 @@ public class UserController extends BaseController { ...@@ -667,36 +664,63 @@ public class UserController extends BaseController {
if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || terminalType == null) { if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || terminalType == null) {
return new ResultObject(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMsg()); return new ResultObject(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMsg());
} }
Subject subject = SecurityUtils.getSubject(); List<SysUser> users = sysUserService.findByUserName(username);
UsernamePasswordToken token = new UsernamePasswordToken(username, password); SysUser user = null;
subject.login(token); if (users != null && !users.isEmpty()) {
List<SysUser> list = sysUserService.findByUserName(username); user = users.get(0);
SysUser sysUser = null; }
if (list != null && !list.isEmpty()) { if (user == null) {
sysUser = list.get(0); throw new UnknownAccountException();
}
String pwd = user.getPassword();
if (pwd != null && !pwd.equals(sysUserService.encryptPassword(password))) {
throw new IncorrectCredentialsException();
}
String userStatus = user.getUserStatus();
//账号被停用或注销
if (!com.glsx.common.constants.Constants.SYS_USER_STATUS_NORMAL.equals(userStatus)) {
throw new LockedAccountException();
}
/*
* 同一用户单端登录
*/
Collection<Session> sessions = redisSessionDAO.getActiveSessions();
for (Session sess : sessions) {
//shiro session可以拿到已经登陆的username
System.out.println("登录用户" + sess.getAttribute("username"));
if (username.equals(sess.getAttribute("username"))) {
redisSessionDAO.delete(sess); //session清除
sess.setTimeout(0);
}
} }
/* /*
* 各端用户只可以登录各端,除技工用户外,其它端都可以登录后台管理系统 * 各端用户只可以登录各端,除技工用户外,其它端都可以登录后台管理系统
*/ */
Integer userType = sysUser.getUserType(); Integer userType = user.getUserType();
logger.info("用户类型=" + userType); logger.info("用户类型=" + userType);
if (terminalType != userType) { if (terminalType != userType) {
if (Constants.USER_TYPE_MANAGER == terminalType) { if (Constants.USER_TYPE_MANAGER == terminalType ) {
if (Constants.USER_TYPE_MECHANIC == userType) { if (Constants.USER_TYPE_MECHANIC == userType) {
logger.info("登录后台管理系统,该账号暂无权限登录"); logger.info("登录后台管理系统,该账号暂无权限登录");
return new ResultObject(ResultCodeEnum.USER_LOGIN_NO_AUTH.getCode(), ResultCodeEnum.USER_LOGIN_NO_AUTH.getMsg()); return new ResultObject(ResultCodeEnum.USER_LOGIN_NO_AUTH.getCode(), ResultCodeEnum.USER_LOGIN_NO_AUTH.getMsg());
} }
} else { }else {
logger.info("该账号暂无权限登录"); logger.info("该账号暂无权限登录");
return new ResultObject(ResultCodeEnum.USER_LOGIN_NO_AUTH.getCode(), ResultCodeEnum.USER_LOGIN_NO_AUTH.getMsg()); return new ResultObject(ResultCodeEnum.USER_LOGIN_NO_AUTH.getCode(), ResultCodeEnum.USER_LOGIN_NO_AUTH.getMsg());
} }
} }
/*
* shiro登录校验
*/
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERNAME_KEY, username);
session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, sysUser.getId()); session.setAttribute(com.glsx.common.constants.Constants.SESSION_USERID_KEY, user.getId());
map.put("JSESSIONID", subject.getSession().getId()); map.put("JSESSIONID", subject.getSession().getId());
result.setData(map); result.setData(map);
logger.info("用户登录成功:username=" + username); logger.info("用户登录成功:username="+ username);
} catch (UnknownAccountException uae) { } catch (UnknownAccountException uae) {
logger.info("用户名和密码不匹配"); logger.info("用户名和密码不匹配");
result = new ResultObject(ResultCodeEnum.USERNAME_OR_PASSWORD_NOT_RIGHT.getCode(), ResultCodeEnum.USERNAME_OR_PASSWORD_NOT_RIGHT.getMsg()); result = new ResultObject(ResultCodeEnum.USERNAME_OR_PASSWORD_NOT_RIGHT.getCode(), ResultCodeEnum.USERNAME_OR_PASSWORD_NOT_RIGHT.getMsg());
......
...@@ -602,15 +602,12 @@ public class SysUserServiceImpl implements SysUserService { ...@@ -602,15 +602,12 @@ public class SysUserServiceImpl implements SysUserService {
if (sysUser == null) { if (sysUser == null) {
throw new UnknownAccountException(); throw new UnknownAccountException();
} }
/*String pwd = sysUser.getPassword(); //由于需要做单端登录限制用户所有校验放到controller中
if (pwd != null && !pwd.equals(encryptPassword(username, password))) { /* String userStatus = sysUser.getUserStatus();
throw new IncorrectCredentialsException();
}*/
String userStatus = sysUser.getUserStatus();
//账号被停用或注销 //账号被停用或注销
if (!com.glsx.common.constants.Constants.SYS_USER_STATUS_NORMAL.equals(userStatus)) { if (!com.glsx.common.constants.Constants.SYS_USER_STATUS_NORMAL.equals(userStatus)) {
throw new LockedAccountException(); throw new LockedAccountException();
} }*/
return sysUser; return sysUser;
} }
......
...@@ -41,20 +41,18 @@ public class UserRealm extends AuthorizingRealm{ ...@@ -41,20 +41,18 @@ public class UserRealm extends AuthorizingRealm{
protected AuthorizationInfo doGetAuthorizationInfo( protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) { PrincipalCollection principals) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
SysUser user = ShiroUtil.getUser(); /*SysUser user = ShiroUtil.getUser();
// 角色列表 // 角色列表
Set<String> roles = new HashSet<String>(); Set<String> roles = new HashSet<String>();
roles.add("运营");
// 功能列表 // 功能列表
Set<String> menus = new HashSet<String>(); Set<String> menus = new HashSet<String>();*/
menus.add("system:test:get");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
/*roles = roleService.selectRoleKeys(user.getUserId()); /*roles = roleService.selectRoleKeys(user.getUserId());
menus = menuService.selectPermsByUserId(user.getUserId());*/ menus = menuService.selectPermsByUserId(user.getUserId());*/
// 角色加入AuthorizationInfo认证对象 // 角色加入AuthorizationInfo认证对象
info.setRoles(roles); /*info.setRoles(roles);
// 权限加入AuthorizationInfo认证对象 // 权限加入AuthorizationInfo认证对象
info.setStringPermissions(menus); info.setStringPermissions(menus);*/
return info; return info;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!