簡體   English   中英

如何保護網絡服務?

[英]how to secure web service?

我創建了一個Web服務(我認為),它會產生以下輸出:

[{"MANAGER_ID":0,"DEPARTMENT_ID":90,"SALARY":24000,"HIRE_DATE":"1987-06-17","FIRST_NAME":"Steven","COMMISSION_PCT":0,"EMAIL":"SKING","EMPLOYEE_ID":100,"JOB_ID":"AD_PRES","PHONE_NUMBER":"515.123.4567","LAST_NAME":"King"}]

下面是我的代碼:

package resource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@Path("hr")
public class HumanResources {
    @SuppressWarnings("unused")
    @Context
    private UriInfo context;

    /**
     * Default constructor. 
     */
    public HumanResources() {
    // TODO Auto-generated constructor stub
    }

    /**
     * Retrieves representation of an instance of HumanResources
     * @return an instance of String
     * @throws NamingException 
     * @throws SQLException 
     */
    @GET
    @Produces("application/json")
    public String getText() throws JSONException, NamingException, SQLException {
    // TODO return proper representation object
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
    Statement sel = conn.createStatement();
    ResultSet rs = sel.executeQuery("select * from employees where rownum <= 5");

    JSONObject employees = new JSONObject();
    JSONArray emp = new JSONArray();

    while (rs.next()) {
        JSONObject employee = new JSONObject();
        employee.put("EMPLOYEE_ID", rs.getInt("EMPLOYEE_ID"));
        employee.put("FIRST_NAME", rs.getString("FIRST_NAME"));
        employee.put("LAST_NAME", rs.getString("LAST_NAME"));
        employee.put("EMAIL", rs.getString("EMAIL"));
        employee.put("PHONE_NUMBER", rs.getString("PHONE_NUMBER"));
        employee.put("HIRE_DATE", rs.getDate("HIRE_DATE"));
        employee.put("JOB_ID", rs.getString("JOB_ID"));
        employee.put("SALARY", rs.getDouble("SALARY"));
        employee.put("COMMISSION_PCT", rs.getDouble("COMMISSION_PCT"));
        employee.put("MANAGER_ID", rs.getInt("MANAGER_ID"));
        employee.put("DEPARTMENT_ID", rs.getInt("DEPARTMENT_ID"));
        emp.put(employee);
    }

    employees.put("EMPLOYEES", emp);

    sel.close();
    return emp.toString();
    }

    /**
     * PUT method for updating or creating an instance of HumanResources
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/plain")
    public void putText(String content) {
    }

}

如果我想在訪問數據之前添加身份驗證方案,我可以通過什么方式保護它? 在我的另一個系統中,我在數據庫級別(oracle)創建了一個函數,該函數接受用戶名和密碼,如果有效則返回true,否則返回false。 我可以使用它還是需要另一種方式?

感謝您的幫助。

謝謝。

創建一個新的Web服務方法來處理用戶身份驗證,給定用戶名和密碼返回一個唯一的會話令牌,該令牌必須傳遞給該Web服務的任何后續調用,然后您可以檢查接收到的令牌的有效性。

一個例子:

  1. 調用Authentication(String userName, String userPassword)方法。
  2. 該方法執行其工作,並檢查用戶是否已通過身份驗證。
  3. 該方法為用戶返回一個唯一的會話字符串(該方法還將其保存到數據庫中)。
  4. 調用SomeOtherMethod(String articleCode, Int articleQuantity, Single articlePrice, String autheticationToken)方法。
  5. 該方法檢查提供的authenticationToken可以在數據庫中找到並且沒有過期。

暫無
暫無

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

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