|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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 | + * http://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 | +package org.springframework.security.web.bind.support; |
| 17 | + |
| 18 | +import org.springframework.core.MethodParameter; |
| 19 | +import org.springframework.core.annotation.AnnotationUtils; |
| 20 | +import org.springframework.expression.BeanResolver; |
| 21 | +import org.springframework.expression.Expression; |
| 22 | +import org.springframework.expression.ExpressionParser; |
| 23 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 24 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 25 | +import org.springframework.security.core.annotation.CurrentSecurityContext; |
| 26 | +import org.springframework.security.core.context.SecurityContext; |
| 27 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 28 | +import org.springframework.stereotype.Controller; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | +import org.springframework.web.bind.support.WebDataBinderFactory; |
| 31 | +import org.springframework.web.context.request.NativeWebRequest; |
| 32 | +import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
| 33 | +import org.springframework.web.method.support.ModelAndViewContainer; |
| 34 | + |
| 35 | +import java.lang.annotation.Annotation; |
| 36 | + |
| 37 | +/** |
| 38 | + * Allows resolving the {@link SecurityContext} using the |
| 39 | + * {@link CurrentSecurityContext} annotation. For example, the following |
| 40 | + * {@link Controller}: |
| 41 | + * |
| 42 | + * <pre> |
| 43 | + * @Controller |
| 44 | + * public class MyController { |
| 45 | + * @RequestMapping("/im") |
| 46 | + * public void security(@CurrentSecurityContext SecurityContext context) { |
| 47 | + * // do something with context |
| 48 | + * } |
| 49 | + * } |
| 50 | + * </pre> |
| 51 | + * |
| 52 | + * it can also support the spring SPEL expression to get the value from SecurityContext |
| 53 | + * <pre> |
| 54 | + * @Controller |
| 55 | + * public class MyController { |
| 56 | + * @RequestMapping("/im") |
| 57 | + * public void security(@CurrentSecurityContext(expression="authentication") Authentication authentication) { |
| 58 | + * // do something with context |
| 59 | + * } |
| 60 | + * } |
| 61 | + * </pre> |
| 62 | + * |
| 63 | + * <p> |
| 64 | + * Will resolve the SecurityContext argument using {@link SecurityContextHolder#getContext()} from |
| 65 | + * the {@link SecurityContextHolder}. If the {@link SecurityContext} is null, it will return null. |
| 66 | + * If the types do not match, null will be returned unless |
| 67 | + * {@link CurrentSecurityContext#errorOnInvalidType()} is true in which case a |
| 68 | + * {@link ClassCastException} will be thrown. |
| 69 | + * </p> |
| 70 | + * |
| 71 | + * @author Dan Zheng |
| 72 | + * @since 5.2.x |
| 73 | + */ |
| 74 | +public final class CurrentSecurityContextArgumentResolver |
| 75 | + implements HandlerMethodArgumentResolver { |
| 76 | + |
| 77 | + private ExpressionParser parser = new SpelExpressionParser(); |
| 78 | + |
| 79 | + private BeanResolver beanResolver; |
| 80 | + /** |
| 81 | + * check if this argument resolve can support the parameter. |
| 82 | + * @param parameter the method parameter. |
| 83 | + * @return true = it can support parameter. |
| 84 | + * |
| 85 | + * @see |
| 86 | + * org.springframework.web.method.support.HandlerMethodArgumentResolver# |
| 87 | + * supportsParameter(org.springframework.core.MethodParameter) |
| 88 | + */ |
| 89 | + public boolean supportsParameter(MethodParameter parameter) { |
| 90 | + return findMethodAnnotation(CurrentSecurityContext.class, parameter) != null; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * resolve the argument to inject into the controller parameter. |
| 95 | + * @param parameter the method parameter. |
| 96 | + * @param mavContainer the model and view container. |
| 97 | + * @param webRequest the web request. |
| 98 | + * @param binderFactory the web data binder factory. |
| 99 | + * |
| 100 | + * @see org.springframework.web.method.support.HandlerMethodArgumentResolver# |
| 101 | + * resolveArgument (org.springframework.core.MethodParameter, |
| 102 | + * org.springframework.web.method.support.ModelAndViewContainer, |
| 103 | + * org.springframework.web.context.request.NativeWebRequest, |
| 104 | + * org.springframework.web.bind.support.WebDataBinderFactory) |
| 105 | + */ |
| 106 | + public Object resolveArgument(MethodParameter parameter, |
| 107 | + ModelAndViewContainer mavContainer, NativeWebRequest webRequest, |
| 108 | + WebDataBinderFactory binderFactory) throws Exception { |
| 109 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 110 | + if (securityContext == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + Object securityContextResult = securityContext; |
| 114 | + |
| 115 | + CurrentSecurityContext securityContextAnnotation = findMethodAnnotation( |
| 116 | + CurrentSecurityContext.class, parameter); |
| 117 | + |
| 118 | + String expressionToParse = securityContextAnnotation.expression(); |
| 119 | + if (StringUtils.hasLength(expressionToParse)) { |
| 120 | + StandardEvaluationContext context = new StandardEvaluationContext(); |
| 121 | + context.setRootObject(securityContext); |
| 122 | + context.setVariable("this", securityContext); |
| 123 | + |
| 124 | + Expression expression = this.parser.parseExpression(expressionToParse); |
| 125 | + securityContextResult = expression.getValue(context); |
| 126 | + } |
| 127 | + |
| 128 | + if (securityContextResult != null |
| 129 | + && !parameter.getParameterType().isAssignableFrom(securityContextResult.getClass())) { |
| 130 | + if (securityContextAnnotation.errorOnInvalidType()) { |
| 131 | + throw new ClassCastException(securityContextResult + " is not assignable to " |
| 132 | + + parameter.getParameterType()); |
| 133 | + } |
| 134 | + else { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + return securityContextResult; |
| 139 | + } |
| 140 | + /** |
| 141 | + * Sets the {@link BeanResolver} to be used on the expressions |
| 142 | + * @param beanResolver the {@link BeanResolver} to use |
| 143 | + */ |
| 144 | + public void setBeanResolver(BeanResolver beanResolver) { |
| 145 | + this.beanResolver = beanResolver; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Obtains the specified {@link Annotation} on the specified {@link MethodParameter}. |
| 150 | + * |
| 151 | + * @param annotationClass the class of the {@link Annotation} to find on the |
| 152 | + * {@link MethodParameter} |
| 153 | + * @param parameter the {@link MethodParameter} to search for an {@link Annotation} |
| 154 | + * @return the {@link Annotation} that was found or null. |
| 155 | + */ |
| 156 | + private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, |
| 157 | + MethodParameter parameter) { |
| 158 | + T annotation = parameter.getParameterAnnotation(annotationClass); |
| 159 | + if (annotation != null) { |
| 160 | + return annotation; |
| 161 | + } |
| 162 | + Annotation[] annotationsToSearch = parameter.getParameterAnnotations(); |
| 163 | + for (Annotation toSearch : annotationsToSearch) { |
| 164 | + annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), |
| 165 | + annotationClass); |
| 166 | + if (annotation != null) { |
| 167 | + return annotation; |
| 168 | + } |
| 169 | + } |
| 170 | + return null; |
| 171 | + } |
| 172 | +} |
0 commit comments