有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我需要在Spring中检查每个控制器中的有效会话吗?

假设在Spring Mvc的web应用程序中,我们需要检查每个控制器或JSP中的有效会话吗?如何在MVC中解决会话管理问题?我们基本上做什么? 还有哪些东西可以为我的应用程序增加额外的安全性


共 (4) 个答案

  1. # 1 楼答案

    不,您不需要在Spring中检查每个控制器中的有效会话

    Spring框架使会话管理变得非常简单,因为您可以在单个xml文件中配置Spring安全性,Spring将为您管理会话

    Check out these examples on spring

  2. # 2 楼答案

    是否要检查每个控制器中的每个会话的授权或身份验证?可以使用aspectj

  3. # 4 楼答案

    我们通常在过滤层中检查会话是否过期,并将其映射到DispatcherServlet,这样,所有将由spring处理的传入请求都将首先被过滤,因此,如果会话已经过期,则不允许与spring控制器进行任何交互。如果发现会话已过期,请将重定向发送到一个页面,该页面将通知用户其会话已过期

    样本过滤器代码

    public class MyFilter implements Filter{
        ...
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            if (isSessionExpired((HttpServletRequest) theRequest)) {
                response.sendRedirect(((HttpServletRequest) theRequest).getContextPath() + "/expired.jsp");
                response.flushBuffer();
            }else{
                //..its not yet expired, continue
                theChain.doFilter(theRequest, theResp);
            }
        }
        ...
    }
    

    映射到web中的DispatcherServlet。xml

        <filter>
            <filter-name>MyFilter</filter-name>
            <filter-class>com.mycompany.ourproject.filter.MyFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>MyFilter</filter-name>
            <servlet-name>springdispatcher</servlet-name>
        </filter-mapping>
    
        <servlet>
            <servlet-name>springdispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>