|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.event; |
| 18 | + |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import org.springframework.context.ApplicationEvent; |
| 22 | +import org.springframework.security.authorization.AuthorizationDecision; |
| 23 | +import org.springframework.security.core.Authentication; |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * A parent class for {@link AuthorizationGrantedEvent} and |
| 28 | + * {@link AuthorizationDeniedEvent}. |
| 29 | + * |
| 30 | + * @author Josh Cummings |
| 31 | + * @since 5.8 |
| 32 | + */ |
| 33 | +public class AuthorizationEvent extends ApplicationEvent { |
| 34 | + |
| 35 | + private final Supplier<Authentication> authentication; |
| 36 | + |
| 37 | + private final AuthorizationDecision decision; |
| 38 | + |
| 39 | + /** |
| 40 | + * Construct an {@link AuthorizationEvent} |
| 41 | + * @param authentication the principal requiring access |
| 42 | + * @param object the object to which access was requested |
| 43 | + * @param decision whether authorization was granted or denied |
| 44 | + */ |
| 45 | + public AuthorizationEvent(Supplier<Authentication> authentication, Object object, AuthorizationDecision decision) { |
| 46 | + super(object); |
| 47 | + Assert.notNull(authentication, "authentication supplier cannot be null"); |
| 48 | + this.authentication = authentication; |
| 49 | + this.decision = decision; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the principal requiring access |
| 54 | + * @return the principal requiring access |
| 55 | + */ |
| 56 | + public Supplier<Authentication> getAuthentication() { |
| 57 | + return this.authentication; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the object to which access was requested |
| 62 | + * @return the object to which access was requested |
| 63 | + */ |
| 64 | + public Object getObject() { |
| 65 | + return getSource(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the response to the princpal's request |
| 70 | + * @return |
| 71 | + */ |
| 72 | + public AuthorizationDecision getAuthorizationDecision() { |
| 73 | + return this.decision; |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments