Java tutorial
/******************************************************************************* * Copyright (c) 2015, 2016 104446930@qq.com * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.zhangmz.pickles.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 org.zhangmz.pickles.orm.model.Account; import org.zhangmz.pickles.orm.model.Enduser; 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; private Cache<String, Enduser> loginEndusers; @PostConstruct public void init() { logger.debug("? " + loginTimeoutSecs); // expireAfterWrite ?expireAfterAccess ? // CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(loginTimeoutSecs, TimeUnit.SECONDS).build(); loginUsers = CacheBuilder.newBuilder().maximumSize(1000) .expireAfterAccess(loginTimeoutSecs, TimeUnit.SECONDS).build(); loginEndusers = 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); } public void putEnduser(String token, Enduser enduser) { loginEndusers.put(token, enduser); } public Enduser getEnduser(String token) { return loginEndusers.getIfPresent(token); } public void invalidateEnduser(String token) { loginEndusers.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); } public boolean isLogin(String token) { return this.isLogin(token, 1); } /** * * @Title: isLogin * @Description: ? * @param token * @param type 1?2 * @return * @throws * : * :2016125 ?9:25:58 * ??? */ public boolean isLogin(String token, int type) { boolean bln = false; switch (type) { case 1: bln = hasLoginCache(token, 3); break; case 2: bln = (null != loginEndusers.getIfPresent(token)); break; default: break; } return bln; } public Account getLoginAccount(String token) { return getLoginAccount(token, 3); } /** * * @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; } /** * * @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; } }