Commit f51a83fb by qwmqiuwenmin

模拟登录

parent 9c10fc03
package com.gic.haoban.manage.api.dto;
import java.io.Serializable;
import java.util.Date;
public class StaffDTO implements Serializable{
private String staffId;
private String wxUserId;
private String phoneNumber;
private String staffName;
private String nationCode;
private String nickName;
private Integer sex;
private String postion;
private Integer activeFlag;
private String extendPostion;
private Integer statusFlag;
private Date createTime;
private Date updateTime;
private static final long serialVersionUID = 1L;
public String getStaffId() {
return staffId;
}
public void setStaffId(String staffId) {
this.staffId = staffId == null ? null : staffId.trim();
}
public String getWxUserId() {
return wxUserId;
}
public void setWxUserId(String wxUserId) {
this.wxUserId = wxUserId == null ? null : wxUserId.trim();
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber == null ? null : phoneNumber.trim();
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName == null ? null : staffName.trim();
}
public String getNationCode() {
return nationCode;
}
public void setNationCode(String nationCode) {
this.nationCode = nationCode == null ? null : nationCode.trim();
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName == null ? null : nickName.trim();
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getPostion() {
return postion;
}
public void setPostion(String postion) {
this.postion = postion == null ? null : postion.trim();
}
public Integer getActiveFlag() {
return activeFlag;
}
public void setActiveFlag(Integer activeFlag) {
this.activeFlag = activeFlag;
}
}
package com.gic.haoban.manage.api.service; package com.gic.haoban.manage.api.service;
import com.gic.haoban.manage.api.dto.StaffDTO;
public interface StaffApiService { public interface StaffApiService {
public StaffDTO selectById(String staffId);
} }
package com.gic.haoban.manage.service.service; package com.gic.haoban.manage.service.service;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
public interface StaffService { public interface StaffService {
TabHaobanStaff selectById(String staffId);
} }
package com.gic.haoban.manage.service.service.impl; package com.gic.haoban.manage.service.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.service.StaffApiService; import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.service.StaffService;
@Service @Service
public class StaffApiServiceImpl implements StaffApiService { public class StaffApiServiceImpl implements StaffApiService {
@Autowired
StaffService staffService;
@Override
public StaffDTO selectById(String staffId) {
TabHaobanStaff staff = staffService.selectById(staffId);
return EntityUtil.changeEntityByJSON(StaffDTO.class, staff);
}
} }
package com.gic.haoban.manage.service.service.out.impl; package com.gic.haoban.manage.service.service.out.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.gic.haoban.manage.service.dao.mapper.StaffMapper;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.service.StaffService; import com.gic.haoban.manage.service.service.StaffService;
@Service @Service
public class StaffServiceImpl implements StaffService { public class StaffServiceImpl implements StaffService {
@Autowired
StaffMapper mapper;
@Override
public TabHaobanStaff selectById(String id) {
return mapper.selectByPrimaryKey(id);
}
} }
package com.gic.haoban.manage.web.auth;
import com.gic.redis.data.util.RedisUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Created 2018/7/26.
*
* @author hua
*/
public class AuthRequestUtil {
private static final Logger logger = LoggerFactory.getLogger(AuthRequestUtil.class);
public static final String REDIS_LOGIN_USER_TOKEN_PREFIX = "GIC:HAOBAN:HAOBAN_MOBILE_APP:LOGIN_USER:";
public static final String REDIS_FAKE_LOGIN_FLAG_PREFIX = "GIC:HAOBAN:HAOBAN_MOBILE_APP:FAKE_LOGIN_TOKEN:";
public static Object getSessionUser() {
return getSession().getAttribute(GlobalParams.LOGIN_SESSION_KEY);
}
public static void setSessionUser(Object obj) {
getSession().setAttribute(GlobalParams.LOGIN_SESSION_KEY, obj);
}
public static void delSessionUser() {
getSession().removeAttribute(GlobalParams.LOGIN_SESSION_KEY);
}
public static Object getAppLoginUser() {
String token = getRequest().getHeader("token");
logger.info("token1111111111=" + token);
if (StringUtils.isBlank(token)) {
return null;
}
Object cache = RedisUtil.getCache(token);
return cache;
}
private static void setLoginUserLastToken(String token, Object obj) {
try {
String userId = BeanUtils.getProperty(obj, "user.userId");
if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(token)) {
RedisUtil.setCache(REDIS_LOGIN_USER_TOKEN_PREFIX + userId, token, 31L, TimeUnit.DAYS);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
/**
* 是否为用户最后一次登录token
*
* @param token
* @param userId
* @return
*/
public static boolean isLoginUserLastToken(String token, String userId) {
Object lastUserLoginToken = RedisUtil.getCache(REDIS_LOGIN_USER_TOKEN_PREFIX + userId);
// 如果用户最后一次登录token不符则返回false
return !(lastUserLoginToken instanceof CharSequence) || StringUtils.equals(token, (CharSequence) lastUserLoginToken);
}
private static void setFakeLoginFlag(String token) {
if (StringUtils.isNotBlank(token)) {
RedisUtil.setCache(REDIS_FAKE_LOGIN_FLAG_PREFIX + token, new Date(), 30L, TimeUnit.DAYS);
}
}
/**
* 是否为伪登录token
*
* @param token
* @return
*/
public static boolean isFakeLoginToken(String token) {
return StringUtils.isNotBlank(token) && RedisUtil.getCache(REDIS_FAKE_LOGIN_FLAG_PREFIX + token) != null;
}
public static void setAppLoginUser(String token, Object obj) {
RedisUtil.setCache(token, obj, 30L, TimeUnit.DAYS);
getResponse().setHeader("token", token);
setLoginUserLastToken(token, obj);
}
public static void setAppFakeLoginUser(String token, Object obj) {
RedisUtil.setCache(token, obj, 30L, TimeUnit.DAYS);
getResponse().setHeader("token", token);
setFakeLoginFlag(token);
}
public static void delToken() {
String token = getRequest().getHeader("token");
RedisUtil.delCache(token);
}
public static void setAppLoginUser(Object obj) {
String token = getRequest().getHeader("token");
RedisUtil.setCache(token, obj, 30L, TimeUnit.DAYS);
getResponse().setHeader("token", token);
setLoginUserLastToken(token, obj);
}
public static void setAppFakeLoginUser(Object obj) {
String token = getRequest().getHeader("token");
RedisUtil.setCache(token, obj, 30L, TimeUnit.DAYS);
getResponse().setHeader("token", token);
}
public static HttpSession getSession() {
HttpSession session = null;
try {
session = getRequest().getSession();
} catch (Exception e) {
}
return session;
}
public static HttpServletRequest getRequest() {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
return attrs.getRequest();
}
public static HttpServletResponse getResponse() {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
return attrs.getResponse();
}
}
package com.gic.haoban.manage.web.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.gic.haoban.common.utils.HaobanResponse;
@RestController
public class ApplicationController extends WebBaseController{
@RequestMapping("application-list")
public HaobanResponse applicationList() {
return null;
}
}
package com.gic.haoban.manage.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gic.haoban.common.utils.AuthRequestUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
@RestController
public class LoginController extends WebBaseController{
@Autowired
StaffApiService staffApiService;
@RequestMapping("login")
public HaobanResponse login() {
StaffDTO staff = staffApiService.selectById("1");
AuthRequestUtil.setSessionUser(staff);
return resultResponse(HaoBanErrCode.ERR_1, staff);
}
@RequestMapping("login-out")
public HaobanResponse loginOut() {
AuthRequestUtil.delSessionUser();
return resultResponse(HaoBanErrCode.ERR_1);
}
}
...@@ -19,5 +19,5 @@ ...@@ -19,5 +19,5 @@
<!--<dubbo:registry address="zookeeper://115.159.182.172:2199" protocol="dubbo" id="remoteAdd"/>--> <!--<dubbo:registry address="zookeeper://115.159.182.172:2199" protocol="dubbo" id="remoteAdd"/>-->
<!--<dubbo:registry address="zookeeper://localhost:2181|zookeeper://115.159.182.172:2199" protocol="dubbo"/>--> <!--<dubbo:registry address="zookeeper://localhost:2181|zookeeper://115.159.182.172:2199" protocol="dubbo"/>-->
<dubbo:reference interface="com.gic.haoban.manage.api.service.StaffApidService" id="staffApiService"/>
</beans> </beans>
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