提交 669cf6d5 编写于 作者: 寒風冷度夜雨's avatar 寒風冷度夜雨 🈴

message:宠物商城项目

desc:商品模块
author:王荣力
time:20230919
上级 4ca04b33
......@@ -8,6 +8,8 @@ import cn.youle.pet.shop.pojo.dto.product.ProductSaveDTO;
import cn.youle.pet.shop.pojo.vo.product.ProductDetailVO;
import cn.youle.pet.shop.pojo.vo.product.ProductListVO;
import cn.youle.pet.shop.service.IPetProductService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -40,7 +42,7 @@ public class PetProductController {
public JsonResponse create(@RequestBody ProductSaveDTO productSaveDTO) {
try {
String id = "";
if (productSaveDTO.getId() == null) {
if (productSaveDTO.getProductId() == null) {
id = petProductService.create(productSaveDTO);
} else {
id = petProductService.update(productSaveDTO);
......@@ -89,7 +91,7 @@ public class PetProductController {
@PostMapping("/delete")
public JsonResponse delete(@RequestBody ProductDetailDTO productDetailDTO) {
try {
petProductService.detail(productDetailDTO);
petProductService.delete(productDetailDTO);
return JsonResponse.ok();
} catch (Exception e) {
return JsonResponse.failed(e.getMessage());
......
......@@ -15,7 +15,7 @@ public class ProductSaveDTO implements Serializable {
private static final long serialVersionUID = -1229947645623374823L;
private Long id;
private Long productId;
/**
* 商品名称
......@@ -25,7 +25,7 @@ public class ProductSaveDTO implements Serializable {
/**
* 商品种类
*/
private String category;
private String type;
/**
* 商品图片
......@@ -86,4 +86,9 @@ public class ProductSaveDTO implements Serializable {
* 规格
*/
private String specifications;
/**
* 商品风格
*/
private String style;
}
......@@ -86,7 +86,7 @@ public class PetProductServiceImpl extends ServiceImpl<PetProductMapper, PetProd
@Override
public String update(ProductSaveDTO productSaveDTO) {
log.info("=========================开始更新商品获取前端输入:{}=========================", JSON.toJSONString(productSaveDTO));
PetProduct petProduct = checkProduct(productSaveDTO.getId());
PetProduct petProduct = checkProduct(productSaveDTO.getProductId());
BeanUtils.copyProperties(productSaveDTO,petProduct);
TokenVO userInfo = getUserInfo();
......
package cn.youle.pet.shop.shiro;
import cn.youle.pet.shop.exception.PetShopException;
import cn.youle.pet.shop.mapper.PetAdminsMapper;
import cn.youle.pet.shop.pojo.entity.PetAdmins;
import cn.youle.pet.shop.mapper.*;
import cn.youle.pet.shop.pojo.entity.*;
import cn.youle.pet.shop.shiro.auth.TokenVO;
import cn.youle.pet.shop.utils.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
......@@ -20,9 +20,11 @@ import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @projectName: pet-shop
......@@ -36,6 +38,18 @@ public class MyRealm extends AuthorizingRealm {
@Autowired
private PetAdminsMapper petAdminsMapper;
@Autowired
private PetRoleMapper petRoleMapper;
@Autowired
private PetPermissionMapper petPermissionMapper;
@Autowired
private PetAdminsRoleMapper petAdminsRoleMapper;
@Autowired
private PetRolePermissionMapper petRolePermissionMapper;
/**
* 授权
*
......@@ -47,14 +61,65 @@ public class MyRealm extends AuthorizingRealm {
log.info("==================开始授权==================");
// 获取登录用户信息
Subject subject = SecurityUtils.getSubject();
TokenVO tokenVO = (TokenVO) subject.getPrincipal();
if (tokenVO == null) {
throw new PetShopException("没有登录用户信息");
}
// 根据用户信息查询对应的角色
List<PetAdmins> petAdmins = petAdminsMapper.selectList(new LambdaQueryWrapper<PetAdmins>().eq(PetAdmins::getCount, tokenVO.getCount()));
if (CollectionUtils.isEmpty(petAdmins)) {
throw new PetShopException("没有对应用户信息");
}
PetAdmins admins = petAdmins.get(0);
// 根据用户id查找角色id
List<PetAdminsRole> petAdminsRoles = petAdminsRoleMapper.selectList(new LambdaQueryWrapper<PetAdminsRole>().eq(PetAdminsRole::getAdminId, admins.getId()));
if (CollectionUtils.isEmpty(petAdminsRoles)) {
throw new PetShopException("没有对应用户的角色信息");
}
// 查询所有角色id
List<Long> collect = petAdminsRoles.stream().map(PetAdminsRole::getRoleId).collect(Collectors.toList());
// 将角色添加到下面的set中
Set<String> roles = new HashSet<>();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
// 将对应的权限放入以下列表中
Set<String> permissions = new HashSet<>();
List<String> permissionIdList = new ArrayList<>();
for (Long roleId : collect) {
PetRole petRole = petRoleMapper.selectById(roleId);
if (petRole == null) {
throw new PetShopException("没有角色信息");
}
roles.add(petRole.getName());
// 根据角色id查找对应的权限信息
List<PetRolePermission> petRolePermissions = petRolePermissionMapper.selectList(new LambdaQueryWrapper<PetRolePermission>().eq(PetRolePermission::getRoleId, roleId));
if (CollectionUtils.isEmpty(petRolePermissions)) {
throw new PetShopException("没有权限信息");
}
// 获取所有权限的id集合
for (PetRolePermission petRolePermission : petRolePermissions) {
String permissionId = petRolePermission.getPermissionId();
permissionIdList.add(permissionId);
}
}
for (String permissionId : permissionIdList) {
PetPermission petPermission = petPermissionMapper.selectById(Long.valueOf(permissionId));
if (petPermission == null) {
throw new PetShopException("没有权限信息");
}
permissions.add(petPermission.getValue());
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
log.info("==================授权成功==================");
return null;
info.addStringPermissions(permissions);
info.addRoles(roles);
return info;
}
/**
......@@ -63,7 +128,7 @@ public class MyRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
log.info("==================开始认证==================");
if(authenticationToken == null){
if (authenticationToken == null) {
// 报错
throw new PetShopException("没有需要认证的数据");
}
......@@ -76,13 +141,13 @@ public class MyRealm extends AuthorizingRealm {
// 解密获取username,用于和数据库进行对比
String count = tokenVO.getCount();
List<PetAdmins> petAdmins = petAdminsMapper.selectList(new LambdaQueryWrapper<PetAdmins>().eq(PetAdmins::getCount, count));
if(CollectionUtils.isEmpty(petAdmins)){
if (CollectionUtils.isEmpty(petAdmins)) {
// 报错
throw new PetShopException("没有该用户");
}
PetAdmins admins = petAdmins.get(0);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(admins.getName(),admins.getPassword(),getName());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(admins.getName(), admins.getPassword(), getName());
log.info("==================认证成功==================");
return info;
......
......@@ -33,6 +33,7 @@ public class ShiroConfig {
map.put("/api/pet-admins/login","anon");
map.put("/api/pet-admins/register","anon");
map.put("/api/pet-admins/checkLogin","anon");
map.put("/api/pet-product/*","authc");
map.put("/common/upload","anon");
map.put("/common/download","anon");
// 对所有用户认证
......
package cn.youle.pet.shop.utils;
import cn.hutool.jwt.JWT;
import cn.youle.pet.shop.shiro.auth.TokenVO;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
......@@ -49,6 +48,9 @@ public class JWTUtil {
claims.put("password", tokenVO.getPassword());
claims.put("name", tokenVO.getName());
claims.put("weChatId", tokenVO.getWeChatId());
claims.put("nickname", tokenVO.getNickname());
claims.put("code", tokenVO.getCode());
claims.put("company", tokenVO.getCompany());
String jwt = Jwts.builder()
.setHeaderParam(Header.CONTENT_TYPE, "HS256")
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
......@@ -73,8 +75,11 @@ public class JWTUtil {
tokenVO.setPassword((String) body.get("password"));
tokenVO.setName((String) body.get("name"));
tokenVO.setWeChatId((String) body.get("weChatId"));
tokenVO.setNickname((String) body.get("nickname"));
tokenVO.setCode((String) body.get("code"));
tokenVO.setCompany((String) body.get("company"));
return tokenVO;
}catch (Exception e){
} catch (Exception e) {
log.error("解析token出错,原因是:" + e.getMessage());
return null;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册