👋 欢迎浏览我的技术博客,记录学习笔记 本人28届软工一枚

9.12 密码加密 JPA条件查询

一.编写安全配置类 SecurityConfig

1.我为什么要费劲去编写这个类?

我将使用的BCryptPasswordEncoder在这个Spring Security6体系中,我如果使用这个加密而不配置一下的话,就会出许多Bug

2.我要在这个类配置什么?

作为一个配置类,我们应对其注入BCryptPasswordEncoder对象,并配置安全过滤器链。
注入时采用接口编程思想,也是使用父类注册,但是new的时候new的是子类,我这么做的好处就是在编码时如果我仅需要需改加密算法,那我只需要将return的子类改变一下就行

@Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

然后就是第二个需要配置的:csrf防护()
(1)这是干啥的呢?
用于前后端不分离的项目,应对后端用cookie和Session做登录验证且浏览器自动将cookie带到同域名请求
(2)我为什么要关掉?为何不需要
开启 CSRF 时,Spring Security 会强制要求所有 POST/PUT/DELETE 请求必须携带 CSRF 令牌,否则直接返回 403。作为一个前后端项目,前端不具备思考能力(执行业务能力),仅用于挂载数据,因此无处获取该令牌,所以关掉

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(csrf->csrf.disable())
                .authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
        return http.build();
    }
}

二.jpa条件查询:

1.jpa的repository类上在原先继承JpaRepository<Goods, Long>的基础上再继承JpaSpecificationExecutor

@Repository
public interface GoodsRepository extends JpaRepository<Goods, Long>, JpaSpecificationExecutor<Goods> {
    Page<Goods> findByMerchantId(long merchantId, Pageable pageable);


}

2.设计特定DTO专门用来查询,要包括查询条件,例如页码和页的size,关键词及相关字段的最大最小值状态等

3.service中构造动态查询:

Specification<Goods> spec = (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // ✅ 固定条件1:软删除,永远只查 deleted=0
            predicateList.add(cb.equal(root.get("deleted"), 0));

            // ✅ 动态条件,参数不为null才拼接
            if (queryDTO.getCategory() != null) {
                predicateList.add(cb.equal(root.get("category"), queryDTO.getCategory()));
            }
            if (queryDTO.getKeyword() != null && !queryDTO.getKeyword().isEmpty()) {
                // goodsName 模糊匹配
                predicateList.add(cb.like(root.get("goodsName"), "%" + queryDTO.getKeyword() + "%"));
            }
            if (queryDTO.getMinPrice() != null) {
                predicateList.add(cb.greaterThanOrEqualTo(root.get("goodsPrice"), queryDTO.getMinPrice()));
            }
            if (queryDTO.getMaxPrice() != null) {
                predicateList.add(cb.lessThanOrEqualTo(root.get("goodsPrice"), queryDTO.getMaxPrice()));
            }
            if (queryDTO.getStatus() != null) {
                predicateList.add(cb.equal(root.get("status"), queryDTO.getStatus()));
            }
            if (queryDTO.getMerchantId() != null) {
                predicateList.add(cb.equal(root.get("merchantId"), queryDTO.getMerchantId()));
            }
            return cb.and(predicateList.toArray(new Predicate[0]));
        };

        // JPA分页页码从0开始,前端pageNum-1
        Pageable pageable = PageRequest.of(
                queryDTO.getPageNum() - 1,
                queryDTO.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime") //按创建时间倒序
        );

        return goodsRepository.findAll(spec, pageable);
    }
posted @ 2026-09-12 21:03  气球飞起  阅读(5)  评论(0)    收藏  举报