| | |
| | | package com.xcong.excoin.common; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xcong.excoin.common.contants.AppContants; |
| | | import com.xcong.excoin.common.exception.GlobalException; |
| | | import com.xcong.excoin.modules.member.entity.MemberEntity; |
| | | import com.xcong.excoin.utils.RedisUtils; |
| | | import com.xcong.excoin.utils.SpringContextHolder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.http.HttpRequest; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.ArrayList; |
| | | |
| | | /** |
| | | * 登陆用户工具类 |
| | |
| | | * @author wzy |
| | | * @date 2020-05-14 |
| | | **/ |
| | | @Slf4j |
| | | public class LoginUserUtils { |
| | | |
| | | private static final String ANON = "anonymousUser"; |
| | | |
| | | public static MemberEntity getAppLoginUser() { |
| | | return (MemberEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| | | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(ANON)) { |
| | | throw new GlobalException("无法获取登陆信息"); |
| | | } else { |
| | | return (MemberEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| | | } |
| | | } |
| | | |
| | | public static String getAppLoginUserToken() { |
| | | return (String) SecurityContextHolder.getContext().getAuthentication().getCredentials(); |
| | | } |
| | | |
| | | public static void resetAppLoginUser(MemberEntity memberEntity) { |
| | | String token = getAppLoginUserToken(); |
| | | RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class); |
| | | String jsonStr = redisUtils.getString(AppContants.PC_LOGIN_PREFIX + token); |
| | | if (StrUtil.isNotBlank(jsonStr)) { |
| | | redisUtils.set(AppContants.PC_LOGIN_PREFIX + token, JSONObject.toJSONString(memberEntity)); |
| | | } else { |
| | | redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(memberEntity)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * mybatis 拦截器专用 |
| | | * |
| | | * @return MemberEntity |
| | | */ |
| | | public static MemberEntity getUser() { |
| | | if (SecurityContextHolder.getContext().getAuthentication() == null) { |
| | | return null; |
| | | } |
| | | |
| | | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(ANON)) { |
| | | return null; |
| | | } else { |
| | | return (MemberEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| | | } |
| | | } |
| | | |
| | | public static boolean isBrowser(HttpServletRequest request) { |
| | | String userAgent = request.getHeader("user-agent"); |
| | | if (userAgent.toLowerCase().contains("mobile") || userAgent.contains("CFNetwork") || userAgent.toLowerCase().contains("okhttp")) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | } |