我正在尝试在 jax-rs 中执行基本身份验证,我使用 jersey 来实现。但它不起作用。 请检查我尝试过的代码。 我试图遵循其他人在网络上的做法,但他们都不起作用,我遵循 YouTube 视频仍然不起作用。我使用 Postman,即使我不添加用户名和密码,我仍然可以访问发布请求,但事实并非如此,我不知道代码有什么问题。请帮忙。
package org.learningrestapi.models;
import java.time.LocalDateTime;
public class Customer {
String firstName;
String lastName;
int customerId;
LocalDateTime lastModified;
public Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDateTime getLastModified() {
return lastModified;
}
public void setLastModified(LocalDateTime lastModified) {
this.lastModified = lastModified;
}
}
package org.learningrestapi.resources;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import org.learningrestapi.models.Customer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;
import java.util.*;
@Path("/customers")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CustomerResource {
private static final Map<Integer, Customer> data = new HashMap<>();
@POST
@RolesAllowed("admin")
public Response createCustomer(Customer newCustomer){
newCustomer.setCustomerId(data.size() + 1);
newCustomer.setLastModified(LocalDateTime.now());
data.put(newCustomer.getCustomerId(), newCustomer);
return Response.status(200).entity(
data.get(newCustomer.getCustomerId()))
.build();
}
@GET
@Path("/{customerId}")
public Response getCustomer(@PathParam("customerId") int customerId, @Context Request request) {
//System.out.println("this is the real date : " + ifModifiedSinceHeader);
Customer customer = data.get(customerId);
if (customer == null) {
return Response.status(404).build(); // returns 404 Not Found
}
EntityTag tag=new EntityTag(
Integer.toString(customer.hashCode())
);
LocalDateTime lastModified = customer.getLastModified();
Instant instant = lastModified.atZone(ZoneOffset.UTC).toInstant();
Date date = Date.from(instant);
CacheControl cc = new CacheControl();
cc.setMaxAge(120);
Response.ResponseBuilder builder= request.evaluatePreconditions(date,tag);
if(builder!=null){
System.out.println(builder.toString());
builder.cacheControl(cc);
builder.lastModified(date);
return builder.build();
}
//
System.out.println("the response is null : cause it doesnt match the etag");
builder = Response.ok(customer, "application/json");
builder.cacheControl(cc);
builder.tag(tag);
return builder.build();
}
@PUT
@Path("/{customerId}")
public Response updateCustomer(Customer customerUpdate, @PathParam("customerId") int customerId, @Context Request request){
Customer customer = data.get(customerId);
EntityTag tag=new EntityTag(
Integer.toString(customer.hashCode())
);
Response.ResponseBuilder builder= request.evaluatePreconditions(tag);
if(builder !=null){
//preconditions not met
return builder.build();
}
LocalDateTime currentDateTime= LocalDateTime.now();
customerUpdate.setLastModified(currentDateTime);
customerUpdate.setCustomerId(1);
data.put(customerId,customerUpdate);
builder =Response.noContent();
// Response.status(200).entity(
// data.get(customerId))
// .build();
return builder.build();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<security-constraint>
<web-resource-collection>
<web-resource-name>customer creation</web-resource-name>
<url-pattern>/api/customers</url-pattern>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Realm</realm-name>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
</web-app>