You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot JPA实体非托管报错与接口401未授权问题求助

针对Spring Boot + JPA两个问题的解决方案

一、解决"Not a managed type: class com.example1.project.User.User"异常

这个异常的核心原因是Spring Data JPA未扫描到User实体类,无法将其识别为受管实体。结合你的代码和项目结构,给出以下解决方法:

  1. 修正主启动类的包扫描配置
    你的主启动类Application位于com.example1.project包下,默认扫描该包及其子包,但如果JPA配置存在特殊设置,可能导致实体类扫描遗漏。修改主启动类,添加注解明确指定扫描范围:
package com.example1.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackages = "com.example1.project.User") // 指定实体类所在包
@EnableJpaRepositories(basePackages = "com.example1.project.User") // 指定Repository所在包
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. 确认实体类注解与依赖正确性
  • 你的User类使用jakarta.persistence.*包下的注解,符合Spring Boot 3.x要求;若使用Spring Boot 2.x需替换为javax.persistence.*包。
  • 确保项目依赖包含Spring Data JPA与对应数据库驱动,例如Maven配置:
<!-- Spring Data JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL驱动 -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

二、解决接口401 Unauthorized问题

接口返回401是因为Spring Security默认拦截所有请求,而你的配置仅定义了用户信息,未设置请求授权规则。解决方案如下:

  1. 完善SecurityConfiguration配置
    添加SecurityFilterChain Bean,明确请求授权规则,同时开启HTTP Basic认证适配PostMan凭据验证:
package com.example1.project;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

    @Bean
    public InMemoryUserDetailsManager userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(user);
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf(csrf -> csrf.disable()) // REST接口可关闭CSRF防护
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/api/login").permitAll() // 允许登录接口匿名访问
                .anyRequest().authenticated() // 其他接口需认证
            )
            .httpBasic(); // 使用HTTP Basic认证,适配PostMan的凭据设置
        return http.build();
    }
}
  1. PostMan正确配置方式
    在PostMan的Authorization标签中选择Basic Auth类型,输入用户名user、密码password后发送请求即可。

  2. 解决HikariPool警告
    控制台中Thread starvation or clock leap detected警告是数据库连接池问题,需确保MySQL服务正常运行,同时检查application.properties中的数据库配置:

spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?useSSL=false&serverTimezone=UTC
spring.datasource.username=你的数据库用户名
spring.datasource.password=你的数据库密码
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

内容的提问来源于stack exchange,提问作者CosmaPOP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:07:04