-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use AuthorizationAnnotationPropertyConversionService
Closes gh-15721
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ework/security/authorization/method/AuthorizationAnnotationPropertyConversionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2002-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 org.springframework.security.authorization.method; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
import org.springframework.core.convert.converter.ConverterRegistry; | ||
import org.springframework.core.convert.converter.GenericConverter; | ||
import org.springframework.core.convert.support.DefaultConversionService; | ||
import org.springframework.core.convert.support.GenericConversionService; | ||
|
||
/** | ||
* A {@link ConversionService} configured with converters that provide class to string | ||
* conversion | ||
* | ||
* @author DingHao | ||
* @since 6.3 | ||
*/ | ||
public final class AuthorizationAnnotationPropertyConversionService extends GenericConversionService { | ||
|
||
private static volatile AuthorizationAnnotationPropertyConversionService sharedInstance; | ||
|
||
private AuthorizationAnnotationPropertyConversionService() { | ||
addConverters(this); | ||
} | ||
|
||
/** | ||
* Returns a shared instance of | ||
* {@code AuthorizationAnnotationPropertyConversionService}. | ||
* @return a shared instance of | ||
* {@code AuthorizationAnnotationPropertyConversionService} | ||
*/ | ||
public static AuthorizationAnnotationPropertyConversionService getSharedInstance() { | ||
AuthorizationAnnotationPropertyConversionService cs = sharedInstance; | ||
if (cs == null) { | ||
synchronized (AuthorizationAnnotationPropertyConversionService.class) { | ||
cs = sharedInstance; | ||
if (cs == null) { | ||
cs = new AuthorizationAnnotationPropertyConversionService(); | ||
sharedInstance = cs; | ||
} | ||
} | ||
} | ||
return cs; | ||
} | ||
|
||
/** | ||
* Adds the converters that provide type conversion for | ||
* AuthorizationAnnotationProperty values to the provided {@link ConverterRegistry}. | ||
* @param converterRegistry the registry of converters to add to | ||
*/ | ||
public static void addConverters(ConverterRegistry converterRegistry) { | ||
DefaultConversionService.addDefaultConverters(converterRegistry); | ||
converterRegistry.addConverter(new ClassToStringConverter()); | ||
} | ||
|
||
static class ClassToStringConverter implements GenericConverter { | ||
|
||
@Override | ||
public Set<ConvertiblePair> getConvertibleTypes() { | ||
return Collections.singleton(new ConvertiblePair(Class.class, String.class)); | ||
} | ||
|
||
@Override | ||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { | ||
return (source != null) ? source.toString() : null; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters