UserAuthLocal.java 3.89 KB
package com.ui.User;

import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.AuthModule;
import com.ui.model.req.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zhengyouwei on 2016/7/13.
 * 鉴权
 */
@Component
@EnableAsync
public class UserAuthLocal {

    private static ConcurrentHashMap<String, User> usermap = new ConcurrentHashMap<>();

    private static ConcurrentHashMap<String, AuthModule> modulemap = new ConcurrentHashMap<>();

    @Autowired
    HttpRestClient httpRestClient;

    //@PostConstruct
    public synchronized void init() {

        //加载用户信息
        BaseResponse<List<User>> response = httpRestClient.exchangeForget(HttpUriContants.GET_All_USER, new ParameterizedTypeReference<BaseResponse<List<User>>>() {
        }, null);
        List<User> userlist = response.getData();
        for (User user : userlist) {
            usermap.put(user.getName(), user);
        }

        //加载mudule信息
        BaseResponse<List<AuthModule>> moduleResponse = httpRestClient.exchangeForget(HttpUriContants.GET_All_MODULE, new ParameterizedTypeReference<BaseResponse<List<AuthModule>>>() {}, null);
        List<AuthModule> modulelist = moduleResponse.getData();
        for (AuthModule module : modulelist) {
            modulemap.put(module.getModuleName(), module);
        }

    }

    /**
     * jian quan
     * @param user
     * @param module
     * @return
     */
    public boolean auth(String user,String module){
        if(usermap.isEmpty()){//改用延时加载
            init();
        }
        User u = usermap.get(user);
        if (u == null){
            return false;
        }
        AuthModule authModule = modulemap.get(module);
        if (authModule == null){
            return false;
        }
        if(u.getLevel() >= authModule.getModuleLevel()){
            return true;
        }
        return false;
    }

    /**
     * 获取用户
     *
     * @param name
     * @return
     */
    public User getUserByname(String name) {
        if(usermap.isEmpty()){//改用延时加载
            init();
        }
        return usermap.get(name);
    }

    /**
     * 获取用户
     *
     * @param name
     * @return
     */
    public AuthModule getAuthModuleByname(String name) {
        if(modulemap.isEmpty()){//改用延时加载
            init();
        }
        return modulemap.get(name);
    }

    /**
     * 更新用户信息
     *
     * @param name
     */
    @Async
    public void flushUser(String name) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        BaseResponse<User> response = httpRestClient.exchangeForget(HttpUriContants.GET_USER_BY_NAME, new ParameterizedTypeReference<BaseResponse<User>>() {
        }, map);
        User user = response.getData();
        if (user == null){
            usermap.remove(name);
        }else{
            usermap.put(name, user);
        }
    }

    /**
     * 更新用户信息
     *
     * @param name
     */
    @Async
    public void flushModule(String name) {
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        BaseResponse<AuthModule> response = httpRestClient.exchangeForget(HttpUriContants.MODULE_GET_BYNAME, new ParameterizedTypeReference<BaseResponse<AuthModule>>() {
        }, map);
        AuthModule authModule = response.getData();
        if (authModule == null){
            modulemap.remove(name);
        }else{
            modulemap.put(name, authModule);
        }
    }

}