-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...y/security-method/src/appTest/java/com/example/security/method/AuthorizedObjectTests.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,53 @@ | ||
package com.example.security.method; | ||
|
||
import java.time.Duration; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.aot.smoketest.support.assertj.AssertableOutput; | ||
import org.springframework.aot.smoketest.support.junit.ApplicationTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@ApplicationTest | ||
public class AuthorizedObjectTests { | ||
|
||
@Test | ||
void anonymousCanOnlyCallPermittedMethods(AssertableOutput output) { | ||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(output) | ||
// @formatter:off | ||
.hasSingleLineContaining("testAuthorizedObject(): object.getUserProperty() correctly allowed as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): object.getAdminProperty() correctly allowed as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): object.getUserPropertyJsr250() correctly allowed as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): object.getUserPropertySecured() correctly allowed as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserProperty() correctly denied as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getAdminProperty() correctly denied as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertyJsr250() correctly denied as anonymous") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertySecured() correctly denied as anonymous")); | ||
// @formatter:on | ||
} | ||
|
||
@Test | ||
void userCanOnlyCallPermittedMethods(AssertableOutput output) { | ||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(output) | ||
// @formatter:off | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserProperty() correctly allowed as user") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getAdminProperty() correctly denied as user") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertyJsr250() correctly allowed as user") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertySecured() correctly allowed as user")); | ||
// @formatter:on | ||
} | ||
|
||
@Test | ||
void adminCanOnlyCallPermittedMethods(AssertableOutput output) { | ||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertThat(output) | ||
// @formatter:off | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserProperty() correctly denied as admin") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getAdminProperty() correctly allowed as admin") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertyJsr250() correctly denied as admin") | ||
.hasSingleLineContaining("testAuthorizedObject(): authorized.getUserPropertySecured() correctly denied as admin")); | ||
// @formatter:on | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
security/security-method/src/main/java/com/example/security/method/AuthorizableObject.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,31 @@ | ||
package com.example.security.method; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
|
||
import org.springframework.security.access.annotation.Secured; | ||
|
||
public class AuthorizableObject { | ||
|
||
private final String property = "value"; | ||
|
||
@HasRole("ADMIN") | ||
public String getAdminProperty() { | ||
return this.property; | ||
} | ||
|
||
@HasRole("USER") | ||
public String getUserProperty() { | ||
return this.property; | ||
} | ||
|
||
@RolesAllowed("USER") | ||
public String getUserPropertyJsr250() { | ||
return this.property; | ||
} | ||
|
||
@Secured("ROLE_USER") | ||
public String getUserPropertySecured() { | ||
return this.property; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...curity-method/src/main/java/com/example/security/method/AuthorizedReturnValueService.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,18 @@ | ||
package com.example.security.method; | ||
|
||
import org.springframework.security.authorization.method.AuthorizeReturnObject; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AuthorizedReturnValueService { | ||
|
||
@AuthorizeReturnObject | ||
public AuthorizableObject getAuthorizedObject() { | ||
return new AuthorizableObject(); | ||
} | ||
|
||
public AuthorizableObject getObject() { | ||
return new AuthorizableObject(); | ||
} | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
security/security-method/src/main/java/com/example/security/method/HasRole.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,14 @@ | ||
package com.example.security.method; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import org.springframework.security.access.prepost.PreAuthorize; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@PreAuthorize("hasRole('{value}')") | ||
public @interface HasRole { | ||
|
||
String value(); | ||
|
||
} |
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