У меня возникла проблема с запросами и их обработкой.
Имеется ManagerController, который принимает различные запросы. Вот он:
@Controller
@RequestMapping("/managerToolsService")
public class ManagerController {
@Autowired
private ScheduleService scheduleService;
@Autowired
private TrainService trainService;
@Autowired
private TicketService ticketService;
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
@GetMapping("/viewBookingTrains")
public String viewBookingTrains(Model model) {
//List<TrainDTO> bookingTrains = trainService.getAll();
List<Train> bookingTrains = trainService.getAllValidTrains();
model.addAttribute("bookingTrains", bookingTrains);
return "booking-train-users-list";
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
@RequestMapping(name = "/viewSchedules", params = "trainId")
public String viewBookingTrains(@RequestParam(value = "trainId") Long trainId, Model model) {
List<Schedule> selectedScheduleByTrain = scheduleService.getByTrain(
trainService.getById(trainId)
);
model.addAttribute("selectedSchedulesByTrain", selectedScheduleByTrain);
return "schedule-list";
}
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')")
@RequestMapping(name = "/viewBookingUsersInfo", params = "scheduleId")
public String viewBookingUsersInfo(@RequestParam(value = "scheduleId") Long scheduleId, Model model) {
// List<Seat> bookingSeats = ticketService.getBookingSeatsBySchedule(
// scheduleService.getById(scheduleId)
// );
List<Ticket> tickets = ticketService.getBySchedules(
scheduleService.getById(scheduleId)
);
model.addAttribute("tickets", tickets);
return "booking-users-in-train";
}
}
И с первым перехватом запроса проблем нету, он отрабатывает, когда я перехожу по урлу
/viewBookingTrains
Затем, у меня в JSP-шке от первого перехвата есть ссылки (POST-запрос):
<!-- Add HTML table -->
<table class="table table-striped">
<tr>
<th>Train name</th>
</tr>
<!-- Loop over and print stations -->
<c:forEach var="tmpTrain" items="${bookingTrains}">
<tr>
<td>${tmpTrain.name}</td>
<td><a href="/managerToolsService/viewSchedules?trainId=${tmpTrain.id}">Watch</a> </td>
</tr>
</c:forEach>
И в результате, когда я перехожу по ссылке
a href="/managerToolsService/viewSchedules?trainId=${tmpTrain.id}
я получаю
HTTP Status 404 – Not Found
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Хотя урл тут должен обрабатываться (
http://localhost:8080/managerToolsService/viewSchedules?trainId=4
).
Может быть, это происходит из-за неправильного конфига Spring security?
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackageClasses = UserDetailServiceImpl.class)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:8080");
}
};
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.setAllowedOrigins(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/registration",
"/login",
"/",
"/static/**",
"/home",
"/stations/list",
"/schedule/scheduleList",
"/schedule/scheduleByStationsAndDate")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("login")
.passwordParameter("password")
.defaultSuccessUrl("/home")
.failureUrl("/login?error=true")
.and()
.cors()
.and()
.csrf().disable()
.logout().permitAll();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean()
throws Exception {
return super.authenticationManagerBean();
}
}