Skip to content

SecureRestEndpoints

kohlhaas edited this page Jun 20, 2024 · 1 revision

Obtaining and Using an Access Token in Rapla

Getting an Access Token

To obtain an access token for the user admin with an empty password, use the following curl command:

curl -X POST "http://localhost:8051/rapla/authentication?username=admin" --data '""'

Using the Access Token

Use the obtained token for secured requests by including it in the Authorization header:

curl -X GET "http://localhost:8051/rapla/pruefungen/secure" -H 'Authorization: Bearer InsertTokenHere'

Securing an Endpoint in Java

@Inject
RemoteSession session;
private final HttpServletRequest request;

@Inject
public RaplaPruefungen(@Context HttpServletRequest request) {
    this.request = request;
}

@GET
@Produces(MediaType.TEXT_HTML)
@Path("secure")
public void generateSecurePage(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
    User user = session.checkAndGetUser(request);
    java.io.PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Secured Page</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Welcome, " + user.getUsername());
    out.println("</body>");
    out.println("</html>");
}

This example demonstrates how to secure a page by verifying the access token and authenticating the user.

Clone this wiki locally