|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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 org.springframework.security.authorization; |
| 18 | + |
| 19 | +import org.springframework.security.access.hierarchicalroles.NullRoleHierarchy; |
| 20 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
| 21 | +import org.springframework.security.authentication.AuthenticationTrustResolver; |
| 22 | +import org.springframework.security.authentication.AuthenticationTrustResolverImpl; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * A factory for creating different kinds of {@link AuthorizationManager} instances. |
| 27 | + * |
| 28 | + * @param <T> the type of object that the authorization check is being done on |
| 29 | + * @author Steve Riesenberg |
| 30 | + * @since 7.0 |
| 31 | + */ |
| 32 | +public final class DefaultAuthorizationManagerFactory<T> implements AuthorizationManagerFactory<T> { |
| 33 | + |
| 34 | + private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); |
| 35 | + |
| 36 | + private RoleHierarchy roleHierarchy = new NullRoleHierarchy(); |
| 37 | + |
| 38 | + private String rolePrefix = "ROLE_"; |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns the {@link AuthenticationTrustResolver} used to check the user's |
| 42 | + * authentication. |
| 43 | + * @return the {@link AuthenticationTrustResolver} |
| 44 | + */ |
| 45 | + public AuthenticationTrustResolver getTrustResolver() { |
| 46 | + return this.trustResolver; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets the {@link AuthenticationTrustResolver} used to check the user's |
| 51 | + * authentication. |
| 52 | + * @param trustResolver the {@link AuthenticationTrustResolver} to use |
| 53 | + */ |
| 54 | + public void setTrustResolver(AuthenticationTrustResolver trustResolver) { |
| 55 | + Assert.notNull(trustResolver, "trustResolver cannot be null"); |
| 56 | + this.trustResolver = trustResolver; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the {@link RoleHierarchy} used to discover reachable authorities. |
| 61 | + * @return the {@link RoleHierarchy} |
| 62 | + */ |
| 63 | + public RoleHierarchy getRoleHierarchy() { |
| 64 | + return this.roleHierarchy; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Sets the {@link RoleHierarchy} used to discover reachable authorities. |
| 69 | + * @param roleHierarchy the {@link RoleHierarchy} to use |
| 70 | + */ |
| 71 | + public void setRoleHierarchy(RoleHierarchy roleHierarchy) { |
| 72 | + Assert.notNull(roleHierarchy, "roleHierarchy cannot be null"); |
| 73 | + this.roleHierarchy = roleHierarchy; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the prefix used to create an authority name from a role name. |
| 78 | + * @return the role prefix |
| 79 | + */ |
| 80 | + public String getRolePrefix() { |
| 81 | + return this.rolePrefix; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sets the prefix used to create an authority name from a role name. Can be an empty |
| 86 | + * string. |
| 87 | + * @param rolePrefix the role prefix to use |
| 88 | + */ |
| 89 | + public void setRolePrefix(String rolePrefix) { |
| 90 | + Assert.notNull(rolePrefix, "rolePrefix cannot be null"); |
| 91 | + this.rolePrefix = rolePrefix; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public AuthorizationManager<T> hasRole(String role) { |
| 96 | + return hasAnyRole(role); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public AuthorizationManager<T> hasAnyRole(String... roles) { |
| 101 | + return withRoleHierarchy(AuthorityAuthorizationManager.hasAnyRole(this.rolePrefix, roles)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public AuthorizationManager<T> hasAuthority(String authority) { |
| 106 | + return withRoleHierarchy(AuthorityAuthorizationManager.hasAuthority(authority)); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public AuthorizationManager<T> hasAnyAuthority(String... authorities) { |
| 111 | + return withRoleHierarchy(AuthorityAuthorizationManager.hasAnyAuthority(authorities)); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public AuthorizationManager<T> authenticated() { |
| 116 | + return withTrustResolver(AuthenticatedAuthorizationManager.authenticated()); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public AuthorizationManager<T> fullyAuthenticated() { |
| 121 | + return withTrustResolver(AuthenticatedAuthorizationManager.fullyAuthenticated()); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public AuthorizationManager<T> rememberMe() { |
| 126 | + return withTrustResolver(AuthenticatedAuthorizationManager.rememberMe()); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public AuthorizationManager<T> anonymous() { |
| 131 | + return withTrustResolver(AuthenticatedAuthorizationManager.anonymous()); |
| 132 | + } |
| 133 | + |
| 134 | + private AuthorityAuthorizationManager<T> withRoleHierarchy(AuthorityAuthorizationManager<T> authorizationManager) { |
| 135 | + authorizationManager.setRoleHierarchy(this.roleHierarchy); |
| 136 | + return authorizationManager; |
| 137 | + } |
| 138 | + |
| 139 | + private AuthenticatedAuthorizationManager<T> withTrustResolver( |
| 140 | + AuthenticatedAuthorizationManager<T> authorizationManager) { |
| 141 | + authorizationManager.setTrustResolver(this.trustResolver); |
| 142 | + return authorizationManager; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments