Reflect grouping in directory structure
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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 Jsr250AotTests {
|
||||
|
||||
@Test
|
||||
void anonymousCanCallOnlyAnonymousMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining(
|
||||
"testJsr250Anonymous(): jsr250ProtectedService.anonymous() worked as anonymous")
|
||||
.hasSingleLineContaining("testJsr250User(): jsr250ProtectedService.user() failed as anonymous")
|
||||
.hasSingleLineContaining("testJsr250Admin(): jsr250ProtectedService.admin() failed as anonymous")
|
||||
.hasSingleLineContaining(
|
||||
"testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as anonymous")
|
||||
.hasSingleLineContaining(
|
||||
"testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as anonymous");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void userCanCallUserMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("testJsr250User(): jsr250ProtectedService.user() worked as user")
|
||||
.hasSingleLineContaining("testJsr250Admin(): jsr250ProtectedService.admin() failed as user")
|
||||
.hasSingleLineContaining("testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as user")
|
||||
.hasSingleLineContaining("testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as user");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminCanCallAdminMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining("testJsr250Admin(): jsr250ProtectedService.admin() worked as admin")
|
||||
.hasSingleLineContaining("testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as admin")
|
||||
.hasSingleLineContaining(
|
||||
"testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as admin");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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 PostAuthorizeAotTests {
|
||||
|
||||
@Test
|
||||
void anonymousCanCallOnlyAnonymousMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining(
|
||||
"testPostAuthorizeAnonymous(): postAuthorizeProtectedService.anonymous() worked as anonymous")
|
||||
.hasSingleLineContaining(
|
||||
"testPostAuthorizeUser(): postAuthorizeProtectedService.user() failed as anonymous")
|
||||
.hasSingleLineContaining(
|
||||
"testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() failed as anonymous");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void userCanCallUserMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining(
|
||||
"testPostAuthorizeUser(): postAuthorizeProtectedService.user() worked as user")
|
||||
.hasSingleLineContaining(
|
||||
"testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() failed as user");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminCanCallAdminMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining(
|
||||
"testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() worked as admin");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
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
|
||||
class PreAuthorizeAotTests {
|
||||
|
||||
@Test
|
||||
void anonymousCanCallOnlyAnonymousMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining(
|
||||
"testAnonymous(): preAuthorizeProtectedService.anonymous() worked as anonymous")
|
||||
.hasSingleLineContaining("testUser(): preAuthorizeProtectedService.user() failed as anonymous")
|
||||
.hasSingleLineContaining("testAdmin(): preAuthorizeProtectedService.admin() failed as anonymous");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void userCanCallUserMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output).hasSingleLineContaining("testUser(): preAuthorizeProtectedService.user() worked as user")
|
||||
.hasSingleLineContaining("testAdmin(): preAuthorizeProtectedService.admin() failed as user");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminCanCallAdminMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining("testAdmin(): preAuthorizeProtectedService.admin() worked as admin");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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 SecuredAotTests {
|
||||
|
||||
@Test
|
||||
void anonymousCanCallOnlyAnonymousMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining(
|
||||
"testSecuredAnonymous(): securedProtectedService.anonymous() worked as anonymous")
|
||||
.hasSingleLineContaining("testSecuredUser(): securedProtectedService.user() failed as anonymous")
|
||||
.hasSingleLineContaining("testSecuredAdmin(): securedProtectedService.admin() failed as anonymous");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void userCanCallUserMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining("testSecuredUser(): securedProtectedService.user() worked as user")
|
||||
.hasSingleLineContaining("testSecuredAdmin(): securedProtectedService.admin() failed as user");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminCanCallAdminMethod(AssertableOutput output) {
|
||||
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
assertThat(output)
|
||||
.hasSingleLineContaining("testSecuredAdmin(): securedProtectedService.admin() worked as admin");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.UserDetailsManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CLR implements CommandLineRunner {
|
||||
|
||||
private final PreAuthorizeProtectedService preAuthorizeProtectedService;
|
||||
|
||||
private final SecuredProtectedService securedProtectedService;
|
||||
|
||||
private final Jsr250ProtectedService jsr250ProtectedService;
|
||||
|
||||
private final PostAuthorizeProtectedService postAuthorizeProtectedService;
|
||||
|
||||
private final UserDetailsManager userDetailsManager;
|
||||
|
||||
public CLR(PreAuthorizeProtectedService preAuthorizeProtectedService,
|
||||
SecuredProtectedService securedProtectedService, Jsr250ProtectedService jsr250ProtectedService,
|
||||
PostAuthorizeProtectedService postAuthorizeProtectedService, UserDetailsManager userDetailsManager) {
|
||||
this.preAuthorizeProtectedService = preAuthorizeProtectedService;
|
||||
this.securedProtectedService = securedProtectedService;
|
||||
this.jsr250ProtectedService = jsr250ProtectedService;
|
||||
this.postAuthorizeProtectedService = postAuthorizeProtectedService;
|
||||
this.userDetailsManager = userDetailsManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
testAnonymous();
|
||||
testUser();
|
||||
testAdmin();
|
||||
testPostAuthorizeAnonymous();
|
||||
testPostAuthorizeUser();
|
||||
testPostAuthorizeAdmin();
|
||||
testSecuredAnonymous();
|
||||
testSecuredUser();
|
||||
testSecuredAdmin();
|
||||
testJsr250Anonymous();
|
||||
testJsr250User();
|
||||
testJsr250Admin();
|
||||
testJsr250PermitAll();
|
||||
testJsr250DenyAll();
|
||||
}
|
||||
|
||||
private void testAnonymous() {
|
||||
impersonateAnonymous();
|
||||
this.preAuthorizeProtectedService.anonymous();
|
||||
System.out.println("testAnonymous(): preAuthorizeProtectedService.anonymous() worked as anonymous");
|
||||
}
|
||||
|
||||
private void testPostAuthorizeAnonymous() {
|
||||
impersonateAnonymous();
|
||||
this.postAuthorizeProtectedService.anonymous();
|
||||
System.out
|
||||
.println("testPostAuthorizeAnonymous(): postAuthorizeProtectedService.anonymous() worked as anonymous");
|
||||
}
|
||||
|
||||
private void testSecuredAnonymous() {
|
||||
impersonateAnonymous();
|
||||
this.securedProtectedService.anonymous();
|
||||
System.out.println("testSecuredAnonymous(): securedProtectedService.anonymous() worked as anonymous");
|
||||
}
|
||||
|
||||
private void testJsr250Anonymous() {
|
||||
impersonateAnonymous();
|
||||
this.jsr250ProtectedService.anonymous();
|
||||
System.out.println("testJsr250Anonymous(): jsr250ProtectedService.anonymous() worked as anonymous");
|
||||
}
|
||||
|
||||
private void testUser() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.preAuthorizeProtectedService.user();
|
||||
throw new IllegalStateException("testUser(): preAuthorizeProtectedService.user() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testUser(): preAuthorizeProtectedService.user() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
this.preAuthorizeProtectedService.user();
|
||||
System.out.println("testUser(): preAuthorizeProtectedService.user() worked as user");
|
||||
}
|
||||
|
||||
private void testPostAuthorizeUser() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.postAuthorizeProtectedService.user();
|
||||
throw new IllegalStateException(
|
||||
"testPostAuthorizeUser(): postAuthorizeProtectedService.user() worked as anonymous");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
System.out.println("testPostAuthorizeUser(): postAuthorizeProtectedService.user() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
this.postAuthorizeProtectedService.user();
|
||||
System.out.println("testPostAuthorizeUser(): postAuthorizeProtectedService.user() worked as user");
|
||||
}
|
||||
|
||||
private void testSecuredUser() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.securedProtectedService.user();
|
||||
throw new IllegalStateException("testSecuredUser(): securedProtectedService.user() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testSecuredUser(): securedProtectedService.user() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
this.securedProtectedService.user();
|
||||
System.out.println("testSecuredUser(): securedProtectedService.user() worked as user");
|
||||
}
|
||||
|
||||
private void testJsr250User() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.jsr250ProtectedService.user();
|
||||
throw new IllegalStateException("testJsr250User(): jsr250ProtectedService.user() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testJsr250User(): jsr250ProtectedService.user() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
this.jsr250ProtectedService.user();
|
||||
System.out.println("testJsr250User(): jsr250ProtectedService.user() worked as user");
|
||||
}
|
||||
|
||||
private void testAdmin() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.preAuthorizeProtectedService.admin();
|
||||
throw new IllegalStateException("testAdmin(): preAuthorizeProtectedService.admin() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testAdmin(): preAuthorizeProtectedService.admin() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
try {
|
||||
this.preAuthorizeProtectedService.admin();
|
||||
throw new IllegalStateException("testAdmin(): preAuthorizeProtectedService.admin() worked as user");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testAdmin(): preAuthorizeProtectedService.admin() failed as user");
|
||||
}
|
||||
|
||||
impersonateAdmin();
|
||||
this.preAuthorizeProtectedService.admin();
|
||||
System.out.println("testAdmin(): preAuthorizeProtectedService.admin() worked as admin");
|
||||
}
|
||||
|
||||
private void testPostAuthorizeAdmin() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.postAuthorizeProtectedService.admin();
|
||||
throw new IllegalStateException(
|
||||
"testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() worked as anonymous");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
System.out.println("testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
try {
|
||||
this.postAuthorizeProtectedService.admin();
|
||||
throw new IllegalStateException(
|
||||
"testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() worked as user");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() failed as user");
|
||||
}
|
||||
|
||||
impersonateAdmin();
|
||||
this.postAuthorizeProtectedService.admin();
|
||||
System.out.println("testPostAuthorizeAdmin(): postAuthorizeProtectedService.admin() worked as admin");
|
||||
}
|
||||
|
||||
private void testSecuredAdmin() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.securedProtectedService.admin();
|
||||
throw new IllegalStateException("testSecuredAdmin(): securedProtectedService.admin() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testSecuredAdmin(): securedProtectedService.admin() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
try {
|
||||
this.securedProtectedService.admin();
|
||||
throw new IllegalStateException("testSecuredAdmin(): securedProtectedService.admin() worked as user");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testSecuredAdmin(): securedProtectedService.admin() failed as user");
|
||||
}
|
||||
|
||||
impersonateAdmin();
|
||||
this.securedProtectedService.admin();
|
||||
System.out.println("testSecuredAdmin(): securedProtectedService.admin() worked as admin");
|
||||
}
|
||||
|
||||
private void testJsr250Admin() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.jsr250ProtectedService.admin();
|
||||
throw new IllegalStateException("testJsr250Admin(): jsr250ProtectedService.admin() worked as anonymous");
|
||||
}
|
||||
catch (AuthenticationException ex) {
|
||||
System.out.println("testJsr250Admin(): jsr250ProtectedService.admin() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
try {
|
||||
this.jsr250ProtectedService.admin();
|
||||
throw new IllegalStateException("testJsr250Admin(): jsr250ProtectedService.admin() worked as user");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testJsr250Admin(): jsr250ProtectedService.admin() failed as user");
|
||||
}
|
||||
|
||||
impersonateAdmin();
|
||||
this.jsr250ProtectedService.admin();
|
||||
System.out.println("testJsr250Admin(): jsr250ProtectedService.admin() worked as admin");
|
||||
}
|
||||
|
||||
private void testJsr250DenyAll() {
|
||||
impersonateAnonymous();
|
||||
try {
|
||||
this.jsr250ProtectedService.denyAll();
|
||||
throw new IllegalStateException(
|
||||
"testJsr250DenyAll(): jsr250ProtectedService.denyAll() worked as anonymous");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as anonymous");
|
||||
}
|
||||
|
||||
impersonateUser();
|
||||
try {
|
||||
this.jsr250ProtectedService.denyAll();
|
||||
throw new IllegalStateException("testJsr250DenyAll(): jsr250ProtectedService.denyAll() worked as user");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as user");
|
||||
}
|
||||
|
||||
impersonateAdmin();
|
||||
try {
|
||||
this.jsr250ProtectedService.denyAll();
|
||||
System.out.println("testJsr250DenyAll(): jsr250ProtectedService.denyAll() worked as admin");
|
||||
}
|
||||
catch (AccessDeniedException ex) {
|
||||
System.out.println("testJsr250DenyAll(): jsr250ProtectedService.denyAll() failed as admin");
|
||||
}
|
||||
}
|
||||
|
||||
private void testJsr250PermitAll() {
|
||||
impersonateAnonymous();
|
||||
this.jsr250ProtectedService.permitAll();
|
||||
System.out.println("testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as anonymous");
|
||||
|
||||
impersonateUser();
|
||||
this.jsr250ProtectedService.permitAll();
|
||||
System.out.println("testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as user");
|
||||
|
||||
impersonateAdmin();
|
||||
this.jsr250ProtectedService.permitAll();
|
||||
System.out.println("testJsr250PermitAll(): jsr250ProtectedService.permitAll() worked as admin");
|
||||
}
|
||||
|
||||
private void impersonateAnonymous() {
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
}
|
||||
|
||||
private void impersonateUser() {
|
||||
UserDetails user = this.userDetailsManager.loadUserByUsername("user");
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new TestingAuthenticationToken(user, user.getPassword(), List.copyOf(user.getAuthorities())));
|
||||
}
|
||||
|
||||
private void impersonateAdmin() {
|
||||
UserDetails user = this.userDetailsManager.loadUserByUsername("admin");
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new TestingAuthenticationToken(user, user.getPassword(), List.copyOf(user.getAuthorities())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import jakarta.annotation.security.DenyAll;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class Jsr250ProtectedService {
|
||||
|
||||
public void anonymous() {
|
||||
System.out.println("anonymous()");
|
||||
}
|
||||
|
||||
@PermitAll
|
||||
public void permitAll() {
|
||||
System.out.println("permitAll()");
|
||||
}
|
||||
|
||||
@DenyAll
|
||||
public void denyAll() {
|
||||
System.out.println("denyAll()");
|
||||
}
|
||||
|
||||
@RolesAllowed("USER")
|
||||
public void user() {
|
||||
System.out.println("user()");
|
||||
}
|
||||
|
||||
@RolesAllowed("ADMIN")
|
||||
public void admin() {
|
||||
System.out.println("admin()");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import org.springframework.security.access.prepost.PostAuthorize;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PostAuthorizeProtectedService {
|
||||
|
||||
public void anonymous() {
|
||||
System.out.println("anonymous()");
|
||||
}
|
||||
|
||||
@PostAuthorize("returnObject == authentication?.name")
|
||||
public String user() {
|
||||
System.out.println("user()");
|
||||
return "user";
|
||||
}
|
||||
|
||||
@PostAuthorize("returnObject == authentication?.name")
|
||||
public String admin() {
|
||||
System.out.println("admin()");
|
||||
return "admin";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PreAuthorizeProtectedService {
|
||||
|
||||
public void anonymous() {
|
||||
System.out.println("anonymous()");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
public void user() {
|
||||
System.out.println("user()");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public void admin() {
|
||||
System.out.println("admin()");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SecuredProtectedService {
|
||||
|
||||
public void anonymous() {
|
||||
System.out.println("anonymous()");
|
||||
}
|
||||
|
||||
@Secured("ROLE_USER")
|
||||
public void user() {
|
||||
System.out.println("user()");
|
||||
}
|
||||
|
||||
@Secured("ROLE_ADMIN")
|
||||
public void admin() {
|
||||
System.out.println("admin()");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.provisioning.UserDetailsManager;
|
||||
|
||||
@Configuration
|
||||
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
|
||||
class SecurityConfiguration {
|
||||
|
||||
@Bean
|
||||
UserDetailsManager userDetailsManager() {
|
||||
UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER")
|
||||
.build();
|
||||
UserDetails admin = User.withDefaultPasswordEncoder().username("admin").password("password").roles("ADMIN")
|
||||
.build();
|
||||
|
||||
return new InMemoryUserDetailsManager(user, admin);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.security.method;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SecurityMethodApplication {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
SpringApplication.run(SecurityMethodApplication.class, args);
|
||||
Thread.currentThread().join(); // To be able to measure memory consumption
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user