Compare commits

..
3 Commits
Author SHA1 Message Date
wonder 5f770d69a8 🔄Update: 构建切面类 2025-10-10 23:39:36 +08:00
wonder 8a7d39e533 🔄Update: 搭建 AOP 骨架 2025-10-10 22:50:08 +08:00
wonder 59feaf0ccd 🔄Update: 创建 product 相关模块 2025-10-10 22:28:52 +08:00
13 changed files with 275 additions and 0 deletions
+5
View File
@@ -59,5 +59,10 @@
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,18 @@
package cn.hezhaohui.pc.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义注解,实现鉴权
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PreAuthorize {
/**
* 权限标识符
*/
public String value() default "";
}
@@ -0,0 +1,53 @@
package cn.hezhaohui.pc.aspect;
import cn.hezhaohui.pc.annotation.PreAuthorize;
import cn.hezhaohui.pc.mapper.UserPermissionMapper;
import jakarta.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.List;
import static cn.hezhaohui.pc.service.UserService.loginUser;
@Component
@Aspect
public class AuthorizeAspect {
@Resource
private UserPermissionMapper userPermissionMapper;
@Pointcut("@annotation(cn.hezhaohui.pc.annotation.PreAuthorize)")
public void authorizePointCut() {
}
/**
* 对后端接口鉴权
* 1. 获取当前用户角色
* 2. 获取角色对应权限
* 3. 判断当前权限标识符是否被包含
*
* @return
*/
@Around("authorizePointCut()")
public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
List<String> permissions = userPermissionMapper.selectPermissionByUserId(loginUser.get().getId());
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String permission = method.getAnnotation(PreAuthorize.class).value();
if (permissions.contains(permission)) {
return joinPoint.proceed();
} else {
throw new RuntimeException("权限不足");
}
}
}
@@ -0,0 +1,55 @@
package cn.hezhaohui.pc.controller;
import cn.hezhaohui.pc.annotation.PreAuthorize;
import cn.hezhaohui.pc.entity.Product;
import cn.hezhaohui.pc.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/product")
@Tag(name = "商品")
public class ProductController {
@Resource
private ProductService productService;
@GetMapping
@Operation(summary = "列出所有商品")
@PreAuthorize("PRODUCT_READ")
public List<Product> listAll() {
return productService.listAll();
}
@GetMapping("/{id}")
@Operation(summary = "通过id查找商品")
@PreAuthorize("PRODUCT_READ")
public Product findById(@PathVariable Long id) {
return productService.getById(id);
}
@PostMapping
@Operation(summary = "添加商品")
@PreAuthorize("PRODUCT_CREATE")
public void add(@RequestBody Product product) {
productService.saveProduct(product);
}
@PutMapping
@Operation(summary = "更新商品")
@PreAuthorize("PRODUCT_UPDATE")
public void update(@RequestBody Product product) {
productService.updateProduct(product);
}
@DeleteMapping("/{id}")
@Operation(summary = "删除商品")
@PreAuthorize("PRODUCT_DELETE")
public void delete(@PathVariable Long id) {
productService.deleteProduct(id);
}
}
@@ -16,6 +16,12 @@ public class UserController {
@Resource
private UserService userService;
@PostMapping("/login")
@Operation(summary = "用户登录")
public String login(@RequestBody User user) {
return userService.login(user);
}
@GetMapping
@Operation(summary = "列出所有用户")
public List<User> listAll() {
@@ -0,0 +1,20 @@
package cn.hezhaohui.pc.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName("products")
public class Product {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private String description;
private Double price;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
@@ -0,0 +1,9 @@
package cn.hezhaohui.pc.mapper;
import cn.hezhaohui.pc.entity.Product;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
@@ -0,0 +1,10 @@
package cn.hezhaohui.pc.mapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserPermissionMapper {
public List<String> selectPermissionByUserId(Long userId);
}
@@ -0,0 +1,15 @@
package cn.hezhaohui.pc.service;
import cn.hezhaohui.pc.entity.Product;
import java.util.List;
public interface ProductService {
List<Product> listAll();
Product getById(Long id);
void saveProduct(Product Product);
void updateProduct(Product Product);
void deleteProduct(Long id);
}
@@ -5,10 +5,15 @@ import cn.hezhaohui.pc.entity.User;
import java.util.List;
public interface UserService {
ThreadLocal<User> loginUser = new ThreadLocal<>();
List<User> listAll();
User getById(Long id);
void saveUser(User user);
void updateUser(User user);
void deleteUser(Long id);
String login(User user);
String logout();
}
@@ -0,0 +1,47 @@
package cn.hezhaohui.pc.service.impl;
import cn.hezhaohui.pc.entity.Product;
import cn.hezhaohui.pc.mapper.ProductMapper;
import cn.hezhaohui.pc.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
@Resource
private ProductMapper productMapper;
@Override
public List<Product> listAll() {
return productMapper.selectList(null);
}
@Override
public Product getById(Long id) {
return productMapper.selectById(id);
}
@Override
public void saveProduct(Product Product) {
LocalDateTime now = LocalDateTime.now();
Product.setCreatedAt(now);
Product.setUpdatedAt(now);
productMapper.insert(Product);
}
@Override
public void updateProduct(Product Product) {
LocalDateTime now = LocalDateTime.now();
Product.setUpdatedAt(now);
productMapper.updateById(Product);
}
@Override
public void deleteProduct(Long id) {
productMapper.deleteById(id);
}
}
@@ -3,6 +3,7 @@ package cn.hezhaohui.pc.service.impl;
import cn.hezhaohui.pc.entity.User;
import cn.hezhaohui.pc.mapper.UserMapper;
import cn.hezhaohui.pc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@@ -11,6 +12,9 @@ import java.util.List;
@Service
public class UserServiceImpl implements UserService {
ThreadLocal<User> loginUser = new ThreadLocal<>();
@Resource
private UserMapper userMapper;
@@ -43,4 +47,17 @@ public class UserServiceImpl implements UserService {
public void deleteUser(Long id) {
userMapper.deleteById(id);
}
@Override
public String login(User user) {
User result = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
loginUser.set(result);
return "Hello, " + result.getUsername() + "!";
}
@Override
public String logout() {
loginUser.remove();
return "Good bye!";
}
}
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.hezhaohui.pc.mapper.UserPermissionMapper">
<select id="selectPermissionByUserId" parameterType="Long" resultType="java.lang.String">
select distinct p.name
from permissions p
left join role_permissions rp on p.id = rp.permission_id
left join user_roles ur on rp.role_id = ur.role_id
where ur.user_id = #{userId}
</select>
</mapper>