簡體   English   中英

如何保護 servlet 不被外部用戶訪問?

[英]How to protect a servlet from external users?

我嘗試在 web.xml 中使用安全約束。 我通過使用角色授予管理員權限。 如何測試 servlet 是否安全並且只能由管理員訪問?

要測試 servlet,您至少需要兩個 Google 帳戶。 必須將一個 Google 帳戶至少添加為 Google App Engine 管理控制台上的查看者,不得添加另一個 Google 帳戶。 未在管理控制台中添加的 Google 帳戶不應能夠訪問角色定義為管理員的任何 servlet。

如果由於某種原因測試失敗,您需要確保已按照文檔中的所有步驟來保護 servlet 並實施身份驗證模式。 下面以 Google OAuth 和 UserService 為例進行概述。

開箱即用的 Google App Engine 為您提供了兩個角色供您在應用程序中使用:用戶和管理員。

管理員用戶被定義為在 Google App Engine 項目中被列為三個角色之一的任何用戶,因此如果您想授予某人對您的 servlet 的管理員訪問權限,您可以在http://中將他們添加為查看者appengine.google.com面板。

UserService class 使您可以訪問已登錄的用戶。 您需要使用它為您的用戶創建登錄名 URL,使用他或她的 Google 帳戶通過 Google 登錄,將他或她重定向到您的應用程序,然后使用UserService.isUserAdmin()確定該用戶是否確實是管理員用戶。

Using the Users Service詳細介紹了如何開始使用UserService class。

package guestbook;

import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class GuestbookServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            resp.setContentType("text/plain");
            if(userService.isUserAdmin()) {
                resp.getWriter().println("Hello, " + user.getNickname() + ", you are logged in as an admin");
            } else {
                resp.getWriter().println("Hello, " + user.getNickname());
            }
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
    }
}

Google App Engine 用戶 Java API 概述演示了如何處理 Google App Engine 上的用戶登錄:

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class MyServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        UserService userService = UserServiceFactory.getUserService();

        String thisURL = req.getRequestURI();

        resp.setContentType("text/html");
        if (req.getUserPrincipal() != null) {
            resp.getWriter().println("<p>Hello, " +
                                 req.getUserPrincipal().getName() +
                                 "!  You can <a href=\"" +
                                 userService.createLogoutURL(thisURL) +
                                 "\">sign out</a>.</p>");
        } else {
            resp.getWriter().println("<p>Please <a href=\"" +
                                 userService.createLoginURL(thisURL) +
                                 "\">sign in</a>.</p>");
        } 
    }
}

保護 Servlet:

如果您有用戶除非登錄才能訪問的頁面,您可以在部署描述符中為這些頁面建立安全約束(web.xml

部署描述符:安全和身份驗證頁面演示了如何修改您的 web.xml,以便只有管理員可以訪問某些 servlets。

<security-constraint>
    <web-resource-collection>
        <url-pattern>/profile/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

在此示例中,servlet /profile可由具有任何角色的用戶訪問,由*指示,而/admin servlet 僅可由具有角色admin的用戶訪問。

雖然 Google App Engine Java 確實具有內置的安全性,但作用有些有限。 如果您需要對用戶角色進行更精細的控制,請參閱Luke Taylor 在 Spring Google App Engine 中的安全性上的帖子 這個例子是舊的,但是如果你把你的日志記錄級別提高到 TRACE,你可以讓它在最新版本的 Spring 和最新的 GAE SDK 上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM