这里使用HttpAuthenticationFeature构建Jersey rest客户端,用于访问认证/授权安全背后的rest API。
https://onitroad.com 更多教程
如何保护 REST API
对于启用身份验证的 rest api,请使用与角色相关的注释,例如 @RolesAllowed
。
例如,这是使 REST API 安全的代码。
@Path("/employees") public class JerseyService { @RolesAllowed("ADMIN") @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Employees getAllEmployees() { Employees list = new Employees(); list.setEmployeeList(new ArrayList<Employee>()); list.getEmployeeList().add(new Employee(1, "JackLi Gupta")); list.getEmployeeList().add(new Employee(2, "JackLi Kolenchiskey")); list.getEmployeeList().add(new Employee(3, "Tomm Kameron")); return list; } }
Jersey 客户端 - HttpAuthenticationFeature
HttpAuthenticationFeature
类提供 HttpBasic 和 Digest 客户端身份验证功能。
该功能以 4 种模式之一工作,例如:BASIC、BASIC NON-PREEMPTIVE、DIGEST 和 UNIVERSAL。
让我们快速了解一下它们。
- BASIC - 它是抢占式身份验证方式,例如:信息始终与每个 HTTP 请求一起发送。此模式必须与 SSL/TLS 结合使用,因为密码仅发送 BASE64 编码。
- BASIC NON-PREEMPTIVE - 它是非抢占式身份验证方式,例如:仅当服务器以 401 状态码拒绝请求时才添加身份验证信息,然后使用身份验证信息重复请求。
- DIGEST - Http 摘要认证。不需要使用 SSL/TLS。
- 通用 - 非抢占模式下基本和摘要认证的组合,例如:在 401 响应的情况下,根据 WWW-Authenticate HTTP 标头中定义的请求的认证使用适当的认证。
要使用 HttpAuthenticationFeature ,请构建它的实例并向客户端注册。
基本认证方式
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password"); final Client client = ClientBuilder.newClient(); client.register(feature);
基本身份验证 - 非抢先模式
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder() .nonPreemptive() .credentials("username", "password") .build(); final Client client = ClientBuilder.newClient(); client.register(feature);
通用模式
//HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal("username", "password"); //Universal builder having different credentials for different schemes HttpAuthenticationFeature feature = HttpAuthenticationFeature.universalBuilder() .credentialsForBasic("username1", "password1") .credentials("username2", "password2").build(); final Client client = ClientBuilder.newClient(); client.register(feature);
Jersey REST客户端代码
下面是 jersey rest 客户端基本身份验证示例,它接受用于身份验证的用户名和密码详细信息。
public static void main(String[] args) throws IOException { httpGETCollectionExample(); } private static void httpGETCollectionExample() { ClientConfig clientConfig = new ClientConfig(); HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("onitroad", "password"); clientConfig.register( feature) ; clientConfig.register(JacksonFeature.class); Client client = ClientBuilder.newClient( clientConfig ); WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("employees"); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); System.out.println(response.getStatus()); System.out.println(response.getStatusInfo()); if(response.getStatus() == 200) { Employees employees = response.readEntity(Employees.class); List<Employee> listOfEmployees = employees.getEmployeeList(); System.out.println(Arrays.toString( listOfEmployees.toArray(new Employee[listOfEmployees.size()]) )); } }
用户名/密码正确时的输出
200 OK [Employee [id=1, name=JackLi Gupta], Employee [id=2, name=JackLi Kolenchiskey], Employee [id=3, name=Tomm Kameron]]
用户名/密码不正确时的输出
401 Unauthorized
日期:2020-09-17 00:16:33 来源:oir作者:oir