diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/Principal.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/Principal.java index 064e055..4d2a5f7 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/Principal.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/Principal.java @@ -40,8 +40,10 @@ @Documented public @interface Principal { + @Deprecated String id() default ValueConstants.DEFAULT_NONE; + @Deprecated String realName() default ValueConstants.DEFAULT_NONE; /** diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/PrincipalAnnotationUtils.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/PrincipalAnnotationUtils.java index ceebf44..a65ea26 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/PrincipalAnnotationUtils.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/PrincipalAnnotationUtils.java @@ -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; @@ -51,7 +53,7 @@ public static T toObject(final Pac4jPrincipal principal, final Principal ann } try{ - T instance = org.springframework.beans.BeanUtils.instantiateClass(paramType); + T instance = BeanUtils.instantiateClass(paramType); Map attributes = ProfileUtils.toMap(profile); com.buession.beans.BeanUtils.populate(instance, attributes); @@ -67,4 +69,11 @@ public static 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); + } + } diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/PrincipalMethodArgumentResolver.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/PrincipalMethodArgumentResolver.java index fbf24b5..fac892c 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/PrincipalMethodArgumentResolver.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/PrincipalMethodArgumentResolver.java @@ -64,13 +64,8 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter){ @Override protected Mono 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 { diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/package-info.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/package-info.java index 836ab9c..90487e8 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/package-info.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/reactive/package-info.java @@ -26,4 +26,7 @@ * @author Yong.Teng * @since 2.1.0 */ -package com.buession.security.pac4j.annotation.reactive; \ No newline at end of file +@NonNullApi +package com.buession.security.pac4j.annotation.reactive; + +import org.springframework.lang.NonNullApi; \ No newline at end of file diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/PrincipalMethodArgumentResolver.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/PrincipalMethodArgumentResolver.java index 9a0cd58..8562bd1 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/PrincipalMethodArgumentResolver.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/PrincipalMethodArgumentResolver.java @@ -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 { diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/package-info.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/package-info.java index e8e2327..1ce7876 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/package-info.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/annotation/servlet/package-info.java @@ -19,7 +19,10 @@ * +-------------------------------------------------------------------------------------------------------+ * | License: http://www.apache.org/licenses/LICENSE-2.0.txt | * | Author: Yong.Teng | - * | Copyright @ 2013-2020 Buession.com Inc. | + * | Copyright @ 2013-2022 Buession.com Inc. | * +-------------------------------------------------------------------------------------------------------+ */ -package com.buession.security.pac4j.annotation.servlet; \ No newline at end of file +@NonNullApi +package com.buession.security.pac4j.annotation.servlet; + +import org.springframework.lang.NonNullApi; \ No newline at end of file diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/JsonAjaxRequestResolver.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/JsonAjaxRequestResolver.java index 2cfc428..6a12965 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/JsonAjaxRequestResolver.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/JsonAjaxRequestResolver.java @@ -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()); } diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/TextAjaxRequestResolver.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/TextAjaxRequestResolver.java index a7ad0b9..2f8d8a0 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/TextAjaxRequestResolver.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/http/TextAjaxRequestResolver.java @@ -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; @@ -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)); } } diff --git a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/profile/ProfileUtils.java b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/profile/ProfileUtils.java index 016ec5f..e02e1ec 100644 --- a/buession-security-pac4j/src/main/java/com/buession/security/pac4j/profile/ProfileUtils.java +++ b/buession-security-pac4j/src/main/java/com/buession/security/pac4j/profile/ProfileUtils.java @@ -60,7 +60,7 @@ public static CommonProfile getProfileFromPac4jPrincipal(Pac4jPrincipal principa * @return Map */ public static Map toMap(final CommonProfile profile){ - final Map attributes = new LinkedHashMap<>(profile.getAttributes()); + final Map attributes = new LinkedHashMap<>(profile.getAttributes().size() + 15); attributes.put("id", profile.getId()); attributes.put("email", profile.getEmail()); @@ -78,6 +78,8 @@ public static Map toMap(final CommonProfile profile){ attributes.put("roles", profile.getRoles()); attributes.put("permissions", profile.getPermissions()); + attributes.putAll(profile.getAttributes()); + return attributes; }