Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSP页面应该如何检查身份验证_Javascript_Jsp_Authentication_Web Applications_Login - Fatal编程技术网

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

Javascript JSP页面应该如何检查身份验证,javascript,jsp,authentication,web-applications,login,Javascript,Jsp,Authentication,Web Applications,Login,我是网络编程新手。我要求使用一种通用模式来执行诸如检查身份验证之类的操作。以下是场景: 该网站有一个供访问者登录的页面。它将获取用户名和加密密码并将其发送到服务器,然后从服务器获取错误代码(用户名/密码不匹配)或身份验证密钥。当用户成功登录时,我希望网站自动跳转到显示网站主要功能的main.jsp页面 在本例中,我希望main.jsp检查用户身份验证。也就是说,我不希望发生这样的事情,比如用户可以直接打开www.example.com/main.jsp,如果他们这样做,我希望将他们重定向到登录页

我是网络编程新手。我要求使用一种通用模式来执行诸如检查身份验证之类的操作。以下是场景:

该网站有一个供访问者登录的页面。它将获取用户名和加密密码并将其发送到服务器,然后从服务器获取错误代码(用户名/密码不匹配)或身份验证密钥。当用户成功登录时,我希望网站自动跳转到显示网站主要功能的
main.jsp
页面

在本例中,我希望
main.jsp
检查用户身份验证。也就是说,我不希望发生这样的事情,比如用户可以直接打开
www.example.com/main.jsp
,如果他们这样做,我希望将他们重定向到登录页面

那个么,如何在页面上传递身份验证信息,如何防止用户不登录就直接访问
main.jsp
?我需要使用会话或其他什么吗

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


不应该。您应该使用容器管理的身份验证,并通过URL模式在web.xml中定义登录/安全性


由Glen Best添加:

例如,在web.xml中添加如下内容:

<security-constraint>
   <display-name>GET: Employees Only</display-name>
   <web-resource-collection>
      <web-resource-name>Restricted Get</web-resource-name>
      <url-pattern>/restricted/employee/*</url-pattern>
      <http-method>GET</http-method>
   </web-resource-collection>
   <auth-constraint>
      <role-name>Employee</role-name>
   </auth-constraint>
   <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
   </user-data-constraint>
</security-constraint>

获取:仅限员工
受限获取
/受限制/雇员/*
得到
雇员
保密的

您可以尝试使用过滤器:

筛选器可以在请求到达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>
这也适用于我

<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>


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...
      req.getRequestDispatcher("login.jsp").forward(req, resp); //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 admin
        fc.doFilter(request, response); it redirected towards main.jsp
      }

    }
  }

  @Override
  public void destroy() {
  }
}

会话检查过滤器
yourjavapackage.SessionCheckFilter
会话检查过滤器
/main.jsp
公共类SessionCheckFilter实现过滤器{
私有字符串上下文路径;
@凌驾
public void init(FilterConfig fc)抛出ServletException{
contextPath=fc.getServletContext().getContextPath();
}
@凌驾
public void doFilter(ServletRequest请求、ServletResponse响应、FilterChain fc)抛出IOException、ServletException{
HttpServletRequest req=(HttpServletRequest)请求;
HttpServletResponse res=(HttpServletResponse)响应;
如果(req.getSession().getAttribute(“登录用户”)==null){//检查会话中是否设置了登录用户。。。
req.getRequestDispatcher(“login.jsp”).forward(req,resp);//或要重定向的页面
}否则{
String userType=(String)req.getSession().getAttribute(“登录用户”);
if(userType.equals(“ADMIN”){//检查用户类型是否为ADMIN
doFilter(请求、响应);它重定向到main.jsp
}
}
}
@凌驾
公共空间销毁(){
}
}
如何使用:

String username = request.getRemoteUser();

如果我理解正确,你需要的似乎是在你的web应用程序上进行过滤。。。您可以尝试阅读Servlet规范中定义的容器管理的身份验证。这对你来说够正式的了吗?一定要用这个&不要编写你自己的解决方案!这是预期的方式。不仅如此,根据JEE平台规范,web容器将了解用户身份和角色,允许与EJB、JCA等自动集成,并允许通过标准API进行编程访问,例如request.isUserInRole(role)、request.getUserPrinciple()&类似于EJB
String username = request.getRemoteUser();