1
我創建了一個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。我可以使用這個還是我需要以另一種方式來做?
感謝任何幫助。
謝謝。
嗨, 感謝您的回覆。想確認一些事情。 _Call認證(字符串用戶名,字符串userPassword)方法._ 我應該在我的HumanResources類中添加此認證功能? _The方法返回用戶的唯一會話字符串(該方法也將其保存到數據庫中)._ 所以基本上我需要創建生成一個唯一的會話字符串,然後將其保存到我將創建一個表的過程,即。 user_sessions? – adshocker 2011-03-04 01:30:57
_「如果提供的authenticationToken可以在數據庫中找到,未過期的方法檢查。」 _ - 這應該是在人力資源類呢? – adshocker 2011-03-04 01:36:33
的'Authentication'和'CheckToken'(如你願意,你可以爲它們命名),可以放在任何地方;因爲他們不依賴於特定的類(它們是輔助方法),我將它們放在'Utilities'靜態類或類似的東西,只是確保他們可以從你的WebMethod每個調用。 – Albireo 2011-03-04 08:22:54