OmgPaymentProperties.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.ruoyi.app.omgpay;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import java.net.URI;
  5. @Component
  6. @ConfigurationProperties(prefix = "omgpay")
  7. public class OmgPaymentProperties {
  8. private static final String APP_RETURN_SCHEME = "com.twanmsdyh.app";
  9. private static final String APP_RETURN_HOST = "pages";
  10. private static final String APP_RETURN_PATH = "/OrderList/paySuccess/paySuccess";
  11. private String returnUrl;
  12. private String orderResultUrl;
  13. private String appReturnUrl;
  14. public String getReturnUrl() {
  15. return returnUrl;
  16. }
  17. public void setReturnUrl(String returnUrl) {
  18. this.returnUrl = returnUrl;
  19. }
  20. public String getOrderResultUrl() {
  21. return orderResultUrl;
  22. }
  23. public void setOrderResultUrl(String orderResultUrl) {
  24. this.orderResultUrl = orderResultUrl;
  25. }
  26. public String getAppReturnUrl() {
  27. return appReturnUrl;
  28. }
  29. public void setAppReturnUrl(String appReturnUrl) {
  30. this.appReturnUrl = appReturnUrl;
  31. }
  32. public String requireSafeReturnUrl() {
  33. return requireSafeCallbackUrl(returnUrl, "/pay/omg/notify", "ReturnURL");
  34. }
  35. public String requireSafeOrderResultUrl() {
  36. return requireSafeCallbackUrl(orderResultUrl, "/pay/omg/result", "OrderResultURL");
  37. }
  38. public String safeAppReturnUrlOrNull() {
  39. if (appReturnUrl == null || appReturnUrl.isBlank()) {
  40. return null;
  41. }
  42. URI uri;
  43. try {
  44. uri = URI.create(appReturnUrl.trim());
  45. } catch (IllegalArgumentException error) {
  46. throw new IllegalArgumentException("App return URL is invalid", error);
  47. }
  48. if (!APP_RETURN_SCHEME.equalsIgnoreCase(uri.getScheme())
  49. || !APP_RETURN_HOST.equals(uri.getHost())
  50. || !APP_RETURN_PATH.equals(uri.getPath())
  51. || uri.getUserInfo() != null || uri.getQuery() != null || uri.getFragment() != null) {
  52. throw new IllegalArgumentException("App return URL is unsafe");
  53. }
  54. return uri.toASCIIString();
  55. }
  56. private static String requireSafeCallbackUrl(String value, String expectedPath, String name) {
  57. URI uri = requireSafeHttpsUrl(value, "OMG " + name, true);
  58. if (!expectedPath.equals(uri.getPath())) {
  59. throw new IllegalArgumentException("OMG " + name + " is unsafe");
  60. }
  61. return uri.toASCIIString();
  62. }
  63. private static URI requireSafeHttpsUrl(String value, String name, boolean rejectRootPath) {
  64. if (value == null || value.isBlank()) {
  65. throw new IllegalArgumentException(name + " is required");
  66. }
  67. URI uri;
  68. try {
  69. uri = URI.create(value.trim());
  70. } catch (IllegalArgumentException error) {
  71. throw new IllegalArgumentException(name + " is invalid", error);
  72. }
  73. if (value.trim().length() > 200) {
  74. throw new IllegalArgumentException(name + " exceeds 200 characters");
  75. }
  76. if (!uri.isAbsolute() || !"https".equalsIgnoreCase(uri.getScheme())
  77. || uri.getHost() == null || uri.getHost().isBlank()
  78. || uri.getUserInfo() != null || uri.getQuery() != null || uri.getFragment() != null
  79. || (rejectRootPath && (uri.getPath() == null || "/".equals(uri.getPath())))) {
  80. throw new IllegalArgumentException(name + " is unsafe");
  81. }
  82. return uri;
  83. }
  84. }