|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package example; |
| 18 | + |
| 19 | +import org.springframework.context.annotation.Bean; |
| 20 | +import org.springframework.context.annotation.Configuration; |
| 21 | +import org.springframework.context.annotation.Profile; |
| 22 | +import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| 23 | +import org.springframework.security.core.Authentication; |
| 24 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 25 | +import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor.PrincipalResolver; |
| 26 | +import org.springframework.security.oauth2.client.web.client.RequestAttributePrincipalResolver; |
| 27 | +import org.springframework.security.oauth2.client.web.client.SecurityContextHolderPrincipalResolver; |
| 28 | + |
| 29 | +/** |
| 30 | + * Configuration for demonstrating additional strategies for resolving a {@code principal} |
| 31 | + * via the {@link PrincipalResolver}. This sample uses the following profiles to |
| 32 | + * demonstrate multiple configurations: |
| 33 | + * |
| 34 | + * <ol> |
| 35 | + * <li>{@code default} - Demonstrates the default setup with |
| 36 | + * {@link SecurityContextHolderPrincipalResolver}.</li> |
| 37 | + * <li>{@code per-request} - Demonstrates an alternate setup with |
| 38 | + * {@link RequestAttributePrincipalResolver}. Requires specifying the {@code principal} |
| 39 | + * via {@link RequestAttributePrincipalResolver#principal(Authentication)}.</li> |
| 40 | + * <li>{@code anonymous-user} - Demonstrates a custom {@link PrincipalResolver} that |
| 41 | + * statically resolves the {@code principal}.</li> |
| 42 | + * </ol> |
| 43 | + * |
| 44 | + * @author Steve Riesenberg |
| 45 | + */ |
| 46 | +public class PrincipalResolverConfiguration { |
| 47 | + |
| 48 | + @Configuration |
| 49 | + @Profile("per-request") |
| 50 | + public static class PerRequestPrincipalResolverConfiguration { |
| 51 | + |
| 52 | + @Bean |
| 53 | + public PrincipalResolver principalResolver() { |
| 54 | + return new RequestAttributePrincipalResolver(); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + @Configuration |
| 60 | + @Profile("anonymous-user") |
| 61 | + public static class AnonymousUserPrincipalResolverConfiguration { |
| 62 | + |
| 63 | + @Bean |
| 64 | + public PrincipalResolver principalResolver() { |
| 65 | + AnonymousAuthenticationToken anonymousUser = new AnonymousAuthenticationToken("anonymous", "anonymousUser", |
| 66 | + AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); |
| 67 | + return (request) -> anonymousUser; |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments