|
| 1 | +/* |
| 2 | + * Copyright 2020 ChenJun ([email protected]) |
| 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 | + |
| 17 | +package com.power4j.flygon.common.security.listener; |
| 18 | + |
| 19 | +import com.power4j.flygon.common.core.constant.CommonConstant; |
| 20 | +import com.power4j.flygon.common.core.context.RequestContext; |
| 21 | +import com.power4j.flygon.common.security.LoginUser; |
| 22 | +import com.power4j.flygon.common.security.model.ApiToken; |
| 23 | +import com.power4j.flygon.common.security.token.ApiTokenAuthentication; |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | +import org.slf4j.MDC; |
| 26 | +import org.springframework.beans.factory.ObjectProvider; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.ApplicationListener; |
| 29 | +import org.springframework.security.authentication.event.AuthenticationSuccessEvent; |
| 30 | +import org.springframework.security.core.Authentication; |
| 31 | +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; |
| 32 | + |
| 33 | +import java.util.Optional; |
| 34 | + |
| 35 | +/** |
| 36 | + |
| 37 | + * @date 2021/1/17 |
| 38 | + * @since 1.0 |
| 39 | + */ |
| 40 | +@RequiredArgsConstructor |
| 41 | +public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { |
| 42 | + |
| 43 | + private final RequestContext requestContext; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private ObjectProvider<AuthenticationSuccessHandler> authenticationSuccessHandlerObjectProvider; |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onApplicationEvent(AuthenticationSuccessEvent event) { |
| 50 | + Authentication authentication = event.getAuthentication(); |
| 51 | + updateContext(authentication); |
| 52 | + authenticationSuccessHandlerObjectProvider.stream().forEach(h -> h.handleAuthenticationSuccess(authentication)); |
| 53 | + } |
| 54 | + |
| 55 | + private void updateContext(Authentication authentication) { |
| 56 | + String uid = null; |
| 57 | + if (authentication instanceof LoginUser) { |
| 58 | + uid = ((LoginUser) authentication).getUid().toString(); |
| 59 | + } |
| 60 | + else if (authentication instanceof PreAuthenticatedAuthenticationToken) { |
| 61 | + uid = Optional.ofNullable(authentication.getDetails()).filter(o -> o instanceof ApiToken) |
| 62 | + .map(o -> ((ApiToken) o).getUuid().toString()).orElse(null); |
| 63 | + } |
| 64 | + if (uid != null) { |
| 65 | + requestContext.setAccountId(uid); |
| 66 | + MDC.put(CommonConstant.MDC_ACCOUNT_ID, uid); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments