Java tutorial
/******************************************************************************* * Copyright (c) 2015, 2016 104446930@qq.com * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.dcits.govsbu.southernbase.baseproject2.helper; import java.util.concurrent.TimeUnit; import javax.annotation.PostConstruct; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.dcits.govsbu.southernbase.baseproject2.orm.model.Account; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; /** * Title:AuthorityHelper.java * Description:?? * Company: DigitalChina 2016 * @author: * @date:2016125 ?9:04:24 * pickles???? * ??? account.groudId * ????????? * ??? account.id?account.groudId 1 * ?account.groudId 2 * * ?Sping Bean? */ @Component public class AuthorityHelper { private static Logger logger = LoggerFactory.getLogger(AuthorityHelper.class); // ? 30 30X60=1800 @Value("${app.loginTimeoutSecs:1800}") private int loginTimeoutSecs; // guava cache private Cache<String, Account> loginUsers; @PostConstruct public void init() { logger.debug("? " + loginTimeoutSecs); // CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(loginTimeoutSecs, TimeUnit.SECONDS).build(); loginUsers = CacheBuilder.newBuilder().maximumSize(1000) .expireAfterAccess(loginTimeoutSecs, TimeUnit.SECONDS).build(); } public void putAccount(String token, Account account) { loginUsers.put(token, account); } public Account getAccount(String token) { return loginUsers.getIfPresent(token); } public void invalidateAccount(String token) { loginUsers.invalidate(token); } /** * * @Title: isAdministrator * @Description: ?? * @param token * @return * @throws * : * :2016125 ?9:22:47 * ?? account.id?account.groudId 1 */ public boolean isAdministrator(String token) { return hasLoginCache(token, 1); } public boolean isAdministrator(Account account) { return account != null && 1 == account.getId().intValue() && 1 == account.getGroupId().intValue(); } public Account getAdministratorAccount(String token) { return getLoginAccount(token, 1); } /** * * @Title: isAdmin * @Description: ?? * @param token * @return * @throws * : * :2016125 ?9:38:47 * ?account.groudId 2 */ public boolean isAdmin(String token) { return hasLoginCache(token, 2); } public Account getAdminAccount(String token) { return getLoginAccount(token, 2); } /** * * @Title: isLogin * @Description: ? * @param token * @return * @throws * : * :2016125 ?9:25:58 * ??? */ public boolean isLogin(String token) { return hasLoginCache(token, 3); } public Account getLoginAccount(String token) { return getLoginAccount(token, 3); } /** * * @Title: hasLoginCacheAuthority * @Description: ?????? * @param token * @param type 1???2???3? * @return * @throws * : * :2016125 ?9:35:11 * ?????? */ private boolean hasLoginCache(String token, int type) { boolean bln = false; if (StringUtils.isNotBlank(token)) { Account account = loginUsers.getIfPresent(token); // ??????????? switch (type) { case 1: bln = (account != null) && (1 == account.getGroupId().intValue()) && (1 == account.getId().intValue()); break; case 2: bln = (account != null) && ((2 == account.getGroupId().intValue()) || (// ? (1 == account.getGroupId().intValue()) && (1 == account.getId().intValue()))); break; case 3: bln = account != null; break; default: break; } } return bln; } /** * * @Title: getLoginCacheAuthority * @Description: 1???2???3? * @param token * @param type * @return * @throws * : * :2016125 ?9:54:26 * ???? */ private Account getLoginAccount(String token, int type) { Account account = null; if (hasLoginCache(token, type)) { account = loginUsers.getIfPresent(token); } return account; } }