S
S
Sland Show2018-09-24 19:43:51
Java
Sland Show, 2018-09-24 19:43:51

Why is the request not reaching the controller?

I have a problem with requests and their processing.

There is a ManagerController that accepts various requests. There he is:

@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";
    }



}


And there are no problems with the first interception of the request, it works when I follow the url. /viewBookingTrains

Then, I have links in the JSP from the first interception (POST request):
<!-- 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>


And as a result, when I follow the link
a href="/managerToolsService/viewSchedules?trainId=${tmpTrain.id}
I get

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.


Although the url should be processed here (
http://localhost:8080/managerToolsService/viewSchedules?trainId=4
).

Maybe it's happening due to wrong spring security config?
@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();
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TonyJa, 2018-09-26
@TonyJa

You are missing the HTTP method @RequestMapping(name = "/viewSchedules", params = "trainId")
Although, maybe I'm wrong and it's default by default?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question