Skip to content

Commit

Permalink
perf($api-gateway): parse user's ID from JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Aug 22, 2021
1 parent 651615c commit a70434f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Mono<Void> save(ServerWebExchange exchange, SecurityContext context) {
public Mono<SecurityContext> load(ServerWebExchange exchange) {
val request = exchange.getRequest();
// Ignore allowed URL
for (var ignoredUrl : mafConfiguration.flattenIgnoredUrls()) {
if (antPathMatcher.match(ignoredUrl, request.getURI().getPath())) {
for (var ignoredUrl : this.mafConfiguration.flattenIgnoredUrls()) {
if (this.antPathMatcher.match(ignoredUrl, request.getURI().getPath())) {
return Mono.empty();
}
}
Expand All @@ -56,10 +56,11 @@ public Mono<SecurityContext> load(ServerWebExchange exchange) {
HttpHeaders.AUTHORIZATION, request.getMethod(), request.getURI());
return Mono.error(new SecurityException(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED, "JWT Required"));
}
return authCenterRemoteApi.parse(authorization)
return this.authCenterRemoteApi.parse(authorization)
.map(parseJwtResponse -> {
log.info("parseJwtResponse: {}", parseJwtResponse);
val userPrincipal = UserPrincipal.createByUsername(parseJwtResponse.getUsername());
userPrincipal.setId(parseJwtResponse.getId());
val authentication = new UsernamePasswordAuthenticationToken(userPrincipal, null);
log.warn("About to authenticate… Authentication is created. {}", authentication);
return authentication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, Au
request.getMethod(), request.getURI(), userPrincipal.getUsername());
request
.mutate()
.headers(httpHeaders -> httpHeaders.set(MafHttpHeader.X_USERNAME.getHeader(),
userPrincipal.getUsername()))
.headers(httpHeaders -> {
httpHeaders.set(MafHttpHeader.X_ID.getHeader(), String.valueOf(userPrincipal.getId()));
httpHeaders.set(MafHttpHeader.X_USERNAME.getHeader(), userPrincipal.getUsername());
})
.build();
return new AuthorizationDecision(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
**/
@Getter
public enum MafHttpHeader {
/**
* X-Id
*/
X_ID("X-Id"),
/**
* X-Username
*/
X_USERNAME("X-Username");
X_USERNAME("X-Username"),
;

private final String header;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@Data
@Accessors(chain = true)
public class ParseJwtResponse {
private Long id;
private String username;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmsoftware.maf.springcloudstarter.util.RequestUtil;
import com.jmsoftware.maf.springcloudstarter.util.UsernameUtil;
import com.jmsoftware.maf.springcloudstarter.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aspectj.lang.JoinPoint;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void beforeHandleRequest(JoinPoint joinPoint) {
assert attributes != null;
val request = attributes.getRequest();
log.info(BEFORE_TEMPLATE, request.getRequestURL().toString(), request.getMethod(),
RequestUtil.getRequestIpAndPort(request), UsernameUtil.getCurrentUsername(),
RequestUtil.getRequestIpAndPort(request), UserUtil.getCurrentUsername(),
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), LINE_SEPARATOR,
JSONUtil.toJsonPrettyStr(joinPoint.getArgs()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jmsoftware.maf.common.domain.DeletedField;
import com.jmsoftware.maf.springcloudstarter.util.UsernameUtil;
import com.jmsoftware.maf.springcloudstarter.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

Expand Down Expand Up @@ -47,7 +47,7 @@ public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {
public void insertFill(MetaObject metaObject) {
// 严格填充,只针对非主键的字段,只有该表注解了fill 并且 字段名和字段属性 能匹配到才会进行填充(null 值不填充)
log.info("Starting to insert fill metaObject: {}", metaObject.getOriginalObject());
this.strictInsertFill(metaObject, CREATED_BY_FIELD_NAME, String.class, UsernameUtil.getCurrentUsername())
this.strictInsertFill(metaObject, CREATED_BY_FIELD_NAME, Long.class, UserUtil.getCurrentId())
.strictInsertFill(metaObject, CREATED_TIME_FIELD_NAME, LocalDateTime.class, LocalDateTime.now())
.strictInsertFill(metaObject, DELETED_FIELD_NAME, Byte.class, DeletedField.NOT_DELETED.getValue());
log.info("Finished to insert fill metaObject: {}", metaObject.getOriginalObject());
Expand All @@ -56,7 +56,7 @@ public void insertFill(MetaObject metaObject) {
@Override
public void updateFill(MetaObject metaObject) {
log.info("Starting to update fill metaObject: {}", metaObject.getOriginalObject());
this.strictUpdateFill(metaObject, MODIFIED_BY_FIELD_NAME, String.class, UsernameUtil.getCurrentUsername())
this.strictUpdateFill(metaObject, MODIFIED_BY_FIELD_NAME, Long.class, UserUtil.getCurrentId())
.strictUpdateFill(metaObject, MODIFIED_TIME_FIELD_NAME, LocalDateTime.class, LocalDateTime.now());
log.info("Finished to update fill metaObject: {}", metaObject.getOriginalObject());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import org.springframework.web.context.request.ServletRequestAttributes;

/**
* Description: UsernameUtil, change description here.
* Description: UserUtil, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/28/2021 1:40 PM
**/
public class UsernameUtil {
private UsernameUtil() {
public class UserUtil {
private UserUtil() {
}

public static String getCurrentUsername() {
final ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return servletRequestAttributes.getRequest().getHeader(MafHttpHeader.X_USERNAME.getHeader());
}

public static Long getCurrentId() {
final ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return Long.valueOf(servletRequestAttributes.getRequest().getHeader(MafHttpHeader.X_ID.getHeader()));
}
}

0 comments on commit a70434f

Please sign in to comment.