package com.mtf.vega.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mtf.vega.cache.Cache;
import com.mtf.vega.cache.CacheManager;
import com.mtf.vega.dao.ResourceMapper;
import com.mtf.vega.dao.Role2ResourceMapper;
import com.mtf.vega.model.Resource;
import com.mtf.vega.model.UserResource;
import com.mtf.vega.service.ISecurityService;
import com.mtf.vega.util.Constants;
import com.mtf.vega.util.SessionInfo;
@Service("sercurityService")
public class SecurityServiceImpl extends BaseService implements ISecurityService {
@Autowired
private Role2ResourceMapper role2resourceMapper;
@Autowired
private ResourceMapper resourceMapper;
/**
* 刷新资源
*/
public synchronized void refreshResources() {
CacheManager.clear(Constants.CACHE_RESOURCE);
}
@Override
public int getPassCode(String uri, SessionInfo sessionInfo) {
Map<String, Resource> map = null;
// step 1 : check resources list
Cache cache = CacheManager.get(Constants.CACHE_RESOURCE);
if (cache != null) {
map = (Map<String, Resource>) cache.getValue();
}
if (map == null) {
map = new HashMap<>();
synchronized (this) {
List<Resource> list = this.resourceMapper.select(null, null);
for (Resource res : list) {
map.put(res.getUri(), res);
}
// add to cache
cache = new Cache();
cache.setValue(map);
CacheManager.add(Constants.CACHE_RESOURCE, cache);
}
}
Resource resource = map.get(uri);
// 资源未定义
if (resource == null) {
return 1;
}
// 特殊授权
List<UserResource> userResourceList = sessionInfo.getResourceList();
if (userResourceList != null && !userResourceList.isEmpty()) {
for (UserResource ur : userResourceList) {
if (ur.getResourceId().equalsIgnoreCase(resource.getId())) {
// 特别拒绝
if (ur.getAllow() == 0) {
return 2;
}
// 特别允许
else if (ur.getAllow() == 1) {
return 0;
}
}
}
}
// 角色授权
if (sessionInfo.getRoleIdList() == null || sessionInfo.getRoleIdList().isEmpty()) {
return 3; // 未授权
}
int res = this.role2resourceMapper.countBySecurityCheck(resource.getId(), sessionInfo.getRoleIdList());
if (res == 0) {
return 3;// 未授权
}
return 0; // OK
}
}
SecurityServiceImpl
最新推荐文章于 2021-04-22 15:01:01 发布