Security SDK Update Steps
Security SDK Update Steps
3. Implementing Authorization:
Purpose: To add code that checks if a user is allowed to access certain parts of
the API.
Detailed Steps:
- Open the main code file for the API endpoint:
- Look for files with names like `Controller.java`, `Resource.java`, or
anything that handles requests (usually inside a `src/main/java` directory).
- Import the Security Common SDK in your Java class:
- At the top of the file (usually under other `import` statements), add:
import com.optum.security.common.AuthorizationService;
- Add the `AuthorizationService` to your class:
- Inside the class (after the `public class YourClassName {` line), add:
@Autowired
private AuthorizationService authorizationService;
- Implement the authorization check in your API method:
- Find the method that handles the request (e.g., `public ResponseEntity<?>
getResource()`).
- Modify it to include the `isAuthorized` check:
public ResponseEntity<?> getResource() {
String resource = "v1/eob";
String action = "READ";
if (authorizationService.isAuthorized(resource, action)) {
// This is where your original code goes to return the resource
return ResponseEntity.ok().body("Access granted to resource");
} else {
// If the user is not authorized, deny access
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Access
denied");
}
}