-
Notifications
You must be signed in to change notification settings - Fork 3k
Add support for Spring Security's @Secured #5225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
This file contains hidden or 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,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-spring-security-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
<relativePath>../</relativePath> | ||
</parent> | ||
|
||
<artifactId>quarkus-spring-security-deployment</artifactId> | ||
<name>Quarkus - Spring Security - Deployment</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-spring-security</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-arc-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-security-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5-internal</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus.security</groupId> | ||
<artifactId>quarkus-security</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>javax.enterprise</groupId> | ||
<artifactId>cdi-api</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-security-test-utils</artifactId> | ||
geoand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-extension-processor</artifactId> | ||
<version>${project.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
...ing-security/deployment/src/main/java/io/quarkus/spring/security/deployment/DotNames.java
This file contains hidden or 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,12 @@ | ||
package io.quarkus.spring.security.deployment; | ||
|
||
import org.jboss.jandex.DotName; | ||
import org.springframework.security.access.annotation.Secured; | ||
|
||
public final class DotNames { | ||
|
||
static final DotName SPRING_SECURED = DotName.createSimple(Secured.class.getName()); | ||
|
||
private DotNames() { | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...c/main/java/io/quarkus/spring/security/deployment/SpringSecurityAnnotationsRegistrar.java
This file contains hidden or 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,25 @@ | ||
package io.quarkus.spring.security.deployment; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.jboss.jandex.DotName; | ||
import org.springframework.security.access.annotation.Secured; | ||
|
||
import io.quarkus.arc.processor.InterceptorBindingRegistrar; | ||
|
||
public class SpringSecurityAnnotationsRegistrar implements InterceptorBindingRegistrar { | ||
|
||
public static final Map<DotName, Set<String>> SECURITY_BINDINGS = new HashMap<>(); | ||
|
||
static { | ||
SECURITY_BINDINGS.put(DotName.createSimple(Secured.class.getName()), Collections.singleton("value")); | ||
} | ||
|
||
@Override | ||
public Map<DotName, Set<String>> registerAdditionalBindings() { | ||
return SECURITY_BINDINGS; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...ployment/src/main/java/io/quarkus/spring/security/deployment/SpringSecurityProcessor.java
This file contains hidden or 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,110 @@ | ||
package io.quarkus.spring.security.deployment; | ||
|
||
import static io.quarkus.security.deployment.SecurityTransformerUtils.findFirstStandardSecurityAnnotation; | ||
import static io.quarkus.security.deployment.SecurityTransformerUtils.hasStandardSecurityAnnotation; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
|
||
import io.quarkus.arc.deployment.AdditionalBeanBuildItem; | ||
import io.quarkus.arc.deployment.InterceptorBindingRegistrarBuildItem; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.ApplicationIndexBuildItem; | ||
import io.quarkus.deployment.builditem.FeatureBuildItem; | ||
import io.quarkus.security.deployment.AdditionalSecurityCheckBuildItem; | ||
import io.quarkus.security.deployment.SecurityCheckInstantiationUtil; | ||
import io.quarkus.security.deployment.SecurityTransformerUtils; | ||
import io.quarkus.spring.security.runtime.interceptor.SpringSecuredInterceptor; | ||
|
||
class SpringSecurityProcessor { | ||
|
||
@BuildStep | ||
FeatureBuildItem feature() { | ||
return new FeatureBuildItem(FeatureBuildItem.SPRING_SECURITY); | ||
} | ||
|
||
@BuildStep | ||
void registerSecurityInterceptors(BuildProducer<InterceptorBindingRegistrarBuildItem> registrars, | ||
BuildProducer<AdditionalBeanBuildItem> beans) { | ||
registrars.produce(new InterceptorBindingRegistrarBuildItem(new SpringSecurityAnnotationsRegistrar())); | ||
beans.produce(new AdditionalBeanBuildItem(SpringSecuredInterceptor.class)); | ||
} | ||
|
||
@BuildStep | ||
void addSpringSecuredSecurityCheck(ApplicationIndexBuildItem index, | ||
BuildProducer<AdditionalSecurityCheckBuildItem> additionalSecurityCheckBuildItems) { | ||
|
||
Set<MethodInfo> methodsWithSecurityAnnotation = new HashSet<>(); | ||
for (AnnotationInstance instance : index.getIndex().getAnnotations(DotNames.SPRING_SECURED)) { | ||
geoand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (instance.value() == null) { | ||
continue; | ||
} | ||
String[] rolesAllowed = instance.value().asStringArray(); | ||
|
||
if (instance.target().kind() == AnnotationTarget.Kind.METHOD) { | ||
MethodInfo methodInfo = instance.target().asMethod(); | ||
checksStandardSecurity(instance, methodInfo); | ||
checksStandardSecurity(instance, methodInfo.declaringClass()); | ||
additionalSecurityCheckBuildItems.produce(new AdditionalSecurityCheckBuildItem(methodInfo, | ||
SecurityCheckInstantiationUtil.rolesAllowedSecurityCheck(rolesAllowed))); | ||
methodsWithSecurityAnnotation.add(methodInfo); | ||
} else if (instance.target().kind() == AnnotationTarget.Kind.CLASS) { | ||
ClassInfo classInfo = instance.target().asClass(); | ||
checksStandardSecurity(instance, classInfo); | ||
for (MethodInfo methodInfo : classInfo.methods()) { | ||
if (!isPublicNonStaticNonConstructor(methodInfo)) { | ||
continue; | ||
} | ||
checksStandardSecurity(instance, methodInfo); | ||
if (!methodsWithSecurityAnnotation.contains(methodInfo)) { | ||
additionalSecurityCheckBuildItems.produce(new AdditionalSecurityCheckBuildItem(methodInfo, | ||
SecurityCheckInstantiationUtil.rolesAllowedSecurityCheck(rolesAllowed))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Validates that there is no @Secured with the standard security annotations at class level | ||
private void checksStandardSecurity(AnnotationInstance instance, ClassInfo classInfo) { | ||
if (hasStandardSecurityAnnotation(classInfo)) { | ||
Optional<AnnotationInstance> firstStandardSecurityAnnotation = findFirstStandardSecurityAnnotation(classInfo); | ||
if (firstStandardSecurityAnnotation.isPresent()) { | ||
String securityAnnotationName = findFirstStandardSecurityAnnotation(classInfo).get().name() | ||
.withoutPackagePrefix(); | ||
throw new IllegalArgumentException("An invalid security annotation combination was detected: Found @" | ||
+ instance.name().withoutPackagePrefix() + " and @" + securityAnnotationName + " on class " | ||
+ classInfo.simpleName()); | ||
} | ||
} | ||
} | ||
|
||
//Validates that there is no @Secured with the standard security annotations at method level | ||
private void checksStandardSecurity(AnnotationInstance instance, MethodInfo methodInfo) { | ||
if (SecurityTransformerUtils.hasStandardSecurityAnnotation(methodInfo)) { | ||
Optional<AnnotationInstance> firstStandardSecurityAnnotation = SecurityTransformerUtils | ||
.findFirstStandardSecurityAnnotation(methodInfo); | ||
if (firstStandardSecurityAnnotation.isPresent()) { | ||
String securityAnnotationName = SecurityTransformerUtils.findFirstStandardSecurityAnnotation(methodInfo).get() | ||
.name() | ||
.withoutPackagePrefix(); | ||
throw new IllegalArgumentException("An invalid security annotation combination was detected: Found " | ||
+ instance.name().withoutPackagePrefix() + " and " + securityAnnotationName + " on method " | ||
+ methodInfo.name()); | ||
} | ||
} | ||
} | ||
|
||
private boolean isPublicNonStaticNonConstructor(MethodInfo methodInfo) { | ||
return Modifier.isPublic(methodInfo.flags()) && !Modifier.isStatic(methodInfo.flags()) | ||
&& !"<init>".equals(methodInfo.name()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.