SecurityServiceImpl


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
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值