java SHA算法使用common包
org.apache.commons.codec.digest.DigestUtils
public static String encodePassword(String psw) {
if(StringUtils.isEmpty(psw)){
return null;
}else{
return DigestUtils.sha1Hex(psw);
}
}
try {
String encode = URLEncoder.encode("123", "unicode").toUpperCase();
return DigestUtils.sha1Hex(encode);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
}
return null;
layuimini的json生成
后台使用简单的用户权限系统
package com.foxconn.iqc3dmmt.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.foxconn.iqc3dmmt.service.RoleService;
@Controller
public class RoleController {
@Autowired
RoleService roleService;
/**
* @description 根據角色獲取菜單
* @return 返回對應角色的菜單的json串
*/
@GetMapping("/getMenu")
@ResponseBody
public Map<String,Object> getMenuByJson(HttpServletRequest request){
@SuppressWarnings("unchecked")
Map<String,Object> user = (Map<String,Object>)request.getSession().getAttribute("user");
String id = user.get("ID")+"";
//數據庫回傳數據
List<Map<String,Object>> list = roleService.getMenu(id);
// Map<String,Object> test1 = new HashMap<>();
// test1.put("PARENT_MENU_ID", "0");
// test1.put("title", "任務總管");
// test1.put("href", "page/task.html");
// test1.put("icon", "#");
// test1.put("ID", "1");
// Map<String,Object> test2 = new HashMap<>();
// test2.put("PARENT_MENU_ID", "1");
// test2.put("title", "任務總管1");
// test2.put("href", "page/task.html");
// test2.put("icon", "#");
// test2.put("ID", "2");
// Map<String,Object> test3 = new HashMap<>();
// test3.put("PARENT_MENU_ID", "2");
// test3.put("title", "任務總管2");
// test3.put("href", "page/task.html");
// test3.put("icon", "#");
// test3.put("ID", "3");
// list.add(test1);
// list.add(test2);
// list.add(test3);
//回傳的Map
Map<String,Object> returnMap = new HashMap<>();
//回傳Map的一級菜單
Map<String,Object> menuInfo = new HashMap<>();
//回傳Map的二級菜單
Map<String,Object> currency = new HashMap<>();
//排序菜單(除了ID相關的內容,用AS變為別名)
for (int i = 0;i < list.size();i++) {
//獲取父ID不是0的菜單(子菜單)
int parentId = Integer.parseInt(list.get(i).get("PARENT_MENU_ID")+"");
if(parentId != 0) {
//創建新的子菜單的List
List<Map<String,Object>> childList = new ArrayList<Map<String,Object>>();
for (int j = 0;j < list.size();j++) {
if(Integer.parseInt(list.get(j).get("ID")+"") == parentId) {
//如果是第一次則創建子菜單的List
if(list.get(j).get("child") == null) {
list.get(j).put("child", childList);
}
//將子菜單放入父菜單中
@SuppressWarnings("unchecked")
List<Map<String,Object>> cList = (List<Map<String,Object>>)list.get(j).get("child");
cList.add(list.get(i));
}
//菜單放進去前要整理格式(去掉ID相關內容)
}
}
}
System.out.println(list);
//刪除多餘子菜單
for (int i = 0;i < list.size();i++) {
int parentId = Integer.parseInt(list.get(i).get("PARENT_MENU_ID")+"");
if(parentId != 0) {
list.remove(i);
i--;
}
}
currency.put("child", list);
menuInfo.put("currency",currency);
returnMap.put("menuInfo",menuInfo);
return returnMap;
}
}