-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default Handler Resolution to Reflection-Based
Closes gh-15496
- Loading branch information
Showing
5 changed files
with
212 additions
and
2 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
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
67 changes: 67 additions & 0 deletions
67
...ngframework/security/authorization/method/ReflectiveMethodAuthorizationDeniedHandler.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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.authorization.method; | ||
|
||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.security.authorization.AuthorizationResult; | ||
|
||
final class ReflectiveMethodAuthorizationDeniedHandler implements MethodAuthorizationDeniedHandler { | ||
|
||
private final Log logger = LogFactory.getLog(getClass()); | ||
|
||
private final Class<?> targetClass; | ||
|
||
private final Class<?> managerClass; | ||
|
||
ReflectiveMethodAuthorizationDeniedHandler(Class<?> targetClass, Class<?> managerClass) { | ||
this.logger.debug( | ||
"Will attempt to instantiate handlerClass attributes using reflection since no application context was supplied to " | ||
+ managerClass); | ||
this.targetClass = targetClass; | ||
this.managerClass = managerClass; | ||
} | ||
|
||
@Override | ||
public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) { | ||
return constructMethodAuthorizationDeniedHandler().handleDeniedInvocation(methodInvocation, | ||
authorizationResult); | ||
} | ||
|
||
@Override | ||
public Object handleDeniedInvocationResult(MethodInvocationResult methodInvocationResult, | ||
AuthorizationResult authorizationResult) { | ||
return constructMethodAuthorizationDeniedHandler().handleDeniedInvocationResult(methodInvocationResult, | ||
authorizationResult); | ||
} | ||
|
||
private MethodAuthorizationDeniedHandler constructMethodAuthorizationDeniedHandler() { | ||
try { | ||
return ((MethodAuthorizationDeniedHandler) this.targetClass.getConstructor().newInstance()); | ||
} | ||
catch (Exception ex) { | ||
throw new IllegalArgumentException("Failed to construct instance of " + this.targetClass | ||
+ ". Please either add a public default constructor to the class " | ||
+ " or publish an instance of it as a Spring bean. If you publish it as a Spring bean, " | ||
+ " either add `@EnableMethodSecurity` to your configuration or " | ||
+ " provide the `ApplicationContext` directly to " + this.managerClass, ex); | ||
} | ||
} | ||
|
||
} |
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
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