whycxzp
2022-06-16 d812eeae7291701dc1c9dda7bcfed89df9f336b8
登录检查bug修复
5个文件已修改
166 ■■■■ 已修改文件
src/main/java/com/whyc/config/ShiroConfig.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/config/WebSocketConfig.java 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/filter/KickedOutFilter.java 77 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/LoginService.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/LoginCheckSocket.java 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/config/ShiroConfig.java
@@ -1,7 +1,6 @@
package com.whyc.config;
import com.whyc.constant.YamlProperties;
import com.whyc.filter.KickedOutFilter;
import com.whyc.filter.RolesOrAuthorizationFilter;
import com.whyc.properties.PropertiesUtil;
import com.whyc.realm.CustomRealm;
src/main/java/com/whyc/config/WebSocketConfig.java
@@ -19,7 +19,9 @@
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        //HttpSession httpSession = (ShiroHttpSession) request.getHttpSession();
        HttpSession httpSession = (HttpSession)request.getHttpSession();
        sec.getUserProperties().put("httpSession",httpSession);
        if(httpSession!=null) {
            sec.getUserProperties().put("httpSession", httpSession);
        }
        super.modifyHandshake(sec, request, response);
    }
src/main/java/com/whyc/filter/KickedOutFilter.java
@@ -1,38 +1,39 @@
package com.whyc.filter;
import com.whyc.pojo.UserInf;
import com.whyc.util.ShiroUtil;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * 账号同时登录只能1个
 */
@WebFilter(urlPatterns = "/*",filterName = "kickedOutFilter")
public class KickedOutFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest) request).getSession();
        //如果是登录,则不进行Session对比,放行
        if(!((HttpServletRequest) request).getRequestURI().contains("login")) {
            UserInf user = ShiroUtil.getUser();
            if (user.getUId() != 0) {
                //System.out.println("Filter过滤器中获取到的当前Session的SessionId为:" + session.getId());
                if (!request.getServletContext().getAttribute(user.getUName()).equals(session.getId())) {
                    //如果当前Session所对应的SessionId与全局中用户对应的SessionId不一致,则清除当前Session
                    session.invalidate();
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script language='javascript' type='text/javascript'>alert('账号已经在其他地方登录,请重新登录');self.location='login.html'</script> ");
                    return;
                }
            }
        }
        chain.doFilter(request,response);
    }
}
//package com.whyc.filter;
//
//import com.whyc.pojo.UserInf;
//import com.whyc.util.ShiroUtil;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//
///**
// * 账号同时登录只能1个
// */
//@WebFilter(urlPatterns = "/*",filterName = "kickedOutFilter")
//public class KickedOutFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpSession session = ((HttpServletRequest) request).getSession();
//        //如果是登录,则不进行Session对比,放行
//        String requestURI = ((HttpServletRequest) request).getRequestURI();
//        if(!requestURI.contains("login")) {
//            UserInf user = ShiroUtil.getUser();
//            if (user.getUId() != 0) {
//                //System.out.println("Filter过滤器中获取到的当前Session的SessionId为:" + session.getId());
//                if (!request.getServletContext().getAttribute(user.getUName()).equals(session.getId())) {
//                    //如果当前Session所对应的SessionId与全局中用户对应的SessionId不一致,则清除当前Session
//                    session.invalidate();
//                    response.setContentType("text/html;charset=utf-8");
//                    response.getWriter().write("<script language='javascript' type='text/javascript'>alert('账号已经在其他地方登录,请重新登录');self.location='login.html'</script> ");
//                    return;
//                }
//            }
//        }
//
//        chain.doFilter(request,response);
//    }
//}
src/main/java/com/whyc/service/LoginService.java
@@ -512,16 +512,12 @@
                    httpSession.invalidate();
                }
            }
//            else {
//                model.set(1,false,"请先登录!");
//            }
            else {
                model.set(1,false,"用户信息失效,请重新登录");
            }
        }catch (IllegalStateException e){
            e.printStackTrace();
            model.set(1,false,"登录信息过期!");
        }catch (Exception e){
            e.printStackTrace();
            model.set(1,false,"登录信息过期!");
            model.set(1,false,"登录信息失效,重新登录");
        }
        return model;
    }
src/main/java/com/whyc/webSocket/LoginCheckSocket.java
@@ -34,45 +34,59 @@
    public void onOpen(Session session, EndpointConfig config){
        this.session = session;
        HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Map<String, Response> res=new HashMap<>();
                    Response sessionRes = new Response().set(1,httpSession.getId());
                    while (!currentThread().isInterrupted()) {
                        Response response = loginService.checkUserWebSocket(httpSession);
                        res.put("checkLogin",response);
                        res.put("session",sessionRes);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        sleep(4000);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EncodeException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }
        if(httpSession == null){
            Map<String, Response> res = new HashMap<>();
            Response resp1 = new Response().set(1, false, "登录信息失效,重新登录");
            res.put("checkLogin", resp1);
            try {
                session.getBasicRemote().sendObject(new Response().set(1,res));
            } catch (IOException | EncodeException e) {
                e.printStackTrace();
            }
        };
        thread.start();
        this.thread = thread;
        }else {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        Map<String, Response> res = new HashMap<>();
                        Response sessionRes = new Response().set(1, httpSession.getId());
                        while (!currentThread().isInterrupted()) {
                            Response response = loginService.checkUserWebSocket(httpSession);
                            res.put("checkLogin", response);
                            res.put("session", sessionRes);
                            session.getBasicRemote().sendObject(new Response().set(1, res));
                            if (response.getData() instanceof Boolean) {
                                if(response.getData().equals(false)) {
                                    this.interrupt();
                                }
                            }
                            sleep(4000);
                        }
                    } catch (Exception e) {
                        this.interrupt();
                    }
                }
            };
            thread.start();
            this.thread = thread;
        }
    }
    @OnClose
    public void onClose(CloseReason closeReason){
    public void onClose(CloseReason closeReason) throws IOException {
        System.err.println("closeReason = " + closeReason);
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        }
    }
    @OnError
    public void onError(Throwable error){
    public void onError(Throwable error) throws IOException {
        error.printStackTrace();
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        }
    }
}