Skip to content

Commit

Permalink
Merge branch 'feature/1.0.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu committed Oct 5, 2024
2 parents 475bc1b + 393961f commit 825cfc2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ private RestResult() {

}

// public static <T> BaseResponseBuilder<T> builder() {
// return new BaseResponseBuilder<>();
// }

public Boolean getSuccess() {
return isSuccess;
}
Expand All @@ -81,49 +77,4 @@ public void setSuccess(Boolean success) {
isSuccess = success;
}

// public static final class BaseResponseBuilder<T> {
//
// private int code;
//
// private String message;
//
// private T data;
//
// private BaseResponseBuilder() {
// }
//
// public BaseResponseBuilder<T> withResponseCodeStatus(ResponseCode responseCode) {
// this.code = responseCode.getCode();
// this.message = responseCode.getMessage();
// return this;
// }
//
// public BaseResponseBuilder<T> withCode(int code) {
// this.code = code;
// return this;
// }
//
// public BaseResponseBuilder<T> withMessage(String message) {
// this.message = message;
// return this;
// }
//
// public BaseResponseBuilder<T> withData(T data) {
// this.data = data;
// return this;
// }
//
// /**
// * Build result.
// *
// * @return result
// */
// public RestResult<T> build() {
// RestResult<T> restResult = new RestResult<>();
// restResult.setCode(code);
// restResult.setMessage(message);
// restResult.setData(data);
// return restResult;
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@

package com.fuhouyu.framework.web.config;

