简体   繁体   English

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

[英]How JSP page should check authentication

I am new to web programming.我是网络编程的新手。 I am asking a common pattern to do things like checking authentication.我要求使用通用模式来执行检查身份验证等操作。 Here is the scenario:这是场景:

The website has a login page for visitors.该网站有一个供访问者使用的登录页面。 It will take username and encrypted password and sent them to server, then get either a error code (username/password doesn't match)or an auth key from the server.它将获取用户名和加密密码并将它们发送到服务器,然后从服务器获取错误代码(用户名/密码不匹配)或身份验证密钥。 When the user logged in successfully, I want the website automatically jump to the main.jsp page that presents the main functionality of the website.当用户登录成功后,我想让网站自动跳转到展示网站主要功能的main.jsp页面。

In this case, I want main.jsp check the user authentication.在这种情况下,我希望main.jsp检查用户身份验证。 That is, I don't want such thing happens like user can directly open www.example.com/main.jsp , and if they did thing like this, I want to redirect them to login page.也就是说,我不希望像用户可以直接打开www.example.com/main.jsp这样的事情发生,如果他们做了这样的事情,我想将他们重定向到登录页面。

So how could I pass authentication information across page, and how could I prevent user from directly accessing the main.jsp without login?那么如何跨页面传递身份验证信息,以及如何防止用户在main.jsp情况下直接访问main.jsp呢? Do I need to use session or anything?我需要使用 session 或任何东西吗?

you could try using filters :您可以尝试使用过滤器

Filter can pre-process a request before it reaches a servlet, post-process a response leaving a servlet, or do both.过滤器可以在请求到达 servlet 之前对其进行预处理,对离开 servlet 的响应进行后处理,或者两者都做。 Filters can intercept, examine, and modify requests and responses.过滤器可以拦截、检查和修改请求和响应。

NOTE: be sure to add a session attribute once your user is logged in, you can use that session attribute on the filter注意:确保在您的用户登录后添加会话属性,您可以在过滤器上使用该会话属性

on your login.jsp add:在您的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网页.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 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() {
  }
}

How JSP page should check authentication JSP 页面应该如何检查身份验证

It shouldn't.不应该。 You should use Container Managed Authentication, and define the login/security in web.xml via URL patterns.您应该使用容器管理身份验证,并通过 URL 模式在 web.xml 中定义登录/安全性。


Added by Glen Best:由 Glen Best 添加:

Eg Add something like this to web.xml:例如,在 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>

This also works for me这也适用于我

<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() {
  }
}

如何使用:

String username = request.getRemoteUser();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Alfresco Share上向JSP页面添加身份验证 - How to Add Authentication to JSP Page on Alfresco Share 如何检查404是否仅用于jsp页面 - How to check if 404 coming only for jsp page 如何在jsp页面中检查会话是否存在 - How to check the existence of a session in a jsp page 我应该如何创建这个新页面? JSP还是servlet? - How should I create this new page? JSP or servlet? 如何创建REST Client,响应应该出现在JSP页面中? - How to Create REST Client, the response should be come in JSP page? 如何检查 jsp 页面中是否存在变量以在索引中发布? - How do I check if variable exists in a jsp page for post in index? java-如何检查JSP页面中是否存在值? - java- how to check if value exists in JSP page? 如何在同一个jsp页面中查看jsp表单请求提交? (ENCTYPE =“多部分/格式数据”) - How can i check jsp form request submit in same jsp page? (enctype=“multipart/form-data”) 在jsp页面中使用指纹进行登录身份验证 - Login authentication using fingerprint in jsp page 使用 JDBC 和 JSP 进行登录身份验证:如何检查用户输入的用户名和密码是否存在于数据库中? - Login Authentication using JDBC and JSP : How to check if the username and Password entered by the user exists in the Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM