I am getting an encrypted String as Query parameter to a Spring rest controller method.
I wanted to decrypt the string before it reaches the method based on some annotat
HandlerMethodArgumentResolver would be the best in this regard.
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Decrypt {
String value();
}
public class DecryptResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(Decrypt.class) != null;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Decrypt attr = parameter.getParameterAnnotation(Decrypt.class);
String encrypted = webRequest.getParameter(attr.value());
String decrypted = decrypt(encrypted);
return decrypted;
}
private String decrypt(String encryptedString) {
// Your decryption logic here
return "decrypted - "+encryptedString;
}
}
@Configuration
@EnableMvc // If you're not using Spring boot
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addArgumentResolvers(List argumentResolvers) {
argumentResolvers.add(new DecryptResolver());
}
}
@RequestMapping(value = "/customer", method = RequestMethod.GET)
public String getAppointmentsForDay(@Decrypt("secret") String customerSecret) {
System.out.println(customerSecret); // Needs to be a decrypted value.
...
}