-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
87 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
25 changes: 15 additions & 10 deletions
25
...oot3/src/main/java/com/acme/backend/springboot/users/support/access/AccessController.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 |
---|---|---|
@@ -1,26 +1,31 @@ | ||
package com.acme.backend.springboot.users.support.access; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import java.util.function.Supplier; | ||
|
||
import org.springframework.security.authorization.AuthorizationDecision; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.web.access.intercept.RequestAuthorizationContext; | ||
|
||
import java.util.function.Supplier; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Example for generic custom access checks on request level. | ||
*/ | ||
@Slf4j | ||
public class AccessController { | ||
|
||
private static final AuthorizationDecision GRANTED = new AuthorizationDecision(true); | ||
private static final AuthorizationDecision DENIED = new AuthorizationDecision(false); | ||
|
||
public static AuthorizationDecision checkAccess(Supplier<Authentication> authentication, RequestAuthorizationContext requestContext) { | ||
private static final AuthorizationDecision GRANTED = new AuthorizationDecision(true); | ||
private static final AuthorizationDecision DENIED = new AuthorizationDecision(false); | ||
|
||
var auth = authentication.get(); | ||
log.info("Check access for username={} path={}", auth.getName(), requestContext.getRequest().getRequestURI()); | ||
public static AuthorizationDecision checkAccess(Supplier<Authentication> authentication, RequestAuthorizationContext requestContext) { | ||
|
||
return GRANTED; | ||
} | ||
var auth = authentication.get(); | ||
if (auth == null) { | ||
log.warn("Authentication provider returned null authentication"); | ||
return DENIED; | ||
} | ||
log.info("Check access for username={} path={}", auth.getName(), requestContext.getRequest().getRequestURI()); | ||
return auth.getAuthorities().stream().map(GrantedAuthority::getAuthority).filter("ROLE_ACCESS"::equals).count() > 0 ? GRANTED : DENIED; | ||
} | ||
} |
39 changes: 38 additions & 1 deletion
39
...gboot3/src/test/java/com/acme/backend/springboot/users/BackendApiSpringboot3AppTests.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 |
---|---|---|
@@ -1,13 +1,50 @@ | ||
package com.acme.backend.springboot.users; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.test.context.support.WithAnonymousUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import com.c4_soft.springaddons.security.oauth2.test.annotations.OpenIdClaims; | ||
import com.c4_soft.springaddons.security.oauth2.test.annotations.WithMockJwtAuth; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class BackendApiSpringboot3AppTests { | ||
|
||
@Autowired | ||
MockMvc api; | ||
|
||
@Test | ||
@WithAnonymousUser | ||
void givenRequestIsAnonymous_whengetUsersMe_thenUnauthorized() throws Exception { | ||
api.perform(get("/api/users/me")).andExpectAll(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
@WithMockJwtAuth(claims = @OpenIdClaims(sub = "Tonton Pirate")) | ||
void givenUserIsNotGrantedWithAccess_whengetUsersMe_thenForbidden() throws Exception { | ||
// @formatter:off | ||
api.perform(get("/api/users/me")) | ||
.andExpect(status().isForbidden()); | ||
// @formatter:on | ||
} | ||
|
||
@Test | ||
void contextLoads() { | ||
@WithMockJwtAuth(authorities = { "ROLE_ACCESS" }, claims = @OpenIdClaims(sub = "Tonton Pirate")) | ||
void givenUserIsGrantedWithAccess_whengetUsersMe_thenOk() throws Exception { | ||
// @formatter:off | ||
api.perform(get("/api/users/me")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.message", is("Hello Tonton Pirate"))); | ||
// @formatter:on | ||
} | ||
|
||
} |
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