如何保护网络服务?
问题描述:
我创建了一个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服务的任何后续呼叫的唯一会话令牌,那么你可以检查有效性接收的令牌。
一个例子:
- 调用
Authentication(String userName, String userPassword)
方法。 - 该方法做它的事情,并检查用户是否通过身份验证。
- 该方法返回用户一个唯一的会话字符串(该方法也将其保存到数据库中)。
- 请致电
SomeOtherMethod(String articleCode, Int articleQuantity, Single articlePrice, String autheticationToken)
方法。 - 该方法检查提供的
authenticationToken
是否可以在数据库中找到并且没有过期。
嗨, 感谢您的回复。想确认一些事情。 _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