shiro (七) spring结合 -- 针对一个系统多角色单立各自首页——加密
?
package com.miv.shiro.common;
?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
?
import com.miv.core.constant.DatabaseConstants;
?
public class ShiroEncryption {
?
? ? public static Map<String, String[]> map = new HashMap<String, String[]>();
? ? public static final String[] ADMIN = { "admin", "animd", "nimda" };
? ? public static final String[] CALLCENTER = { "call", "callCenter", "callcenter" };
? ? public static final String[] AGENCY = { "agency", "acygen", "yacgen" };
? ? public static final String[] USER = { "user", "seru", "ures" };
? ? static {
? ? ? ? map.put(DatabaseConstants.ROLE_CODE_ADMIN + "", ADMIN);
? ? ? ? map.put(DatabaseConstants.ROLE_CODE_CALL_CENTER + "", CALLCENTER);
? ? ? ? map.put(DatabaseConstants.ROLE_CODE_AGENCY + "", AGENCY);
? ? ? ? map.put(DatabaseConstants.ROLE_CODE_USER + "", USER);
? ? }
? ? public static final boolean SUCCESS = true;
? ? public static final boolean FAILURE = false;
?
? ? // 加密
? ? public static String encryption(Integer ROLE_CODE) throws Exception {
? ? ? ? String[] encryption = map.get(ROLE_CODE + "");
? ? ? ? return encryption[getRandom(3)];
? ? }
?
? ? // 解密
? ? public static Integer decryption(String encryption) throws Exception {
? ? ? ? Integer ROLE_CODE = null;
? ? ? ? Iterator<String> iterator = map.keySet().iterator();
? ? ? ? decryption: while (iterator.hasNext()) {
? ? ? ? ? ? String key = iterator.next();
? ? ? ? ? ? String[] decryption = map.get(key);
? ? ? ? ? ? for (int i = 0; i < decryption.length; i++) {
? ? ? ? ? ? ? ? if (decryption[i].equalsIgnoreCase(encryption)) {
? ? ? ? ? ? ? ? ? ? ROLE_CODE = Integer.valueOf(key);
? ? ? ? ? ? ? ? ? ? break decryption;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return ROLE_CODE;
? ? }
?
? ? public static int getRandom(int divisor) {
? ? ? ? int randomI = 0;
? ? ? ? Random random = new Random();
? ? ? ? randomI = Math.abs(random.nextInt()) % divisor;
? ? ? ? return randomI;
? ? }
?
? ? public static String decryptionURL(Integer ROLE_CODE, boolean flag) {
? ? ? ? switch (ROLE_CODE.intValue()) {
? ? ? ? ? ? case 0: {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case 1: {
? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? return "adminPortal/home";
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return "login/login";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? case 2: {
? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? return "callcenterPortal/home";
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return "call_center_portal/login/login";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? case 3: {
? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? return "agencyPortal/home";
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return "agency_portal/login/login";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? case 4: {
? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? return "userPortal/home";
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return "login/login";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return "";
? ? }
?
? ? public static String decryptionURL(Integer ROLE_CODE) {
? ? ? ? switch (ROLE_CODE.intValue()) {
? ? ? ? ? ? case 0: {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case 1: {
? ? ? ? ? ? ? ? return "/adminPortal/home";
?
? ? ? ? ? ? }
? ? ? ? ? ? case 2: {
? ? ? ? ? ? ? ? return "/callcenterportal/device/active";
? ? ? ? ? ? }
? ? ? ? ? ? case 3: {
? ? ? ? ? ? ? ? return "/agencyPortal/home";
? ? ? ? ? ? }
? ? ? ? ? ? case 4: {
? ? ? ? ? ? ? ? return "/userPortal/home";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return "";
? ? }
?
? ? public static String decryptionTitle(Integer ROLE_CODE) {
? ? ? ? switch (ROLE_CODE.intValue()) {
? ? ? ? ? ? case 0: {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case 1: {
? ? ? ? ? ? ? ? return "登录页管理员";
? ? ? ? ? ? }
? ? ? ? ? ? case 2: {
? ? ? ? ? ? ? ? return "登录页客服中心";
? ? ? ? ? ? }
? ? ? ? ? ? case 3: {
? ? ? ? ? ? ? ? return "登录页经销商";
? ? ? ? ? ? }
? ? ? ? ? ? case 4: {
? ? ? ? ? ? ? ? return "登录页普通用户";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return "";
? ? }
?
? ? public static void main(String[] args) {
? ? ? ? Random random = new Random();
? ? ? ? System.out.println(random.nextInt());
? ? ? ? System.out.println(Math.abs(random.nextInt()) % 3);
? ? }
}