ServletUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
  3. *
  4. * Licensed under the GNU Affero General Public License, Version 3 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.gnu.org/licenses/agpl-3.0.html
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cn.tycoding.langchat.common.utils;
  17. import cn.hutool.json.JSONUtil;
  18. import cn.tycoding.langchat.common.constant.CommonConst;
  19. import jakarta.servlet.http.HttpServletRequest;
  20. import jakarta.servlet.http.HttpServletResponse;
  21. import lombok.SneakyThrows;
  22. import org.springframework.web.context.request.RequestContextHolder;
  23. import org.springframework.web.context.request.ServletRequestAttributes;
  24. /**
  25. * @author tycoding
  26. * @since 2024/1/2
  27. */
  28. public class ServletUtil {
  29. @SneakyThrows
  30. public static void write(HttpServletResponse response, R data) {
  31. response.setStatus(data.getCode());
  32. response.setHeader("Content-type", "application/json;charset=" + CommonConst.UTF_8);
  33. response.setCharacterEncoding(CommonConst.UTF_8);
  34. response.getWriter().write(JSONUtil.toJsonStr(data));
  35. }
  36. @SneakyThrows
  37. public static void write(HttpServletResponse response, int status, R data) {
  38. response.setStatus(status);
  39. response.setHeader("Content-type", "application/json;charset=" + CommonConst.UTF_8);
  40. response.setCharacterEncoding(CommonConst.UTF_8);
  41. response.getWriter().write(JSONUtil.toJsonStr(data));
  42. }
  43. public static HttpServletRequest getRequest() {
  44. ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  45. if (servletRequestAttributes != null) {
  46. return servletRequestAttributes.getRequest();
  47. }
  48. return null;
  49. }
  50. public static String getAuthorizationToken() {
  51. String token = getRequest().getHeader("Authorization");
  52. if (token != null && token.toLowerCase().startsWith("bearer")) {
  53. return token.toLowerCase().replace("bearer", "").trim();
  54. }
  55. return null;
  56. }
  57. public static String getToken(String token) {
  58. if (token != null && token.toLowerCase().startsWith("bearer")) {
  59. return token.replace("bearer", "").trim();
  60. }
  61. return token;
  62. }
  63. public static String getIpAddr() {
  64. HttpServletRequest request = getRequest();
  65. if (request == null) {
  66. return "unknown";
  67. } else {
  68. String ip = request.getHeader("x-forwarded-for");
  69. if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
  70. ip = request.getHeader("Proxy-Client-IP");
  71. }
  72. if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
  73. ip = request.getHeader("X-Forwarded-For");
  74. }
  75. if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
  76. ip = request.getHeader("WL-Proxy-Client-IP");
  77. }
  78. if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
  79. ip = request.getHeader("X-Real-IP");
  80. }
  81. if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
  82. ip = request.getRemoteAddr();
  83. }
  84. if ("0:0:0:0:0:0:0:1".equals(ip)) {
  85. ip = "127.0.0.1";
  86. }
  87. if (ip.contains(",")) {
  88. ip = ip.split(",")[0];
  89. }
  90. return ip;
  91. }
  92. }
  93. }