com.quincy.auth.service.impl.AuthorizationSessionServiceImpl Maven / Gradle / Ivy
package com.quincy.auth.service.impl;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import com.quincy.auth.AuthConstants;
import com.quincy.auth.o.DSession;
import com.quincy.auth.o.User;
import com.quincy.auth.service.AuthCallback;
import com.quincy.sdk.Constants;
@Service("authorizationSessionServiceImpl")
public class AuthorizationSessionServiceImpl extends AuthorizationAbstract {
@Override
protected Object getUserObject(HttpServletRequest request) {
HttpSession session = request.getSession(false);
return session==null?null:session.getAttribute(Constants.ATTR_SESSION);
}
@Override
public DSession setSession(HttpServletRequest request, String originalJsessionid, Long userId, AuthCallback callback) {
if(originalJsessionid!=null&&originalJsessionid.length()>0) {//同一user不同客户端登录互踢
HttpSession httpSession = AuthConstants.SESSIONS.get(originalJsessionid);
if(httpSession!=null) {
DSession session = (DSession)httpSession.getAttribute(Constants.ATTR_SESSION);
if(session.getUser().getId().equals(userId))
httpSession.invalidate();
}
}
HttpSession httpSession = request.getSession();
String jsessionid = httpSession.getId();
User user = callback.getUser();
user.setJsessionid(jsessionid);
DSession session = this.createSession(user);
httpSession.setAttribute(Constants.ATTR_SESSION, session);
callback.updateLastLogined(jsessionid);
return session;
}
public void logout(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if(session!=null) {
session.invalidate();
}
}
@Override
protected void saveVcode(HttpServletRequest request, String vcode) {
request.getSession(true).setAttribute(Constants.ATTR_VCODE, vcode);
}
@Override
public String getCachedVcode(HttpServletRequest request) {
return this.getCachedStr(request, Constants.ATTR_VCODE);
}
private String getCachedStr(HttpServletRequest request, String attrName) {
HttpSession session = request.getSession();
if(session!=null) {
Object obj = session.getAttribute(attrName);
return obj==null?null:obj.toString();
}
return null;
}
@Override
public void updateSession(User user) {
HttpSession httpSession = AuthConstants.SESSIONS.get(user.getJsessionid());
DSession dSession = this.createSession(user);
httpSession.setAttribute(Constants.ATTR_SESSION, dSession);
}
@Override
public void updateSession(List users) throws IOException {
for(User user:users) {
this.updateSession(user);
}
}
}