import com.fuhouyu.framework.constants.HttpRequestHeaderConstant;
import com.fuhouyu.framework.context.user.DefaultUserDetail;
import com.fuhouyu.framework.context.user.User;
import com.fuhouyu.framework.utils.ClassUtils;
import com.fuhouyu.framework.utils.JacksonUtil;
import com.fuhouyu.framework.web.handler.HttpRequestContextHandler;
import com.fuhouyu.framework.web.handler.HttpRequestUserHandler;
import com.fuhouyu.framework.web.handler.UserExtensionHandlerInterceptor;
import com.fuhouyu.framework.web.handler.UserExtensionHandler;
import com.fuhouyu.framework.web.handler.UserParseHandler;
import com.fuhouyu.framework.web.properties.WebProperties;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -67,6 +76,7 @@ public void addInterceptors(InterceptorRegistry registry) {
*/
private AsyncHandlerInterceptor getHttpHandlerInterceptor() {
return new HttpRequestUserHandler(this.getUserExtensionHandlerInterceptors(),
this.userParseHandler(),
this.getUserClassName());
}

Expand All @@ -75,28 +85,60 @@ private AsyncHandlerInterceptor getHttpHandlerInterceptor() {
*
* @return 用户扩展处理器
*/
private List<UserExtensionHandlerInterceptor> getUserExtensionHandlerInterceptors() {
private List<UserExtensionHandler> getUserExtensionHandlerInterceptors() {
String[] beanNamesForType = applicationContext.getBeanNamesForType(
UserExtensionHandlerInterceptor.class);
UserExtensionHandler.class);
if (beanNamesForType.length == 0) {
return Collections.emptyList();
}
// 如果存在用户扩展项,进行设置
List<UserExtensionHandlerInterceptor> list = new ArrayList<>(
List<UserExtensionHandler> list = new ArrayList<>(
beanNamesForType.length);
for (String beanName : beanNamesForType) {
list.add(applicationContext.getBean(beanName,
UserExtensionHandlerInterceptor.class));
UserExtensionHandler.class));
}
OrderComparator.sort(list);
AnnotationAwareOrderComparator.sort(list);
return list;
}

/**
* 解析user子类型
*
* @param <T> 用户泛型
* @return user子类型
*/
@SuppressWarnings("unchecked")
private <T extends User> Class<T> getUserClassName() {
String userClassName = webProperties.getUserClassName();
return Objects.isNull(userClassName) ? (Class<T>) DefaultUserDetail.class :
ClassUtils.loadClass(userClassName);
}

/**
* 用户解析的处理器
*
* @return 解析用户信息
*/
private UserParseHandler userParseHandler() {
try {
return applicationContext.getBean(UserParseHandler.class);
} catch (BeansException e) {
// 返回一个默认
return new UserParseHandler() {
@Override
public <T extends User> T parseUser(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Class<T> userSubClass) {
String userInfo = request.getHeader(HttpRequestHeaderConstant.USERINFO_HEADER);
if (Objects.isNull(userInfo)) {
return null;
}
userInfo = URLDecoder.decode(userInfo, Charset.defaultCharset());
return JacksonUtil.readValue(userInfo, userSubClass);
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package com.fuhouyu.framework.web.handler;

import com.fuhouyu.framework.constants.HttpRequestHeaderConstant;
import com.fuhouyu.framework.context.Context;
import com.fuhouyu.framework.context.user.User;
import com.fuhouyu.framework.context.user.UserContextHolder;
import com.fuhouyu.framework.utils.JacksonUtil;
import com.fuhouyu.framework.utils.LoggerUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -30,8 +28,6 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Objects;

Expand All @@ -47,43 +43,45 @@ public class HttpRequestUserHandler implements AsyncHandlerInterceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestUserHandler.class);

private final List<UserExtensionHandlerInterceptor> userExtensionInterceptors;
private final List<UserExtensionHandler> userExtensionInterceptors;

private final UserParseHandler userParseHandler;

/**
* 需要转换的用户类型
*/
private final Class<? extends User> subUserType;

public HttpRequestUserHandler(Class<? extends User> subUserType) {
this(null, subUserType);
public HttpRequestUserHandler(Class<? extends User> subUserType, UserParseHandler userParseHandler) {
this(null, userParseHandler, subUserType);
}

public HttpRequestUserHandler(List<UserExtensionHandlerInterceptor> userExtensionInterceptors,
public HttpRequestUserHandler(List<UserExtensionHandler> userExtensionInterceptors, UserParseHandler userParseHandler,
Class<? extends User> subUserType) {
this.userExtensionInterceptors = userExtensionInterceptors;
this.userParseHandler = userParseHandler;
this.subUserType = subUserType;
}

@Override
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) throws Exception {
String userInfo = request.getHeader(HttpRequestHeaderConstant.USERINFO_HEADER);
if (Objects.isNull(userInfo)) {
User user = userParseHandler.parseUser(request, response, subUserType);
if (Objects.isNull(user)) {
LoggerUtil.debug(LOGGER, "从请求头获取到的用户信息为空, 请求方法:{}", request.getMethod());
return AsyncHandlerInterceptor.super.preHandle(request, response, handler);
}
userInfo = URLDecoder.decode(userInfo, Charset.defaultCharset());
User user = JacksonUtil.readValue(userInfo, subUserType);
Context<User> context = UserContextHolder.createEmptyContext();
context.setObject(user);
if (CollectionUtils.isEmpty(userExtensionInterceptors)) {
return AsyncHandlerInterceptor.super.preHandle(request, response, handler);
}
for (UserExtensionHandlerInterceptor userExtensionInterceptor : userExtensionInterceptors) {
userExtensionInterceptor.userExtensionHandlerInterceptor(user);
for (UserExtensionHandler userExtensionInterceptor : userExtensionInterceptors) {
userExtensionInterceptor.userExtensionHandlerInterceptor(context.getObject());
}
return AsyncHandlerInterceptor.super.preHandle(request, response, handler);
}


@Override
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @since 2024/8/15 19:59
*/
@FunctionalInterface
public interface UserExtensionHandlerInterceptor {
public interface UserExtensionHandler {

/**
* 扩展用户的信息
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* 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 com.fuhouyu.framework.web.handler;

import com.fuhouyu.framework.context.user.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.lang.NonNull;

/**
* <p>
* 用户解析处理器
* </p>
*
* @author fuhouyu
* @since 2024/10/5 23:53
*/
@FunctionalInterface
public interface UserParseHandler {


/**
* 解析用户信息
*
* @param request 请求
* @param response 响应
* @param userSubClass 用户的子类型
* @param <T> 泛型
* @return 用户信息
*/
<T extends User> T parseUser(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Class<T> userSubClass);
}

0 comments on commit 825cfc2

Please sign in to comment.