我正在开发一个用于管理医疗设备和公司的 Spring Boot 应用程序。我已经使用处理搜索请求的 SearchController 建立了一个基本结构。但是,每当我尝试访问搜索端点(例如,http://localhost:8080/search?q=Company)时,我都会遇到重定向循环,最终导致错误页面。
相关代码 SearchController.java
package com.example.medicinska_oprema.controller;
import com.example.medicinska_oprema.model.Company;
import com.example.medicinska_oprema.model.Equipment;
import com.example.medicinska_oprema.service.CompanyService;
import com.example.medicinska_oprema.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
import java.util.List;
@Controller
public class SearchController {
@Autowired
private CompanyService companyService;
@Autowired
private EquipmentService equipmentService;
@GetMapping("/search")
public String search(@RequestParam("q") String query, Model model) {
List<Company> companies = companyService.searchCompanies(query);
List<Equipment> equipments = equipmentService.searchEquipment(query);
model.addAttribute("companies", companies);
model.addAttribute("equipments", equipments);
return "searchResults";
}
}
CompanyService.java
package com.example.medicinska_oprema.service;
import com.example.medicinska_oprema.model.Company;
import com.example.medicinska_oprema.repository.CompanyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CompanyService {
@Autowired
private CompanyRepository companyRepository;
public List<Company> searchCompanies(String keyword) {
return companyRepository.findByNameContainingIgnoreCase(keyword);
}
}
设备服务.java
package com.example.medicinska_oprema.service;
import com.example.medicinska_oprema.model.Equipment;
import com.example.medicinska_oprema.repository.EquipmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EquipmentService {
@Autowired
private EquipmentRepository equipmentRepository;
public List<Equipment> searchEquipment(String keyword) {
return equipmentRepository.findByNameContainingIgnoreCase(keyword);
}
}
CompanyRepository.java
package com.example.medicinska_oprema.repository;
import com.example.medicinska_oprema.model.Company;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface CompanyRepository extends JpaRepository<Company, Long> {
List<Company> findByNameContainingIgnoreCase(String keyword);
}
设备存储库.java
package com.example.medicinska_oprema.repository;
import com.example.medicinska_oprema.model.Equipment;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface EquipmentRepository extends JpaRepository<Equipment, Long> {
List<Equipment> findByNameContainingIgnoreCase(String keyword);
}
安全配置.java
package com.example.medicinska_oprema.security;
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.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/home", "/search").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout((logout) -> logout.permitAll());
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
我希望看到搜索结果页面显示与搜索查询匹配的公司和设备。相反,我被重复重定向到 /login 页面,导致“重定向次数过多”错误。
我已经检查了我的安全配置,并确保所有用户都允许 /search 端点,包括未经身份验证的用户。什么可能导致此重定向循环,我该如何解决它?
我尝试使用 spring boot 2.5.x 版本,并通过仅更改 securityFilterChain 方法来使用,它对我有用。 本地主机:8080/搜索给了我结果
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests((requests) -> requests
.antMatchers("/", "/home", "/search").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout((logout) -> logout.permitAll());
return http.build();
}