简体   繁体   中英

how to secure web service?

i've created a web service (i think) that produces this output:

[{"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"}]

below is my code:

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) {
    }

}

in what ways can i secure this if i wanted to add an authentication scheme prior to accessing the data? in my other system, i created a function at database level (oracle) that accepts a username and password and returns either true if valid and false if not. can i use this or do i need to do it another way?

appreciate any help.

thanks.

Create a new webservice method which deals with the user authentication, given user and password returns a unique session token which must be passed to any subsequent call to the webservice, then you can check the received token for validity.

An example:

  1. Call the Authentication(String userName, String userPassword) method.
  2. The method do its things and check if the user is authenticated.
  3. The method return the user a unique session string (the method also saves it into a database).
  4. Call SomeOtherMethod(String articleCode, Int articleQuantity, Single articlePrice, String autheticationToken) method.
  5. The method check if the provided authenticationToken can be found in the database and is not expired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM