|
| 1 | +package com.github.ahunigel.test.security; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.core.annotation.AnnotationUtils; |
| 6 | +import org.springframework.security.authentication.AbstractAuthenticationToken; |
| 7 | +import org.springframework.security.core.Authentication; |
| 8 | +import org.springframework.security.test.context.TestSecurityContextHolder; |
| 9 | +import org.springframework.test.context.TestContext; |
| 10 | +import org.springframework.test.context.support.AbstractTestExecutionListener; |
| 11 | + |
| 12 | +import java.lang.reflect.AnnotatedElement; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by Nigel.Zheng on 8/3/2018. |
| 20 | + */ |
| 21 | +public class AttachClaimsTestExecutionListener extends AbstractTestExecutionListener { |
| 22 | + private static final Logger logger = LoggerFactory.getLogger(AttachClaimsTestExecutionListener.class); |
| 23 | + |
| 24 | + @Override |
| 25 | + public void beforeTestClass(TestContext testContext) throws Exception { |
| 26 | + AttachClaims annotation = findAnnotation(testContext.getTestClass()); |
| 27 | + |
| 28 | + if (annotation != null) { |
| 29 | + attachClaimsToAuthentication(annotation); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void beforeTestMethod(TestContext testContext) throws Exception { |
| 35 | + AttachClaims annotation = findAnnotation(testContext.getTestMethod()); |
| 36 | + |
| 37 | + if (annotation != null) { |
| 38 | + attachClaimsToAuthentication(annotation); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private AttachClaims findAnnotation(AnnotatedElement annotated) { |
| 43 | + return AnnotationUtils.findAnnotation(annotated, AttachClaims.class); |
| 44 | + } |
| 45 | + |
| 46 | + public void attachClaimsToAuthentication(AttachClaims annotation) { |
| 47 | + Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication(); |
| 48 | + if (authentication != null && authentication instanceof AbstractAuthenticationToken) { |
| 49 | + Map<String, String> claims = Arrays.stream(annotation.value()).collect(Collectors.toMap(Claim::name, Claim::value)); |
| 50 | + Map<String, String> stringMap = toMap(annotation.claims()); |
| 51 | + stringMap.entrySet().stream().forEach(entry -> claims.putIfAbsent(entry.getKey(), entry.getValue())); |
| 52 | + if (!claims.isEmpty()) { |
| 53 | + ((AbstractAuthenticationToken) authentication).setDetails(claims); |
| 54 | + } |
| 55 | + } else { |
| 56 | + logger.warn("No authentication found, do not attach claims"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private Map<String, String> toMap(String[] claims) { |
| 61 | + final Map<String, String> map = new HashMap<>(); |
| 62 | + for (int i = 0; i + 1 < claims.length; i += 2) { |
| 63 | + map.put(claims[i], claims[i + 1]); |
| 64 | + } |
| 65 | + return map; |
| 66 | + } |
| 67 | +} |
0 commit comments