我有一个Sping Boot 应用程序,我试图在其中创建REST端点。但是,当我尝试访问/users
或/test
端点时,收到404错误。我怀疑问题可能与组件扫描配置和@ComponentScan
注解的使用有关。
下面是我的项目结构和代码的概述:
后台应用程序
@SpringBootApplication
@EnableWebSecurity
@ComponentScan(basePackages = {"com.kaquya.backend.configs"})
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
}
字符串
用户控制器
@RestController
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@PostMapping(value = "/users")
public User createUser(@RequestBody User user) {
return userService.register(user.getUsername(), user.getPassword(), user.getEmail());
}
@GetMapping("/test")
public String testEndpoint() {
return "this is a test endpoint";
}
}
型
用户服务
@Service
public class UserService {
private final UserExtractor userExtractor;
private final UserRepository userRepository;
public UserService(UserRepository userRepository, UserExtractor userExtractor) {
this.userRepository = userRepository;
this.userExtractor = userExtractor;
}
public User register(String username, String password, String email) {
User existingUser = userExtractor.findByUsername(username);
if(existingUser != null) {
throw new RuntimeException("Username already exists");
}
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
return userRepository.save(user);
}
}
型
UserExtractor
@Repository
public interface UserExtractor {
User findByUsername(String username);
}
型
用户库
@Repository
public interface UserRepository {
User save(User user);
}
型
UserRepositoryImpl
@Repository
public class UserRepositoryImpl implements UserRepository {
private final JdbcTemplate jdbcTemplate;
@Value("${queries.insertUser}")
private String insertUserQuery;
public UserRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public User save(User user) {
String hashedPassword = PasswordHasherService.hashPassword(user.getPassword());
jdbcTemplate.update(
insertUserQuery,
user.getUsername(),
hashedPassword,
user.getEmail()
);
return user;
}
}
型
PropertyConfig
@Configuration
public class PropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("queries/user_queries.xml"));
return configurer;
}
}
型
SecurityConfig
@EnableWebSecurity
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().permitAll()
)
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}
}
型
我怀疑这个问题可能与BackendApplication
类中的@ComponentScan
注解有关。目前,它只扫描"com.kaquya.backend.configs"
包,这意味着其他组件可能会被忽略。但是,当我尝试向@ComponentScan
注解添加其他包时,遇到了与PropertyConfig
类中定义的查询相关的错误。
我使用的是Java Sping Boot v3.1.1,我意识到某些东西,如mvcMatchers
,antMatchers
,WebSecurityConfigurerAdapter
和authorizeRequests
已经被弃用或删除。
请您帮助我确定问题并建议解决方案,以解决404错误并正确配置我的Sping Boot 应用程序中的组件扫描?
2条答案
按热度按时间58wvjzkj1#
在安全配置中添加以下注解:
字符串
想了解更多关于 Boot 的信息,你可以访问下面的spring官方网站
https://spring.io/guides/gs/securing-web/
8ftvxx2r2#
您可以使用
antMatchers
:字符串
requestMatchers
用于需要基于其他请求条件(如标头、参数等)的复杂匹配,而antMatchers
用于基于蚂蚁式路径模式进行匹配