| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
As a developer, we need to make sure not to publish personally identifiable information (PII) of our users. Majority of times, we achieve this by hiding data at the UI side usually by hiding the element. But, the API response will still contain the PII data.
In JAVA, this can be achieved using a combination of Annotations and ResponseBodyAdvice.
@Target(ElementType.METHOD)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Filter {
// JSON keys that will be filtered
String[] keys() default {};
}
@ControllerAdvice
public class FilterAdvice implements ResponseBodyAdvice<Object> {
@Autowired
Obfuscator obfuscator;
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// Check if the API is annotated
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// write code to obfuscate the data
}
}
@RestController
public class ObfuscationExample {
@RequestMapping("/greeting")
@Filter(keys = {"Name"})
public Map<String,Object> greeting() {
Map<String,Object> response = new HashMap<>();
response.put("Name","Justine");
return response;
}
}
| Back | FazBrowse Home | New Git URL |