1.user控制类
package com.test.bean;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2016/8/24.
*/
public class UserMap {
private static Map<String, String> userMap = new HashMap<String, String>();
public static synchronized void add(String username, String sessionId) {
userMap.put(username, sessionId);
}
public static synchronized boolean isLastLogin(String user, String sessionId) {
return userMap.get(user).equals(sessionId);
}
}
2. web.xml
<filter>
<filter-name>SignLoginFilter</filter-name>
<filter-class>com.test.servlet.SignLoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SignLoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginActionLoginServlet</servlet-name>
<servlet-class>com.test.servlet.LoginActionLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginActionLoginServlet</servlet-name>
<url-pattern>/websso/loginAction_login.json</url-pattern>
</servlet-mapping>
3.LoginActionLoginServlet
package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.test.bean.UserMap;
import com.test.util.JdbcUtils;
import com.test.util.JsonUtils;
import com.test.util.SHAUtils;
public class LoginActionLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static int TIME_OUT=3600;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/x-www-form-urlencoded; charset=UTF-8;application/json;");
HttpSession session = req.getSession();
String memberAccount = req.getParameter("memberAccount");
String memberUmm = req.getParameter("memberUmm");
String check = req.getParameter("check");
String pwd = JdbcUtils.getPwd(memberAccount);
String memberUmmFromDB = SHAUtils.sha1(pwd);
String firstCard = JdbcUtils.getFirstCard(memberAccount);
session.setAttribute("memberAccount", memberAccount);
session.setAttribute("currentCard", firstCard);
UserMap.add(memberAccount, session.getId());
HashMap<String, String> map = new HashMap<String, String>();
map.put("location",
"http://www.sinopecsales.com/gas/ssoLoginRedirect.action");
if (memberUmmFromDB.equals(memberUmm)&& check.equals("45")) {
map.put("success", "0");
} else {
map.put("success", "1");
}
String json = JsonUtils.mapToJson(map);
PrintWriter out = null;
try {
out = resp.getWriter();
out.write(json);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
4.SignLoginFilter
package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.test.bean.UserMap;
public class SignLoginFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain fChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/x-www-form-urlencoded; charset=UTF-8;application/json;");
String url = request.getRequestURL().toString();
if (url.contains("/websso/YanZhengMaServlet")
|| url.contains("/websso/loginAction_login.json")) {
fChain.doFilter(req, resp);
return;
}
HttpSession session = request.getSession();
if (session.getAttribute("memberAccount") == null) {
PrintWriter out = null;
try {
out = resp.getWriter();
out.write("请登录");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
return;
}
String sessionId = session.getId();
Object userNameObject = session.getAttribute("memberAccount");
if (userNameObject != null) {
String userName = userNameObject.toString();
if (!UserMap.isLastLogin(userName, sessionId)) {
PrintWriter out = null;
try {
out = resp.getWriter();
out.write("请重新登录");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
return;
}
}
fChain.doFilter(req, resp);
}
@Override
public void init(FilterConfig fConfig) throws ServletException {
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}