Commit 1dad4c07 authored by zhouqian's avatar zhouqian

Merge branch 'master' of http://gitabc.xyqb.com/head_group/xyqb-user2

# Conflicts:
#	src/main/java/cn/quantgroup/xyqb/Constants.java
parent 455b3cb6
...@@ -46,7 +46,7 @@ public interface Constants { ...@@ -46,7 +46,7 @@ public interface Constants {
} }
interface Session{ interface Session{
String USERS_ESSIONID_CACHE = "userid-sessionvalue:cache:"; String USER_SESSIONID_CACHE = "user-sessionvalue:cache:";
Long ONE_DAY = 24 * 60 * 60L; Long ONE_DAY = 24 * 60 * 60L;
} }
......
...@@ -6,6 +6,8 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -6,6 +6,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisNode;
...@@ -112,6 +114,8 @@ public class RedisConfig { ...@@ -112,6 +114,8 @@ public class RedisConfig {
template.setValueSerializer(jdkSerializationRedisSerializer); template.setValueSerializer(jdkSerializationRedisSerializer);
template.setDefaultSerializer(jdkSerializationRedisSerializer); template.setDefaultSerializer(jdkSerializationRedisSerializer);
template.setConnectionFactory(factory); template.setConnectionFactory(factory);
XyqbSessionContextHolder.redisTemplate = stringRedisTemplate(factory);
return template; return template;
} }
...@@ -127,7 +131,6 @@ public class RedisConfig { ...@@ -127,7 +131,6 @@ public class RedisConfig {
template.setValueSerializer(jdkSerializationRedisSerializer); template.setValueSerializer(jdkSerializationRedisSerializer);
template.setDefaultSerializer(jdkSerializationRedisSerializer); template.setDefaultSerializer(jdkSerializationRedisSerializer);
template.setConnectionFactory(factory); template.setConnectionFactory(factory);
XyqbSessionContextHolder.redisTemplate = template;
return template; return template;
} }
......
package cn.quantgroup.xyqb.controller; package cn.quantgroup.xyqb.controller;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.entity.User; import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.model.session.SessionStruct; import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.util.ApplicationContextHolder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import static cn.quantgroup.xyqb.session.XyqbSessionContextHolder.*;
/** /**
* Created by Miraculous on 15/7/5. * Created by Miraculous on 15/7/5.
...@@ -17,25 +15,17 @@ import javax.servlet.http.HttpSession; ...@@ -17,25 +15,17 @@ import javax.servlet.http.HttpSession;
public interface IBaseController { public interface IBaseController {
default User getCurrentUser() { default User getCurrentUser() {
return null; SessionStruct session = getXSession();
if (session == null) {
return null;
}
return getXSession().getValues().getUser();
} }
default SessionStruct getSessionStruct() { default SessionStruct getSessionStruct() {
String token = StringUtils.defaultString(getRequest().getHeader("x-auth-token"), ""); return getXSession();
ApplicationContextHolder.getBean("stringRedisTemplate");
} }
default HttpSession getSession() {
HttpSession session = null;
try {
HttpServletRequest request;
request.getRequestedSessionId()
session = getRequest().getSession();
} catch (Exception e) {
e.getStackTrace();
}
return session;
}
default Long getChannelId() { default Long getChannelId() {
return getNumber("channelId"); return getNumber("channelId");
...@@ -46,10 +36,9 @@ public interface IBaseController { ...@@ -46,10 +36,9 @@ public interface IBaseController {
} }
default Long getNumber(String name) { default Long getNumber(String name) {
HttpSession session = getSession(); SessionStruct session = getSessionStruct();
try { try {
Long number = Long.valueOf(session.getAttribute(name).toString()); return Long.valueOf(session.getAttribute(name));
return number;
} catch (Exception e) { } catch (Exception e) {
e.getStackTrace(); e.getStackTrace();
} }
...@@ -57,25 +46,25 @@ public interface IBaseController { ...@@ -57,25 +46,25 @@ public interface IBaseController {
} }
default void setChannelId(Long channelId) { default void setChannelId(Long channelId) {
HttpSession session = getSession(); SessionStruct session = getSessionStruct();
if (session != null) { if (session != null) {
session.setAttribute("channleId", channelId); session.setAttribute("channleId", channelId.toString());
} }
} }
default String getAppChannel() { default String getAppChannel() {
HttpSession session = getSession(); SessionStruct session = getSessionStruct();
Object appChannel = session.getAttribute("appChannel"); String appChannel = session.getAttribute("appChannel");
if(appChannel == null){ if(StringUtils.isEmpty(appChannel)){
return null; return null;
} }
return appChannel.toString(); return appChannel;
} }
default HttpServletRequest getRequest() { default HttpServletRequest getRequest() {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes().setAttribute(); .getRequestAttributes();
return attrs.getRequest(); return attrs.getRequest();
} }
...@@ -89,7 +78,7 @@ public interface IBaseController { ...@@ -89,7 +78,7 @@ public interface IBaseController {
//过滤反向代理的ip //过滤反向代理的ip
String[] stemps = ip.split(","); String[] stemps = ip.split(",");
if (stemps != null && stemps.length >= 1) { if (stemps.length >= 1) {
//得到第一个IP,即客户端真实IP //得到第一个IP,即客户端真实IP
ip = stemps[0]; ip = stemps[0];
} }
......
...@@ -110,6 +110,11 @@ public class UserController implements IBaseController { ...@@ -110,6 +110,11 @@ public class UserController implements IBaseController {
return new JsonResult(authBean); return new JsonResult(authBean);
} }
@RequestMapping("/test")
public JsonResult test() {
return JsonResult.buildSuccessResult("", getCurrentUser());
}
private boolean validatePassword(String paramPass, String targetPassword){ private boolean validatePassword(String paramPass, String targetPassword){
return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt)); return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt));
} }
......
...@@ -2,8 +2,12 @@ package cn.quantgroup.xyqb.model.session; ...@@ -2,8 +2,12 @@ package cn.quantgroup.xyqb.model.session;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.apache.commons.collections.map.HashedMap;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by 11 on 2016/12/28. * Created by 11 on 2016/12/28.
*/ */
...@@ -14,4 +18,23 @@ public class SessionStruct implements Serializable { ...@@ -14,4 +18,23 @@ public class SessionStruct implements Serializable {
private String sid; private String sid;
private SessionValue values; private SessionValue values;
public void setAttribute(String key, String value) {
if (value == null) {
value = "";
}
Map<String, String> t = values.getValues();
if (t == null) {
t = new HashMap<>();
}
t.put(key, value);
}
public String getAttribute(String key) {
Map<String, String> t = values.getValues();
if (t == null) {
t = new HashMap<>();
}
return t.containsKey(key) ? t.get(key) : "";
}
} }
...@@ -16,7 +16,7 @@ import java.util.Map; ...@@ -16,7 +16,7 @@ import java.util.Map;
@NoArgsConstructor @NoArgsConstructor
public class SessionValue{ public class SessionValue{
private User user; private User user;
private Map<String, Object> values; private Map<String, String> values;
@JSONField(serializeUsing = Timestamp2LongConverter.class) @JSONField(serializeUsing = Timestamp2LongConverter.class)
private Timestamp createdAt; private Timestamp createdAt;
......
...@@ -50,12 +50,12 @@ public class SessionServiceImpl implements ISessionService{ ...@@ -50,12 +50,12 @@ public class SessionServiceImpl implements ISessionService{
@Override @Override
public String findSessionIdByUserId(Long userId) { public String findSessionIdByUserId(Long userId) {
return stringRedisTemplate.opsForValue().get(Constants.Session.USERS_ESSIONID_CACHE + userId.toString()); return stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + userId.toString());
} }
@Override @Override
public String findSessionValueBySessionId(String sessionId){ public String findSessionValueBySessionId(String sessionId){
String result = stringRedisTemplate.opsForValue().get(Constants.Session.USERS_ESSIONID_CACHE + sessionId); String result = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + sessionId);
return StringUtils.defaultString(result, ""); return StringUtils.defaultString(result, "");
} }
...@@ -68,7 +68,7 @@ public class SessionServiceImpl implements ISessionService{ ...@@ -68,7 +68,7 @@ public class SessionServiceImpl implements ISessionService{
sessionValue.setCreatedAt(now); sessionValue.setCreatedAt(now);
sessionValue.setLastAccessTime(now); sessionValue.setLastAccessTime(now);
sessionValue.setUser(user); sessionValue.setUser(user);
Map<String, Object> values = new HashMap(); Map<String, String> values = new HashMap<>();
sessionValue.setValues(values); sessionValue.setValues(values);
sessionStruct.setValues(sessionValue); sessionStruct.setValues(sessionValue);
return sessionStruct; return sessionStruct;
...@@ -79,6 +79,6 @@ public class SessionServiceImpl implements ISessionService{ ...@@ -79,6 +79,6 @@ public class SessionServiceImpl implements ISessionService{
Timestamp current = new Timestamp(System.currentTimeMillis()); Timestamp current = new Timestamp(System.currentTimeMillis());
sessionValue.setLastAccessTime(current); sessionValue.setLastAccessTime(current);
String json = JSON.toJSONString(sessionValue); String json = JSON.toJSONString(sessionValue);
stringRedisTemplate.opsForValue().set(Constants.Session.USERS_ESSIONID_CACHE + token, json, Constants.Session.ONE_DAY); stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSIONID_CACHE + token, json, Constants.Session.ONE_DAY);
} }
} }
package cn.quantgroup.xyqb.session; package cn.quantgroup.xyqb.session;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.model.session.SessionStruct; import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.util.ApplicationContextHolder; import cn.quantgroup.xyqb.model.session.SessionValue;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
...@@ -15,9 +19,10 @@ import javax.servlet.http.HttpServletRequest; ...@@ -15,9 +19,10 @@ import javax.servlet.http.HttpServletRequest;
public class XyqbSessionContextHolder { public class XyqbSessionContextHolder {
private static final ThreadLocal<SessionStruct> threadSession = new ThreadLocal<>(); private static final ThreadLocal<SessionStruct> threadSession = new ThreadLocal<>();
public static volatile RedisTemplate<String, String> redisTemplate = null; public static RedisTemplate<String, String> redisTemplate = null;
private static final Logger LOGGER = LoggerFactory.getLogger(XyqbSessionContextHolder.class);
public static SessionStruct getSession() { public static SessionStruct getXSession() {
if (threadSession.get() != null) { if (threadSession.get() != null) {
return threadSession.get(); return threadSession.get();
} }
...@@ -26,7 +31,25 @@ public class XyqbSessionContextHolder { ...@@ -26,7 +31,25 @@ public class XyqbSessionContextHolder {
if (token == null || token.length() != 36) { if (token == null || token.length() != 36) {
return null; return null;
} }
redisTemplate.opsForValue().get(Constants.Session) String result = redisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + token);
if (StringUtils.isEmpty(result)) {
return null;
}
try {
SessionValue values = JSON.parseObject(result, SessionValue.class);
if (values == null) {
return null;
}
SessionStruct sessionStruct = new SessionStruct();
sessionStruct.setSid(token);
sessionStruct.setValues(values);
threadSession.set(sessionStruct);
return sessionStruct;
} catch (Exception ex) {
LOGGER.error("序列化session出错", ex);
return 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