打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
防止未登录的用户直接重写URL访问系统(java)
最近在开发系统的时候,碰到个问题,就是未登录的用户可以通过重写url登录他本不能登录的系统。
经过研究,终于把此问题解决了。呵~
思路是:每当有用户成功登录系统时,把其信息保存到session中。在相应被访问的页面,其对应的bean中的构造函数来获得session 中用户对象,若用户对象为空,则表明此用户是未登录的,使其跳转到登录页面。
 
   session 取得代码
 
  public class SessionHelper
  {
    private static final String OA_WEB_SESSION="OAWebSession";
    private static HttpSession getSession()
    {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        return request.getSession();
    }
    public static OAWebSession getOAWebSession()
    {
        Object session = getSession().getAttribute(OA_WEB_SESSION);
        if (session == null)
        {
            session = new OAWebSession();
            getSession().setAttribute(OA_WEB_SESSION, session);
        }
        return (OAWebSession) session;
    }
    public static void removeOAWebSession()
    {
        getSession().removeAttribute(OA_WEB_SESSION);
    }
}
 
  在用户成功登录后把其信息储存到session中
            User user = new User();
            user.login(userID, password);
            SessionHelper.getOAWebSession().setUser(user);
 
   被访问的页面对应的bean
 
   public class Top{
     public Top(){
              user = SessionHelper.getOAWebSession().getUser();
             if (user == null) {
              NavigateHelper.redirect("Logon.jsp");
        }
    }
  }
 
  redirect 方法如下:
   public class NavigateHelper
{
    public static void redirect(String url)
    {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        try
        {
            response.sendRedirect(url);
        }
        catch (IOException ex)
        {
            ExceptionHelper.jumpToErrorPageWithResponseComplete(ex, SessionHelper.getOAWebSession());
        }
    }
}
 
  注明: 由于jsf 是先走构造函数,才走get方法,所以get方法要加个对象或值取得为空的判断。
  注明: 此块功能只对未登录的用户有效,登录了但没权挟的在研究中。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
.NET MVC中登录过滤器拦截的两种方法
Cookie和Session专题
JSF问题集锦
request.getSession()、request.getSession(true)和request.getSession(false)的区别
根据sessionId获取Session对象
jsp/servlet 深入解析session本质
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服