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