2

我想在 Spring 3 MVC 中使用 HttpSession。我已经搜索了所有网络并在http://forum.springsource.org/showthread.php?98850-Adding-to-stuff-to-the-session-while-using-ResponseBody获得了这个解决方案

基本上,我的应用程序通过获取winId自动验证用户并通过 LDAP(它是一个 Intranet 站点)进行授权。

这是应用程序的流程:

  1. 用户输入应用程序 URL ( http://localhost:8082/eIA_Mock_5) 它有一个欢迎页面 (index.jsp)
  2. index.jsp 通过 jQuery 获取winId并点击 login.html(通过 AJAX)并传递 windowsId
  3. login.html(控制器)通过 LDAP 进行身份验证并返回“有效”字符串作为响应
  4. JavaScript,在获得正确的响应后,重定向/加载欢迎页面,即转到localhost:8082/eIA_Mock_5/welcome.html

现在,我有与之关联的过滤器,它检查会话是否对每个传入请求都有效。现在的问题是,即使我将数据设置到 HttpSession,但过滤器或任何其他控制器都无法通过会话获取数据,因此它没有进一步进行。

这是代码。你能建议实际上有什么问题吗?

Home_Controller.java

@Controller
public class Home_Controller {

    public static Log logger = LogFactory.getLog(Home_Controller.class);

    @RequestMapping(value = {"/welcome"})
    public ModelAndView loadWelcomePage(HttpServletRequest request, HttpServletResponse response)
    {
        ModelAndView mdv = new ModelAndView();
        try {
            /*HttpSession session = request.getSession();
            UserMasterBean userBean = (UserMasterBean)session.getAttribute("userBean");
            String userName = userBean.getWindowsId();
            if(userName == null || userName.equalsIgnoreCase(""))
            {   
                mdv.setViewName("homePage");

                System.out.println("Unable to authenticate user ");
                logger.debug("Unable to authenticate user ");
            }
            else
            {
                System.out.println("Welcome User "+userName);
                logger.debug("Welcome User "+userName);
                */
                mdv.setViewName("homePage");
            /*}*/

        }
        catch (Exception e){
            logger.debug("inside authenticateUser ",e);
            e.printStackTrace();
        }
        return mdv;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public @ResponseBody String authenticateUser(@RequestParam String userName, HttpSession session)
    {
        logger.debug("inside authenticateUser");

        String returnResponse = new String();
        try {
            logger.debug("userName for Authentication " + userName);
            System.out.println("userName for Authentication " + userName);

            //HttpSession session  =  request.getSession();

            if (userName == null || userName.trim().equalsIgnoreCase(""))
                returnResponse = "Invalid";
            else
            {
                System.out.println("uname " + userName);

                String ldapResponse = LDAPConnectUtil.isValidActiveDirectoryUser(userName, "");

                if (ldapResponse.equalsIgnoreCase("true"))
                {   
                    returnResponse="Valid";

                    System.out.println(userName + " Authenticated");
                    logger.debug(userName + " Authenticated");

                    UserMasterBean userBean = new UserMasterBean();
                    userBean.setWindowsId(userName);

                    //if(session.getAttribute("userBean")==null)
                    session.setAttribute("userBean", userBean);
                }
                else
                {   
                    returnResponse = "Invalid";

                    //session.setAttribute("userBean", null);

                    System.out.println("Unable to Authenticate the user through Ldap");
                    logger.debug("Unable to Authenticate the user through Ldap");
                }

                System.out.println("ldapResponse " + ldapResponse);
                logger.debug("ldapResponse " + ldapResponse);

                System.out.println("returnResponse " + returnResponse);
            }

            UserMasterBean u = (UserMasterBean)session.getAttribute("userBean");
            System.out.println("winId " + u.getWindowsId());
        }
        catch(Exception e){
            e.printStackTrace();
            logger.debug("Exception in authenticateUser ", e);
        }
        return returnResponse;
    }
}


过滤器

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
{
    System.out.println("in PageFilter");
    boolean flag = false;
    HttpServletRequest objHttpServletRequest = (HttpServletRequest)request;
    HttpServletResponse objHttpServletResponse = (HttpServletResponse)response;
    HttpSession session = objHttpServletRequest.getSession();
    String contextPath = objHttpServletRequest.getContextPath();

    String servletPath = objHttpServletRequest.getSession().getServletContext().getRealPath(objHttpServletRequest.getServletPath());

    logger.debug("contextPath :" + contextPath);
    logger.debug("servletPath :" + servletPath);
    System.out.println("in PageFilter, contextPath :" + contextPath);
    System.out.println("in PageFilter, servletPath :" + servletPath);

    if (servletPath.endsWith("\\") || servletPath.endsWith("/") || 
        servletPath.indexOf("css") > 0 || servletPath.indexOf("jsp") > 0 || 
        servletPath.indexOf("images") > 0 || servletPath.indexOf("js") > 0 || 
        servletPath.endsWith("index.jsp") || servletPath.indexOf("xls") > 0 || 
        servletPath.indexOf("ini") > 0 || servletPath.indexOf("login.html") > 0 || 
        /*servletPath.endsWith("welcome.html") ||*/ servletPath.endsWith("logout.do") )
    {
        System.out.println("User is trying to access allowed pages like Login.jsp, errorPage.jsp, js, images, css");
        logger.debug("User is trying to access allowed pages like Login.jsp, errorPage.jsp, js, images, css");
        flag = true;
    }

    if (flag == false)
    {
        System.out.println("flag = false");

        if (session.getAttribute("userBean") == null)
            System.out.println("yes session.userbean is null");

        if ((session != null) && (session.getAttribute("userBean") != null))
        {
            System.out.println("session!=null && session.getAttribute(userId)!=null");

            logger.debug("IF Part");

            UserMasterBean userBean = (UserMasterBean)session.getAttribute("userBean");
            String windowsId = userBean.getWindowsId();

            logger.debug("User Id " + windowsId + " allowed access");
            System.out.println("User Id " + windowsId + " allowed access");
            flag = true;
        }
        else
        {
            System.out.println("else .....session!=null && session.getAttribute(userId)!=null");
            logger.debug("Else Part");
            flag = false;
        }
    }

    if (flag == true) {
        try {
            System.out.println("before chain.doFilter(request, response)");
            chain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                objHttpServletResponse.sendRedirect(contextPath + "/logout.do");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    else
    {
        try {
            System.out.println("before sendRedirect");
            objHttpServletResponse.sendRedirect(contextPath + "/jsp/errorPage.jsp");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    System.out.println("end of PageFilter");
}


索引.jsp

<script type="text/javascript">
    //alert("inside s13");
    var WinNetwork = new ActiveXObject("WScript.Network");
    var userName = WinNetwork.UserName;
    alert(userName);

    $.ajax({  
        url: "login.html",  
        data: "userName="+userName,
        success: function(result) {
            alert("result == " + result);
            if (result == "Valid")
                window.location = "http://10.160.118.200:8082/eIA_Mock_5/welcome.html";
        }
    });
</script>


*
当我使用 Spring 3 MVC 时, web.xml有一个带有 URL 模式的过滤器条目。

4

1 回答 1

2

我认为 ajax 调用和之后设置 windows.location 的问题。

确保启用 cookie。如果你不这样做,你的 ajax 请求每次都会导致新的会话。

当您这样做window.location = url并且此 url 与您当前的 url 不同时,它也会导致新会话,因为 cookie 与域相关,并且您更改了域,例如从localhostto 10.160.118.200

对于每个请求输出 sessionid 并将其与之前的请求进行比较。它有助于查找重新创建会话的时间。

这个答案也可以提供帮助。

于 2012-10-11T14:36:38.420 回答