Spring+Oracle项目Postman返回空列表但数据库已有数据如何解决

问题背景
基于Spring框架、Oracle数据库开发的项目,已通过Spring文件导入功能完成数据入库操作,但使用Postman验证业务接口时,数据库中已存在对应业务数据,接口却始终返回空列表。当前项目核心安全配置代码如下:
AuthorizationServerConfig 配置
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{ @Autowired private BCryptPasswordEncoder passwordEncoder; @Autowired @Qualifier("authenticationManager") private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .tokenStore(tokenStore()) .accessTokenConverter(accessTokenConverter()); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory().withClient("angularapp") .secret(passwordEncoder.encode("12345")) .scopes("read", "write") .authorizedGrantTypes("password","refresh_token") .accessTokenValiditySeconds(3600) .refreshTokenValiditySeconds(3600); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()"); } @Bean public JwtAccessTokenConverter accessTokenConverter() { JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter(); return jwtAccessTokenConverter; } @Bean public JwtTokenStore tokenStore() { return new JwtTokenStore(accessTokenConverter()); } }
ResourceServerConfig 配置
@Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter{ @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(HttpMethod.GET,"/eventos/participantes").permitAll() .anyRequest().authenticated(); } }
SpringSecurityConfig 配置
@Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private UserDetailsService usuarioService; @Bean public BCryptPasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Override @Autowired protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(this.usuarioService).passwordEncoder(passwordEncoder()); } @Bean("authenticationManager") @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); // 如需修改生成方法的主体,请选择「工具」>「模板」 } @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } }
核心原因排查清单
- 安全过滤链规则覆盖:项目同时存在
ResourceServerConfig、SpringSecurityConfig两个独立的HttpSecurity配置,未给两个配置指定不同的加载优先级时,Spring Security会默认按配置类在容器中的加载顺序生效,/eventos/participantes配置的匿名访问规则会被后加载的SpringSecurityConfig中anyRequest().authenticated()全局规则覆盖,请求在进入业务方法前就被拦截,无法正常触发数据库查询。 - 接口路径匹配偏差:检查项目配置的
server.servlet.context-path参数,若存在全局上下文前缀(例如配置为/api),实际访问接口的完整路径为/api/eventos/participantes,当前antMatchers中写的无上下文前缀的匹配规则会完全失效,导致请求进入鉴权流程,无法正常返回查询数据。 - 业务查询自动过滤条件不匹配:即使请求顺利进入Controller层,若数据查询逻辑中自动拼接了Security上下文内的用户身份参数(如当前登录用户ID、所属机构编码)作为查询过滤条件,匿名访问场景下Security上下文没有有效用户信息,会导致查询条件和库中导入的数据字段不匹配,最终返回空列表。
- JWT令牌配置缺失固定签名:当前
accessTokenConverter()方法未设置固定的对称加密签名,后续携带令牌访问受保护接口时,会出现令牌解析校验失败的问题,间接导致接口无法获取正确的查询结果。
修复参考方案
- 给安全配置类指定明确的加载优先级:在
ResourceServerConfig类上添加@Order(Ordered.HIGHEST_PRECEDENCE)注解,让资源服务的路由校验规则优先加载,专门负责业务资源路径的权限判断,避免被全局Web安全规则覆盖。 - 对齐接口匹配规则和实际请求路径:将全局上下文路径前缀补充到
antMatchers的匹配路径中,确保规则能精准覆盖实际的请求地址。 - 开启SQL执行日志:打印查询接口实际执行的Oracle SQL语句、绑定参数,对比直接在数据库执行相同SQL的返回结果,定位是查询阶段就未匹配到数据,还是返回值序列化环节丢失了内容。
内容的提问来源于stack exchange,提问作者user204089
相关产品推荐
相关产品推荐

