RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 648114
Accepted
Nick
Nick
Asked:2020-04-04 15:02:12 +0000 UTC2020-04-04 15:02:12 +0000 UTC 2020-04-04 15:02:12 +0000 UTC

限制对 servlet 应用程序中页面的访问

  • 772

大家好。伙计们可以举例说明在 servlet 应用程序中限制对页面的访问。因此,当我以用户身份登录时,我无法使用 /admin url 转到管理页面。

这是登录 servlet 代码:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        response.setContentType("text/html");

        String user = request.getParameter("userName");
        String pass = request.getParameter("userPassword");

        if (user.equals("admin") && pass.equals("admin")) {
            response.sendRedirect("/admin");
        }
        if (user.equals("user") && pass.equals("user")) {
            response.sendRedirect("/user");
        }
        if (user.equals("page3") && pass.equals("page3")) {
            response.sendRedirect("page3.jsp");
        }
        else
            pw.println("Login Failed...!");
        pw.close();

    }


}
servlet
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Mikhail Vaysman
    2020-04-04T17:58:10Z2020-04-04T17:58:10Z

    找到您的 tomcat-users.xml 文件并向其添加管理员

    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <!-- какие-то строки -->
      <role rolename="admin"/>
      <user username="admin" password="password" roles="admin"/>
      <!-- какие-то строки -->
    </tomcat-users>
    

    之后在您的应用程序的web.xml中添加

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="tomcat-demo" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <!-- какие-то строки -->
    
        <security-constraint>
            <web-resource-collection>
                <web-resource-name>Admin's panel</web-resource-name>
                <url-pattern>/admin</url-pattern>
                <http-method>GET</http-method>
                <http-method>POST</http-method>
            </web-resource-collection>
            <auth-constraint>
                <role-name>admin</role-name>
            </auth-constraint>
    
            <user-data-constraint>
                <!-- это ТОЛЬКО для тестов -->
                <transport-guarantee>NONE</transport-guarantee>
            </user-data-constraint>
        </security-constraint>
    
        <login-config>
            <auth-method>FORM</auth-method>
            <form-login-config>
                <form-login-page>/login.html</form-login-page>
                <form-error-page>/no_access.html</form-error-page>
            </form-login-config>
        </login-config>
    
        <!-- какие-то строки -->
    
    </web-app>
    

    登录.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
    </head>
    <body>
    <form method="POST" action="j_security_check">
        <p>Login to Admin's panel:</p>
        <p>Name:</p>
        <input type="text" name="j_username" />
        <p>Password:</p>
        <input type="password" name="j_password" />
        <input type="submit" value="Login" />
    </form>
    </body>
    </html>
    

    no_access.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>NOOOOOOOO</title>
    </head>
    <body>
    <h1>login failed!</h1>
    </body>
    </html>
    

    之后,系统将提示您输入密码以访问指定的URL。

    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5