Skip to content

Commit

Permalink
优化 pac4j CommonProfile 转换
Browse files Browse the repository at this point in the history
  • Loading branch information
Yong.Teng committed Aug 10, 2022
1 parent 1435ae2 commit 73ceadc
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
@Documented
public @interface Principal {

@Deprecated
String id() default ValueConstants.DEFAULT_NONE;

@Deprecated
String realName() default ValueConstants.DEFAULT_NONE;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.pac4j.core.profile.CommonProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.MethodParameter;

import java.util.Map;

Expand All @@ -51,7 +53,7 @@ public static <T> T toObject(final Pac4jPrincipal principal, final Principal ann
}

try{
T instance = org.springframework.beans.BeanUtils.instantiateClass(paramType);
T instance = BeanUtils.instantiateClass(paramType);
Map<String, Object> attributes = ProfileUtils.toMap(profile);

com.buession.beans.BeanUtils.populate(instance, attributes);
Expand All @@ -67,4 +69,11 @@ public static <T> T toObject(final Pac4jPrincipal principal, final Principal ann
}
}

public static Object resolve(final MethodParameter parameter, final Pac4jPrincipal principal){
Principal annotation = parameter.getParameterAnnotation(Principal.class);
Class<?> paramType = parameter.getParameterType();

return toObject(principal, annotation, paramType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter){

@Override
protected Mono<Object> resolveName(String name, MethodParameter parameter, ServerWebExchange exchange){
return exchange.getPrincipal().map((principal)->{
Principal annotation = parameter.getParameterAnnotation(Principal.class);
Class<?> paramType = parameter.getParameterType();

return PrincipalAnnotationUtils.toObject((Pac4jPrincipal) principal, annotation,
paramType);
});
return exchange.getPrincipal()
.map((principal)->PrincipalAnnotationUtils.resolve(parameter, (Pac4jPrincipal) principal));
}

private final static class PrincipalNamedValueInfo extends NamedValueInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
* @author Yong.Teng
* @since 2.1.0
*/
package com.buession.security.pac4j.annotation.reactive;
@NonNullApi
package com.buession.security.pac4j.annotation.reactive;

import org.springframework.lang.NonNullApi;
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter){
@Override
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request){
Principal annotation = parameter.getParameterAnnotation(Principal.class);
Class<?> paramType = parameter.getParameterType();

return PrincipalAnnotationUtils.toObject((Pac4jPrincipal) request.getUserPrincipal(), annotation,
paramType);
return PrincipalAnnotationUtils.resolve(parameter, (Pac4jPrincipal) request.getUserPrincipal());
}

private final static class PrincipalNamedValueInfo extends NamedValueInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
* +-------------------------------------------------------------------------------------------------------+
* | License: http://www.apache.org/licenses/LICENSE-2.0.txt |
* | Author: Yong.Teng <webmaster@buession.com> |
* | Copyright @ 2013-2020 Buession.com Inc. |
* | Copyright @ 2013-2022 Buession.com Inc. |
* +-------------------------------------------------------------------------------------------------------+
*/
package com.buession.security.pac4j.annotation.servlet;
@NonNullApi
package com.buession.security.pac4j.annotation.servlet;

import org.springframework.lang.NonNullApi;
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,12 @@ public HttpAction buildAjaxResponse(final WebContext context,
throw UnauthorizedAction.INSTANCE;
}

final StringBuilder buffer = new StringBuilder();
buffer.append("{\"redirect\":");
final StringBuilder buffer = new StringBuilder("{\"redirect\":{");

if(CommonHelper.isNotBlank(url)){
buffer.append('{');
buffer.append("\"url\":\"").append(url).append("\"");
buffer.append('}');
}else{
buffer.append("{}");
}
buffer.append("}}");

return RedirectionActionHelper.buildFormPostContentAction(context, buffer.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.buession.security.pac4j.http;

import com.buession.lang.Constants;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.http.HttpAction;
Expand Down Expand Up @@ -64,7 +65,8 @@ public HttpAction buildAjaxResponse(final WebContext context,
throw UnauthorizedAction.INSTANCE;
}

return RedirectionActionHelper.buildFormPostContentAction(context, Optional.ofNullable(url).orElse(""));
return RedirectionActionHelper.buildFormPostContentAction(context,
Optional.ofNullable(url).orElse(Constants.EMPTY_STRING));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static CommonProfile getProfileFromPac4jPrincipal(Pac4jPrincipal principa
* @return Map
*/
public static Map<String, Object> toMap(final CommonProfile profile){
final Map<String, Object> attributes = new LinkedHashMap<>(profile.getAttributes());
final Map<String, Object> attributes = new LinkedHashMap<>(profile.getAttributes().size() + 15);

attributes.put("id", profile.getId());
attributes.put("email", profile.getEmail());
Expand All @@ -78,6 +78,8 @@ public static Map<String, Object> toMap(final CommonProfile profile){
attributes.put("roles", profile.getRoles());
attributes.put("permissions", profile.getPermissions());

attributes.putAll(profile.getAttributes());

return attributes;
}

Expand Down

0 comments on commit 73ceadc

Please sign in to comment.