-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSecureService.java
More file actions
110 lines (99 loc) · 3.16 KB
/
Copy pathSecureService.java
File metadata and controls
110 lines (99 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.axonivy.connectivity.rest.provider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jakarta.annotation.security.DenyAll;
import jakarta.annotation.security.PermitAll;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Singleton;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import org.apache.cxf.transport.commons_text.StringEscapeUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
* Demonstrates a service which protects it's methods with
* <code>javax.security</code> annotations.
*
* <p>
* By default an ivy REST service requires clients to be authenticated with
* username and password via HTTP-BASIC. This behaviour can be adjusted with the
* following annotations:
* </p>
* <ul>
* <li>{@link jakarta.annotation.security.PermitAll}: allows unauthenticated
* access to anonymous users</li>
* <li>{@link jakarta.annotation.security.RolesAllowed}: users must be
* authenticated and own the defined roles</li>
* <li>{@link jakarta.annotation.security.DenyAll}: nobody is allowed to invoke
* this service</li>
* </ul>
*
* All these annotations can be set either on the service class or on a specific
* method. Service class annotations are considered for all methods. But an
* annotation on a method always override the service class annotation.
*
* @since 6.4.0
*/
@Singleton
@Path("admin")
@Tag(name = ApiConstants.DEMO_TAG)
public class SecureService {
private final List<String> entries = new ArrayList<>(Arrays.asList("Hello world"));
/**
* {@link PermitAll}: no authentication required to call this method.
* Anonymous access granted.
*/
@GET
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
public Response showEntries() {
return Response.status(Status.OK)
.entity(entries)
.build();
}
/**
* No <code>javax.security</code> annotation present: Defaults to HTTP-BASIC
* auth required.
*/
@PUT
@Consumes(MediaType.TEXT_PLAIN)
public Response addEntry(String newEntry) {
var entry = StringEscapeUtils.escapeHtml4(newEntry);
entries.add(entry);
return Response.status(Status.OK)
.entity("Added entry '" + entry + "'")
.build();
}
/**
* {@link RolesAllowed}: only HTTP-BASIC authenticated users which own the
* role 'Boss' are allowed to call this method.
*/
@POST
@Path("/{entryId}")
@RolesAllowed("Boss")
public Response updateEntry(@PathParam("entryId") int id, String newEntry) {
var entry = StringEscapeUtils.escapeHtml4(newEntry);
entries.set(id, entry);
return Response.status(Status.OK)
.entity("Update entry with id (" + id + ") to '" + entry + "'")
.build();
}
/**
* {@link DenyAll}: Access to this method is blocked for all users
*/
@DELETE
@Path("/{entryId}")
@DenyAll
public void removeEntry(@PathParam("entryId") int id) {
entries.remove(id);
}
}