|
|
@@ -1,6 +1,7 @@
|
|
|
package com.ytpm.middle.config;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ytpm.constant.StrConstant;
|
|
|
@@ -13,7 +14,6 @@ import com.ytpm.middle.view.MiddleRoleVO;
|
|
|
import com.ytpm.middle.view.MiddleUserInfo;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
@@ -23,6 +23,7 @@ import org.springframework.security.oauth2.provider.token.UserAuthenticationConv
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.LinkedHashMap;
|
|
|
@@ -42,16 +43,16 @@ public class CustomUserAuthenticationConverter implements UserAuthenticationConv
|
|
|
new ArrayList<>(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));
|
|
|
private UserDetailsService userDetailsService;
|
|
|
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private RedisUtil redisService;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private MiddleUserMapper userMapper;
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private PermissionMapper permissionMapper;
|
|
|
|
|
|
@Override
|
|
|
public Map<String, ?> convertUserAuthentication(Authentication authentication) {
|
|
|
- Map<String, Object> response = new LinkedHashMap();
|
|
|
+ Map<String, Object> response = new LinkedHashMap<>();
|
|
|
response.put("username", authentication.getName());
|
|
|
if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
|
|
|
response.put("authorities", AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
|
|
|
@@ -63,32 +64,36 @@ public class CustomUserAuthenticationConverter implements UserAuthenticationConv
|
|
|
public Authentication extractAuthentication(Map<String, ?> map) {
|
|
|
if (map.containsKey("user_name")) {
|
|
|
String userName = (String) map.get("user_name");
|
|
|
- Object principal = map.get("principal");
|
|
|
- Collection<? extends GrantedAuthority> authorities = this.getAuthorities(map);
|
|
|
+ Object principal;
|
|
|
+ this.getAuthorities(map);
|
|
|
+ Collection<? extends GrantedAuthority> authorities;
|
|
|
MiddleUserInfo user = userMapper.getByLoginName(userName);
|
|
|
if(Objects.isNull(user)){
|
|
|
log.error("当前用户不存在,应该退出登录");
|
|
|
throw new CustomerException("用户登录失效,请重新登录!");
|
|
|
}
|
|
|
- String key = StrConstant.USER_INFO_PRE + userName ;
|
|
|
- if (redisService.hasKey(key)) {
|
|
|
+ String key = StrConstant.ADS_USER_INFO_PRE + userName ;
|
|
|
+ if (Boolean.TRUE.equals(redisService.hasKey(key))) {
|
|
|
String str = redisService.getStr(key);
|
|
|
MiddleUserInfo jwtUser = JSONObject.parseObject(str, MiddleUserInfo.class);
|
|
|
principal = jwtUser;
|
|
|
authorities = jwtUser.getAuthorities();
|
|
|
} else {
|
|
|
- List<MiddleRoleVO> roleList = new ArrayList<>();
|
|
|
+ List<MiddleRoleVO> roleList;
|
|
|
if(1==user.getSuperAdmin()){
|
|
|
roleList = userMapper.getSuperAdmin();
|
|
|
+ user.setRoleList(roleList);
|
|
|
+ user.setPermissionList(permissionMapper.queryAll(new MiddlePermissionParam()));
|
|
|
}else{
|
|
|
roleList = userMapper.getRoleList(user.getUserId());
|
|
|
+ if(CollUtil.isEmpty(roleList)){
|
|
|
+ throw new CustomerException("用户权限不足!");
|
|
|
+ }
|
|
|
+ user.setRoleList(roleList);
|
|
|
+ List<Integer> roleIdList = roleList.stream()
|
|
|
+ .map(MiddleRoleVO::getRoleId).collect(Collectors.toList());
|
|
|
+ user.setPermissionList(permissionMapper.queryAllByRoleIds(roleIdList));
|
|
|
}
|
|
|
- user.setRoleList(roleList);
|
|
|
- List<Integer> roleIdList = roleList.stream()
|
|
|
- .map(MiddleRoleVO::getRoleId).collect(Collectors.toList());
|
|
|
- user.setPermissionList(user.getSuperAdmin() == 1?
|
|
|
- permissionMapper.queryAll(new MiddlePermissionParam())
|
|
|
- : permissionMapper.queryAllByRoleIds(roleIdList));
|
|
|
MiddleUserInfo jwtUser = new MiddleUserInfo();
|
|
|
BeanUtil.copyProperties(user,jwtUser);
|
|
|
authorities = jwtUser.getAuthorities();
|
|
|
@@ -104,15 +109,13 @@ public class CustomUserAuthenticationConverter implements UserAuthenticationConv
|
|
|
/**
|
|
|
* 用户资源授权的方法重写
|
|
|
*/
|
|
|
- private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
|
|
|
- if (!map.containsKey("authorities")) {
|
|
|
- return this.defaultAuthorities;
|
|
|
- } else {
|
|
|
+ private void getAuthorities(Map<String, ?> map) {
|
|
|
+ if (map.containsKey("authorities")){
|
|
|
Object authorities = map.get("authorities");
|
|
|
if (authorities instanceof String) {
|
|
|
- return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
|
|
|
+ AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
|
|
|
} else if (authorities instanceof Collection) {
|
|
|
- return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils.collectionToCommaDelimitedString((Collection) authorities));
|
|
|
+ AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils.collectionToCommaDelimitedString((Collection<?>) authorities));
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("Authorities must be either a String or a Collection");
|
|
|
}
|