栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JSP页面应如何检查身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JSP页面应如何检查身份验证

您可以尝试使用 过滤器

过滤器可以在请求到达servlet之前对其进行预处理,对离开servlet的响应进行后处理,或者两者都进行。筛选器可以拦截,检查和修改请求和响应。

注意: 确保在用户登录后添加会话属性,您可以在过滤器上使用该会话属性

在您的 login.jsp上 添加:

session.setAttribute("LOGIN_USER", user); //user entity if you have or user type of your user account... //if not set then LOGIN_USER will be null

web.xml

<filter>    <filter-name>SessionCheckFilter</filter-name>    <filter-class>yourjavapackage.SessionCheckFilter</filter-class></filter><filter-mapping>    <filter-name>SessionCheckFilter</filter-name>    <!--url-pattern>/app/*</url-pattern-->    <url-pattern>/main.jsp</url-pattern> <!-- url from where you implement the filtering --></filter-mapping>

SessionCheckFilter.java

public class SessionCheckFilter implements Filter {  private String contextPath;  @Override  public void init(FilterConfig fc) throws ServletException {    contextPath = fc.getServletContext().getContextPath();  }  @Override  public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {    HttpServletRequest req = (HttpServletRequest) request;    HttpServletResponse res = (HttpServletResponse) response;    if (req.getSession().getAttribute("LOGIN_USER") == null) { //checks if there's a LOGIN_USER set in session...        res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to redirect    } else {      String userType = (String) req.getSession().getAttribute("LOGIN_USER");      if (!userType.equals("ADMIN")){ //check if user type is not admin        res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to        }      fc.doFilter(request, response);    }  }  @Override  public void destroy() {  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573391.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号