|
@@ -0,0 +1,85 @@
|
|
|
+package com.gihon.sso.service.impl;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import com.gihon.common.entity.GihonRole;
|
|
|
+import com.gihon.common.properties.GihonCommonProperties;
|
|
|
+import com.gihon.common.properties.RedisConstants;
|
|
|
+import com.gihon.common.util.JacksonJsonUtils;
|
|
|
+import com.gihon.sso.entity.vo.SecurityUserInfo;
|
|
|
+import com.gihon.sso.entity.vo.TokenVal;
|
|
|
+import com.gihon.sso.entity.vo.UserInfo;
|
|
|
+import com.gihon.sso.security.GihonAuthentication;
|
|
|
+import com.gihon.sso.service.SecurityTokenService;
|
|
|
+import com.gihon.sso.service.TokenService;
|
|
|
+import com.gihon.sso.service.impl.TokenServiceImpl;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+//TODO 清理token和refreshToken 需要分开 最好TokenStore:有各种实现,类似Cas中的统一认证中心
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@ConditionalOnClass(UserDetails.class)
|
|
|
+@Service("tokenService")
|
|
|
+@EnableConfigurationProperties(GihonCommonProperties.class)
|
|
|
+public class SecurityTokenServiceImpl extends TokenServiceImpl implements SecurityTokenService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Authentication getAuthentication(Authentication authentication) {
|
|
|
+ Authentication r = null;
|
|
|
+ String token = authentication.getPrincipal().toString();
|
|
|
+ SecurityUserInfo userInfo = this.checkTokenInfo(token);
|
|
|
+ if (userInfo != null) {
|
|
|
+ r = new GihonAuthentication(authentication.getPrincipal(), authentication.getCredentials(), userInfo);
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从UserStore中获取用户基本信息 TODO 定时刷新或者AOP通知
|
|
|
+ *
|
|
|
+ * @param account
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public SecurityUserInfo getUserInfo(String account) {
|
|
|
+ String userAccount = (String) stringRedisTemplate.opsForHash().get(USER_STORE, account);
|
|
|
+ SecurityUserInfo userInfo = null;
|
|
|
+ if (StringUtils.hasText(userAccount)) {
|
|
|
+ userInfo = JacksonJsonUtils.readObject(userAccount, SecurityUserInfo.class);
|
|
|
+ } else {
|
|
|
+ UserInfo userInfos = loginUserService.queryUserByUserAccount(userAccount);
|
|
|
+ if (userInfos == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ userInfo = new SecurityUserInfo(userInfos);
|
|
|
+ // add roleList;
|
|
|
+ List<GihonRole> rl = gihonRoleService.getRoleList(userInfos.getId());
|
|
|
+ userInfo.setRoleList(rl.stream().map(r -> r.getCompanyId()+RedisConstants.SEP+r.getRoleCode()).collect(Collectors.toList()));
|
|
|
+ stringRedisTemplate.opsForHash().put(USER_STORE, account, JacksonJsonUtils.writeObject(userInfo));
|
|
|
+ }
|
|
|
+ return userInfo;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public SecurityUserInfo checkTokenInfo(String token) {
|
|
|
+ String tokenValue = stringRedisTemplate.opsForValue().get(TOKEN_PRE + token);
|
|
|
+ TokenVal tokenEntity = null;
|
|
|
+ if (StringUtils.hasText(tokenValue)) {
|
|
|
+ tokenEntity = JacksonJsonUtils.readObject(tokenValue, TokenVal.class);
|
|
|
+ if (tokenEntity != null) {
|
|
|
+ return this.getUserInfo(tokenEntity.getUserAccount());
|
|
|
+ } else {
|
|
|
+ stringRedisTemplate.delete(TOKEN_PRE + token);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|