| @@ -1,73 +0,0 @@ | |||||
| package org.moxianchengbao.controller; | |||||
| import org.moxianchengbao.mapper.MenuMapper; | |||||
| import org.moxianchengbao.pojo.entity.Menu; | |||||
| import org.moxianchengbao.resultR.R; | |||||
| import org.moxianchengbao.service.impl.MenuServiceImpl; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.web.bind.annotation.*; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| /** | |||||
| * @Author moxianchengbao | |||||
| * @Date 2023/8/4 15:31 | |||||
| */ | |||||
| @RestController | |||||
| @RequestMapping("/menu") | |||||
| public class menuController { | |||||
| @Autowired | |||||
| private MenuMapper menuMapper; | |||||
| @Autowired | |||||
| private MenuServiceImpl menuService; | |||||
| @GetMapping("/menuAll") | |||||
| public R menuAll(){ | |||||
| List<Menu> list = menuService.list(); | |||||
| List<Menu> parNode = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList()); | |||||
| for( Menu it : parNode){ | |||||
| it.setChildren(list.stream().filter(m->it.getId().equals(m.getPid())).collect(Collectors.toList())); | |||||
| } | |||||
| return R.SUCCESS(parNode); | |||||
| } | |||||
| @PostMapping("/save") | |||||
| public R menuSave(@RequestBody Menu item){ | |||||
| if(menuService.saveOrUpdate(item)){ | |||||
| return R.SUCCESS(); | |||||
| }else { | |||||
| return R.FAIL(); | |||||
| } | |||||
| } | |||||
| @DeleteMapping("/del/{id}") | |||||
| public R menuDelete(@PathVariable Integer id){ | |||||
| if(menuMapper.deleteById(id)==1){ | |||||
| return R.SUCCESS(); | |||||
| }else{ | |||||
| return R.FAIL(); | |||||
| } | |||||
| } | |||||
| @PostMapping("/batch/del") | |||||
| public R menuBatchDelete(@RequestBody List<Integer> ids){ | |||||
| int len = ids.size(); | |||||
| if(menuMapper.deleteBatchIds(ids)==len){ | |||||
| return R.SUCCESS(); | |||||
| }else { | |||||
| return R.FAIL(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,4 +1,4 @@ | |||||
| package org.moxianchengbao.utils; | |||||
| package org.moxianchengbao.utils; | |||||
| import com.auth0.jwt.JWT; | import com.auth0.jwt.JWT; | ||||
| import com.auth0.jwt.JWTCreator; | import com.auth0.jwt.JWTCreator; | ||||
| @@ -53,13 +53,7 @@ public class JWTUtils { | |||||
| return verify; | return verify; | ||||
| } | } | ||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/7 11:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 检验token合法性 | |||||
| * @作用 80 | |||||
| */ | |||||
| public static boolean checkToken(String token){ | public static boolean checkToken(String token){ | ||||
| boolean flag = false; | boolean flag = false; | ||||
| try{ | try{ | ||||
| @@ -71,26 +65,14 @@ public class JWTUtils { | |||||
| return flag; | return flag; | ||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/13 12:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 通过token获取用户名 | |||||
| * @作用 80 | |||||
| */ | |||||
| public static String getNameByToken(String token){ | public static String getNameByToken(String token){ | ||||
| DecodedJWT tokenInfo = getTokenInfo(token); | DecodedJWT tokenInfo = getTokenInfo(token); | ||||
| String username = tokenInfo.getClaim("username").asString(); | String username = tokenInfo.getClaim("username").asString(); | ||||
| return username; | return username; | ||||
| } | } | ||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/13 12:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 通过token获取用户id | |||||
| * @作用 80 | |||||
| */ | |||||
| public static String getIdByToken(String token){ | public static String getIdByToken(String token){ | ||||
| DecodedJWT tokenInfo = getTokenInfo(token); | DecodedJWT tokenInfo = getTokenInfo(token); | ||||
| String userid = tokenInfo.getClaim("userid").asString(); | String userid = tokenInfo.getClaim("userid").asString(); | ||||
| @@ -0,0 +1,100 @@ | |||||
| package org.moxianchengbao.utils; | |||||
| import com.auth0.jwt.JWT; | |||||
| import com.auth0.jwt.JWTCreator; | |||||
| import com.auth0.jwt.algorithms.Algorithm; | |||||
| import com.auth0.jwt.interfaces.Claim; | |||||
| import com.auth0.jwt.interfaces.DecodedJWT; | |||||
| import java.util.*; | |||||
| /** | |||||
| * JWT工具类 | |||||
| * @Author moxianchengbao | |||||
| */ | |||||
| public class JWTUtils { | |||||
| //自定义签名密钥 | |||||
| private static final String SIG = "HIUDe9%^#&dsf4%#4tr115&*&*wer6grfvewfw"; | |||||
| /** | |||||
| * 生成token | |||||
| * @param map 自定义数据 | |||||
| * @return String | |||||
| */ | |||||
| public static String getToken(Map<String,String> map){ | |||||
| Algorithm alg = Algorithm.HMAC256(SIG); //自定义签名密钥 | |||||
| Calendar ins = Calendar.getInstance(); | |||||
| ins.add(Calendar.DATE,1); //默认过期时间为1天 | |||||
| // ins.add(Calendar.SECOND,30); | |||||
| //jwt builder | |||||
| JWTCreator.Builder builder = JWT.create(); | |||||
| builder.withIssuedAt(new Date()); | |||||
| //payload | |||||
| map.forEach((k,v)->{ | |||||
| builder.withClaim(k,v); | |||||
| }); | |||||
| //signature | |||||
| String token = builder.withExpiresAt(ins.getTime()).sign(alg); | |||||
| return token; | |||||
| } | |||||
| /** | |||||
| * 获取token信息 | |||||
| * @param token | |||||
| * @return DecodedJWT | |||||
| */ | |||||
| public static DecodedJWT getTokenInfo(String token){ | |||||
| DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIG)).build().verify(token); | |||||
| return verify; | |||||
| } | |||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/7 11:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 检验token合法性 | |||||
| * @作用 80 | |||||
| */ | |||||
| public static boolean checkToken(String token){ | |||||
| boolean flag = false; | |||||
| try{ | |||||
| getTokenInfo(token); | |||||
| flag=true; | |||||
| }catch (Exception e){ | |||||
| flag=false; | |||||
| }finally { | |||||
| return flag; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/13 12:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 通过token获取用户名 | |||||
| * @作用 80 | |||||
| */ | |||||
| public static String getNameByToken(String token){ | |||||
| DecodedJWT tokenInfo = getTokenInfo(token); | |||||
| String username = tokenInfo.getClaim("username").asString(); | |||||
| return username; | |||||
| } | |||||
| /** | |||||
| * @Author DragonOne | |||||
| * @Date 2022/3/13 12:16 | |||||
| * @墨水记忆 www.tothefor.com | |||||
| * @属性 通过token获取用户id | |||||
| * @作用 80 | |||||
| */ | |||||
| public static String getIdByToken(String token){ | |||||
| DecodedJWT tokenInfo = getTokenInfo(token); | |||||
| String userid = tokenInfo.getClaim("userid").asString(); | |||||
| return userid; | |||||
| } | |||||
| } | |||||