123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- /*
- * Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
- *
- * Licensed under the GNU Affero General Public License, Version 3 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.gnu.org/licenses/agpl-3.0.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package cn.tycoding.langchat.common.utils;
- import cn.hutool.json.JSONUtil;
- import cn.tycoding.langchat.common.constant.CommonConst;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
- import lombok.SneakyThrows;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- /**
- * @author tycoding
- * @since 2024/1/2
- */
- public class ServletUtil {
- @SneakyThrows
- public static void write(HttpServletResponse response, R data) {
- response.setStatus(data.getCode());
- response.setHeader("Content-type", "application/json;charset=" + CommonConst.UTF_8);
- response.setCharacterEncoding(CommonConst.UTF_8);
- response.getWriter().write(JSONUtil.toJsonStr(data));
- }
- @SneakyThrows
- public static void write(HttpServletResponse response, int status, R data) {
- response.setStatus(status);
- response.setHeader("Content-type", "application/json;charset=" + CommonConst.UTF_8);
- response.setCharacterEncoding(CommonConst.UTF_8);
- response.getWriter().write(JSONUtil.toJsonStr(data));
- }
- public static HttpServletRequest getRequest() {
- ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- if (servletRequestAttributes != null) {
- return servletRequestAttributes.getRequest();
- }
- return null;
- }
- public static String getAuthorizationToken() {
- String token = getRequest().getHeader("Authorization");
- if (token != null && token.toLowerCase().startsWith("bearer")) {
- return token.toLowerCase().replace("bearer", "").trim();
- }
- return null;
- }
- public static String getToken(String token) {
- if (token != null && token.toLowerCase().startsWith("bearer")) {
- return token.replace("bearer", "").trim();
- }
- return token;
- }
- public static String getIpAddr() {
- HttpServletRequest request = getRequest();
- if (request == null) {
- return "unknown";
- } else {
- String ip = request.getHeader("x-forwarded-for");
- if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("X-Forwarded-For");
- }
- if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("X-Real-IP");
- }
- if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- if ("0:0:0:0:0:0:0:1".equals(ip)) {
- ip = "127.0.0.1";
- }
- if (ip.contains(",")) {
- ip = ip.split(",")[0];
- }
- return ip;
- }
- }
- }
|