1 files added
9 files modified
| | |
| | | */ |
| | | public static final String MSG_OPERATION_FAILED = "操作失败!"; |
| | | |
| | | /* |
| | | * token无效 |
| | | * */ |
| | | public static final String MSG_TOKEN_ERROR = "token无效,请重新登陆"; |
| | | |
| | | /* |
| | | * token生成失败 |
| | | * */ |
| | | public static final String MSG_TOKEN_CREATE_ERROR = "token生成失败,请联系管理员"; |
| | | |
| | | /** |
| | | * 操作成功code |
| | | */ |
| | |
| | | */ |
| | | public static final int CODE_OPERATION_FAILED = -1; |
| | | |
| | | /** |
| | | * token校验失败 |
| | | */ |
| | | public static final int CODE_TOKEN_ERROR = -10; |
| | | |
| | | /* |
| | | * token生成失败 |
| | | * */ |
| | | public static final int CODE_TOKEN_CREATE_ERROR = -11; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.moral.exception; |
| | | |
| | | /** |
| | | * @ClassName TokenException |
| | | * @Description Token异常 |
| | | * @Author 陈凯裕 |
| | | * @Date 2021/3/11 14:54 |
| | | * @Version TODO |
| | | **/ |
| | | public class TokenException extends RuntimeException { |
| | | |
| | | /** |
| | | * 错误码 |
| | | */ |
| | | private int code; |
| | | |
| | | /** |
| | | * 错误信息 |
| | | */ |
| | | private String msg; |
| | | |
| | | /** */ |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public TokenException() { |
| | | super(); |
| | | } |
| | | |
| | | public TokenException(String msg) { |
| | | super(msg); |
| | | this.msg = msg; |
| | | } |
| | | |
| | | public TokenException(Throwable t) { |
| | | super(t); |
| | | } |
| | | |
| | | public TokenException(String msg, Throwable t) { |
| | | super(msg); |
| | | this.msg = msg; |
| | | } |
| | | |
| | | public TokenException(int code, String msg) { |
| | | super(msg); |
| | | this.code = code; |
| | | this.msg = msg; |
| | | } |
| | | |
| | | public TokenException(int code, String msg, Throwable t) { |
| | | super(msg, t); |
| | | this.code = code; |
| | | this.msg = msg; |
| | | } |
| | | |
| | | /** |
| | | * Getter method for property <tt>code</tt>. |
| | | * |
| | | * @return property value of code |
| | | */ |
| | | public int getCode() { |
| | | return code; |
| | | } |
| | | |
| | | /** |
| | | * Getter method for property <tt>msg</tt>. |
| | | * |
| | | * @return property value of msg |
| | | */ |
| | | public String getMsg() { |
| | | return msg; |
| | | } |
| | | } |
| | |
| | | package com.moral.util; |
| | | |
| | | import com.moral.constant.Constants; |
| | | import com.moral.exception.TokenException; |
| | | import com.sun.org.apache.bcel.internal.classfile.ConstantString; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | |
| | | } |
| | | |
| | | //token有效期 单位:秒 |
| | | private static final int validity_time = 60*60*24*7; |
| | | //token非法,生成错误 |
| | | public static final int error = -1; |
| | | //token过期 |
| | | public static final int timeout = -2; |
| | | //token有效,生成成功 |
| | | public static final int valid = 1; |
| | | private static final int validity_time = 60*30; |
| | | |
| | | /** |
| | | * @Description: 生成token |
| | |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static Map<String, Object> getToken(String uid, Map<String, Object> userInfo) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | public static String getToken(String uid, Map<String, Object> userInfo) { |
| | | //生成加密token |
| | | try { |
| | | //生成token |
| | | String token = TokenEncryptUtils.encoded(uid + "/" + System.currentTimeMillis() / 1000); |
| | | //查询旧的token |
| | | String oldToken = (String) redisTemplate.opsForHash().get("user_token", uid); |
| | | if(oldToken!=null) |
| | | if (oldToken != null) |
| | | redisTemplate.delete(oldToken); |
| | | //新token写入到value中 |
| | | redisTemplate.opsForValue().set(token, userInfo); |
| | | redisTemplate.expire(token, validity_time, TimeUnit.SECONDS); |
| | | //新token写入到Hash中 |
| | | redisTemplate.opsForHash().put("user_token",uid,token); |
| | | |
| | | result.put("code", valid); |
| | | result.put("token",token); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage()); |
| | | result.put("code",error); |
| | | redisTemplate.opsForHash().put("user_token", uid, token); |
| | | return token; |
| | | } catch (Exception e) { |
| | | log.error("token生成异常:"+e.getMessage()); |
| | | throw new TokenException(Constants.CODE_TOKEN_CREATE_ERROR,Constants.MSG_TOKEN_CREATE_ERROR); |
| | | } |
| | | return result; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: 校验token |
| | | * @Param: [type, token] type: 后台取值:manage 前台取值:api |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static Map<String, Object> checkToken( String token) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | public static void checkToken(String token) { |
| | | try { |
| | | String[] tokenArray = TokenEncryptUtils.decoded(token).split("/"); |
| | | //校验token是否合法 |
| | | if (tokenArray.length != 2) { |
| | | result.put("code", error); |
| | | result.put("msg", "无效的token"); |
| | | return result; |
| | | throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR); |
| | | } |
| | | //校验token是否过期 |
| | | int tokenTime = Integer.parseInt(tokenArray[1]); |
| | | if ((System.currentTimeMillis() / 1000) - tokenTime > validity_time) { |
| | | result.put("code", timeout); |
| | | result.put("msg", "登陆身份已过期,请重新登陆"); |
| | | return result; |
| | | if (!redisTemplate.hasKey(token)) { |
| | | throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR); |
| | | } |
| | | result.put("code", valid); |
| | | return result; |
| | | } catch (Exception e) { |
| | | log.error("token工具类校验token异常" + e.getMessage()); |
| | | result.put("code", error); |
| | | result.put("msg", "无效的token"); |
| | | return result; |
| | | throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR); |
| | | } |
| | | } |
| | | |
| | | //通过token获取用户信息 |
| | | /** |
| | | * @Description: 通过token获取用户信息 |
| | | * @Param: [token] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | public static Map<String, Object> getUserInfoByToken(String token) { |
| | | Map<String, Object> userInfo = (Map<String, Object>) redisTemplate.opsForValue().get(token); |
| | | if(userInfo==null) |
| | | throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR); |
| | | return userInfo; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 销毁token |
| | | * @Param: [uid, token] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | public static void destoryToken(String uid, String token) { |
| | | redisTemplate.delete("token"); |
| | | redisTemplate.opsForHash().delete("user_token", uid); |
| | | } |
| | | |
| | | /** |
| | | * @Description: token延长 |
| | | * @Param: [token] |
| | | * @return: void |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | public static void extendTokenTime(String token) { |
| | | redisTemplate.expire(token, validity_time, TimeUnit.SECONDS); |
| | | } |
| | | } |
| | |
| | | <version>1.0-SNAPSHOT</version> |
| | | <scope>compile</scope> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-devtools</artifactId> |
| | | <optional>true</optional> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistration; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | regisration.addPathPatterns("/**/**");//设置拦截路径 |
| | | regisration.excludePathPatterns(excludePath);//设置不拦截路径 |
| | | } |
| | | |
| | | @Override |
| | | public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| | | registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); |
| | | registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); |
| | | registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); |
| | | WebMvcConfigurer.super.addResourceHandlers(registry); |
| | | } |
| | | } |
| | |
| | | import com.moral.api.service.ManageAccountService; |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.util.ObjectUtils; |
| | | import io.netty.util.internal.ObjectUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Api(tags = {"后台管理"}) |
| | | @Api(tags = {"后台账户管理"}) |
| | | @RestController |
| | | @RequestMapping("/account") |
| | | public class AccountController { |
| | | @Resource |
| | | ManageAccountService accountService; |
| | | @Autowired |
| | | @Qualifier("tokenRedisTemplate") |
| | | RedisTemplate redisTemplate; |
| | | |
| | | |
| | | @PostMapping("login") |
| | | public ResultMessage login(@RequestBody Map<String,Object> paramters){ |
| | | if(!ObjectUtils.checkParamAndMap(paramters,"account","password")) |
| | | return ResultMessage.fail("参数不完整"); |
| | | public ResultMessage login(@RequestBody Map<String, Object> paramters) { |
| | | if (!ObjectUtils.checkParamAndMap(paramters, "account", "password")) |
| | | return ResultMessage.fail("参数不完整"); |
| | | |
| | | Map<String, Object> result = accountService.login(paramters); |
| | | if((int)result.get("accountId")<0) |
| | | |
| | | if ((int) result.get("accountId") < 0) |
| | | return ResultMessage.fail(result); |
| | | return ResultMessage.ok(result); |
| | | } |
| | | |
| | | |
| | | @PostMapping("logout") |
| | | public ResultMessage logout(@RequestBody Map<String, Object> paramters, HttpServletRequest request) { |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.exception.TokenException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | |
| | | @ResponseStatus(HttpStatus.OK) |
| | | public ResultMessage handleException(Exception ex) { |
| | | log.error(ex.getMessage()); |
| | | log.error(ex.getStackTrace().toString()); |
| | | ex.printStackTrace(); |
| | | return ResultMessage.fail(Constants.CODE_OPERATION_FAILED, "请求失败"); |
| | | } |
| | | |
| | |
| | | return ResultMessage.fail(Constants.CODE_OPERATION_FAILED, "请求用户数据失败"); |
| | | } |
| | | |
| | | /** |
| | | * 处理TokenException异常 |
| | | */ |
| | | @ExceptionHandler({TokenException.class}) |
| | | @ResponseBody |
| | | @ResponseStatus(HttpStatus.OK) |
| | | public ResultMessage handleTokenException(TokenException ex) { |
| | | return ResultMessage.fail(ex.getCode(),ex.getMsg()); |
| | | } |
| | | |
| | | } |
| | |
| | | * @since 2021-03-09 |
| | | */ |
| | | public interface ManageAccountService extends IService<ManageAccount> { |
| | | /** |
| | | * @Description: 登陆使用 |
| | | * @Param: [paramters] |
| | | * @return: java.util.Map<java.lang.String,java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | Map<String, Object> login(Map<String, Object> paramters); |
| | | |
| | | /** |
| | | * @Description: 注销使用 |
| | | * @Param: [paramters] |
| | | * @return: java.util.Map<java.lang.String,java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | Map<String, Object> logout(Map<String, Object> paramters); |
| | | } |
| | |
| | | @Resource |
| | | ManageMenuMapper manageMenuMapper; |
| | | |
| | | |
| | | /** |
| | | * @Description: 登陆 |
| | | * @Param: [paramters] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public Map<String, Object> login(Map<String, Object> paramters) { |
| | | Map<String,Object> result = new HashMap<>(); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | //接收参数 |
| | | String cyrpAccount = (String) paramters.get("account"); |
| | | String cyrpPassword = (String) paramters.get("password"); |
| | |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("account", account); |
| | | ManageAccount manageAccount = accountMapper.selectOne(wrapper); |
| | | if(ObjectUtils.isEmpty(manageAccount)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户不存在"); |
| | | if (ObjectUtils.isEmpty(manageAccount)) { |
| | | result.put("accountId", -1); |
| | | result.put("msg", "用户不存在"); |
| | | return result; |
| | | } |
| | | //查询是否逻辑删除 |
| | | if(manageAccount.getIsDelete().equals("1")){ |
| | | result.put("accountId",-2); |
| | | result.put("msg","用户已被封禁"); |
| | | if (manageAccount.getIsDelete().equals("1")) { |
| | | result.put("accountId", -2); |
| | | result.put("msg", "用户已被封禁"); |
| | | return result; |
| | | } |
| | | //校验密码 |
| | | if(!MD5Utils.saltMD5Verify(password,manageAccount.getPassword())){ |
| | | result.put("accountId",-3); |
| | | result.put("msg","用户名密码错误"); |
| | | if (!MD5Utils.saltMD5Verify(password, manageAccount.getPassword())) { |
| | | result.put("accountId", -3); |
| | | result.put("msg", "用户名密码错误"); |
| | | return result; |
| | | } |
| | | //查询角色 |
| | | List<ManageRole> roles = roleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | if(ObjectUtils.isEmpty(roles)){ |
| | | result.put("accountId",-4); |
| | | result.put("msg","用户尚未分配角色"); |
| | | if (ObjectUtils.isEmpty(roles)) { |
| | | result.put("accountId", -4); |
| | | result.put("msg", "用户尚未分配角色"); |
| | | return result; |
| | | } |
| | | //查询菜单 |
| | | List<ManageMenu> menus = manageMenuMapper.getParentChildrenMenusByRoles(roles); |
| | | if(ObjectUtils.isEmpty(menus)){ |
| | | result.put("accountId",-5); |
| | | result.put("msg","用户尚未分配菜单"); |
| | | if (ObjectUtils.isEmpty(menus)) { |
| | | result.put("accountId", -5); |
| | | result.put("msg", "用户尚未分配菜单"); |
| | | return result; |
| | | } |
| | | |
| | | //获取用户token,并且将基本信息存入缓存 |
| | | Map<String,Object> userInfo = new HashMap<>();//需要保存在缓存中用户的数据 |
| | | userInfo.put("accountId",manageAccount.getId());//用户Id |
| | | userInfo.put("userName",manageAccount.getUserName());//用户名称 |
| | | userInfo.put("roles",roles);//用户角色 |
| | | userInfo.put("menus",menus);//用户菜单 |
| | | Map<String, Object> tokenResult = TokenUtils.getToken(String.valueOf(manageAccount.getId()), userInfo); |
| | | if(tokenResult.get("code").equals(TokenUtils.error)){ |
| | | result.put("accountId",-6); |
| | | result.put("msg","生成token错误"); |
| | | return result; |
| | | } |
| | | Map<String, Object> userInfo = new HashMap<>();//需要保存在缓存中用户的数据 |
| | | userInfo.put("accountId", manageAccount.getId());//用户Id |
| | | userInfo.put("userName", manageAccount.getUserName());//用户名称 |
| | | userInfo.put("roles", roles);//用户角色 |
| | | userInfo.put("menus", menus);//用户菜单 |
| | | String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), userInfo); |
| | | |
| | | |
| | | //打包返回信息 |
| | | result.put("accountId",manageAccount.getId());//用户Id |
| | | result.put("userName",manageAccount.getUserName());//用户名称 |
| | | result.put("roles",roles);//用户角色 |
| | | result.put("menus",menus);//用户菜单 |
| | | result.put("token",tokenResult.get("token")); |
| | | result.put("accountId", manageAccount.getId());//用户Id |
| | | result.put("userName", manageAccount.getUserName());//用户名称 |
| | | result.put("roles", roles);//用户角色 |
| | | result.put("menus", menus);//用户菜单 |
| | | result.put("token", token); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 注销 |
| | | * @Param: [paramters] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/11 |
| | | */ |
| | | @Override |
| | | public Map<String, Object> logout(Map<String, Object> paramters) { |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | interceptor: |
| | | exclude: |
| | | - /account/login |
| | | - /swagger-ui.html |
| | | - /swagger-resources/** |
| | | - /webjars/** |
| | | - /account/logout |
| | | AES: |
| | | KEY: |
| | | AD42F7787B035B7580000EF93BE20BAD |