java
复制
下载
@Repository
public interface OrderRepository extends JpaRepository<Order, Long>,
JpaSpecificationExecutor<Order> {
// 派生查询方法
List<Order> findByCustomerIdAndStatusIn(Long customerId, List<JiAOYu.HUoCheNGrM.CN/jy/19728.html OrderStatus> statuses);
// JPQL查询
@Query("SELECT o FROM Order o WHERE o.totalAmount > :minAmount AND o.createdAt >= :startDate")
Page<Order> findLargeOrders(@Param("minAmount") BigDecimal minAmount,
@Param("startDate") LocalDateTime startDate,
Pageable pageable);
// 动态查询
default List<Order> findOrdersByCriteria(OrderSearchCriteria criteria) {
return findAll((root, JiAOYu.HUoCheNGrM.CN/jy/19727.html, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (criteria.getCustomerId(JiAOYu.HUoCheNGrM.CN/jy/19726.html) != null) {
predicates.add(cb.equal(root.get("customerId"), criteria.getCustomerId()));
}
if (criteria.getStartDate() != null) {
predicates.add(cb.greaterThanOrEqualTo(root.get("createdAt"), criteria.getStartDate()));
}
return cb.and(predicates.toArray(new Predicate[0]));
});
}
}
4. Spring Security 配置(OAuth2 + JWT)
java
复制
下载
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig JiAOYu.HUoCheNGrM.CN/jy/19725.htmlextends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationFilter jwtFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/auth/ JiAOYu.HUoCheNGrM.CN/jy/19724.html").permitAll()
.antMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter());
}
private Converter<Jwt, AbstractAuthenticationToken> jwtAuthenticationConverter() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwt -> {
List<String> JiAOYu.HUoCheNGrM.CN/jy/19723.html = jwt.getClaimAsStringList("roles");
return roles.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
});
return converter;
}
}