| ¶Ô±ÈÐÂÎļþ |
| | |
| | | /** |
| | | * Copyright (C) 2018-2020 |
| | | * All rights reserved, Designed By www.yixiang.co |
| | | * 注æï¼ |
| | | * æ¬è½¯ä»¶ä¸ºwww.yixiang.coå¼åç å¶ |
| | | */ |
| | | package com.sandu.common.security; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.sandu.common.redis.online.OnlineUser; |
| | | import com.sandu.common.redis.online.OnlineUserService; |
| | | import com.sandu.common.security.config.SecurityProperties; |
| | | import com.sandu.common.security.jwt.JwtTokenProvider; |
| | | import com.sandu.common.util.RequestHolder; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | /** |
| | | * è·åå½åç»å½çç¨æ· |
| | | * |
| | | * @author Zheng Jie |
| | | * @date 2019-01-17 |
| | | */ |
| | | public class SecurityUtils { |
| | | |
| | | public static LoginUserInfo getUserDetails() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | return null; |
| | | } |
| | | if (authentication.getPrincipal() instanceof LoginUserInfo) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal(); |
| | | return loginUserInfo; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * è·åç³»ç»ç¨æ·åç§° |
| | | * |
| | | * @return ç³»ç»ç¨æ·åç§° |
| | | */ |
| | | public static String getUsername() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | return "æ ç»å½"; |
| | | } |
| | | if (authentication.getPrincipal() instanceof LoginUserInfo) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal(); |
| | | return loginUserInfo.getUsername(); |
| | | } |
| | | return "æ ç»å½"; |
| | | } |
| | | |
| | | /** |
| | | * è·åç³»ç»ç¨æ·id |
| | | * |
| | | * @return ç³»ç»ç¨æ·id |
| | | */ |
| | | public static Long getUserId() { |
| | | SecurityProperties properties = SpringContextHolder.getBean(SecurityProperties.class); |
| | | if (!properties.isCacheOnline()) { |
| | | LoginUserInfo obj = getUserDetails(); |
| | | if (obj == null) { |
| | | return null; |
| | | } |
| | | return obj.getUserId(); |
| | | } else { |
| | | HttpServletRequest httpServletRequest = RequestHolder.getHttpServletRequest(); |
| | | JwtTokenProvider tokenProvider = SpringContextHolder.getBean(JwtTokenProvider.class); |
| | | OnlineUserService onlineUserService = SpringContextHolder.getBean(OnlineUserService.class); |
| | | String username = tokenProvider.getSubjectForce(tokenProvider.getToken(httpServletRequest)); |
| | | OnlineUser one = onlineUserService.getOne(properties.getOnlineKey() + username); |
| | | if (one != null && StrUtil.equals(one.getToken(), tokenProvider.getToken(httpServletRequest))) { |
| | | return one.getUserId(); |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | | } |
| | | } |