SecurityServiceImpl

/*
 * Copyright (c) 2013 LIAONING SHIDAI_WANHENG CO.,LTD. All Rights Reserved.
 * This work contains SHIDAI_WANHENG CO.,LTD.'s unpublished
 * proprietary information which may constitute a trade secret
 * and/or be confidential. This work may be used only for the
 * purposes for which it was provided, and may not be copied
 * or disclosed to others. Copyright notice is precautionary
 * only, and does not imply publication.
 *
 */
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
}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yueyue____1/article/details/80690893
个人分类: Java_EE
想对作者说点什么? 我来说一句
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