Requests not to enter the @RequestMapping in Spring

1

I am practicing an application in Spring and I am having a problem that I am not able to understand.

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping(path="/login", method=RequestMethod.GET)
    public ModelAndView login(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping(path="/api_endpoint/login",method=RequestMethod.POST)
    @ResponseBody
    public UserApiResponse loginUser(@RequestBody User user) {
        UserApiResponse userApiResponse=new UserApiResponse();
        User userExists = userService.findUserByEmail(user.getEmail());
        if (userExists == null) {
            userApiResponse.setMessageError("There is not any user with the email provided");
            userApiResponse.setHttpCode(400);
        }else if(!bCryptPasswordEncoder.matches(user.getPassword(), userExists.getPassword())){
            userApiResponse.setMessageError("Incorrect password");
            userApiResponse.setHttpCode(401);
        }else{
            userApiResponse.setHttpCode(200);
        }

        return userApiResponse;
    }


    @RequestMapping(path="/registration",method=RequestMethod.GET)
    public ModelAndView registration(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User(); 
        modelAndView.addObject("user", user);
        modelAndView.setViewName("registration");
        return modelAndView;
    }

    @RequestMapping(path="/registration",method=RequestMethod.POST)
    public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        User userExists = userService.findUserByEmail(user.getEmail());
        if (userExists != null) {
            bindingResult
                    .rejectValue("email", "error.user",
                            "There is already a user registered with the email provided");
        }
        if (bindingResult.hasErrors()) {
            modelAndView.setViewName("registration");
        } else {
            userService.saveUser(user);
            modelAndView.addObject("successMessage", "User has been registered successfully");
            modelAndView.addObject("user", new User());
            modelAndView.setViewName("registration");

        }
        return modelAndView;
    }

I have these @RequestMapping , but it does not matter the path that I put in the request from the client, which always comes in the first @RequestMapping , the login . Any suggestions?

    
asked by Hictus 21.06.2018 в 16:00
source

1 answer

1

When you enter an anonymous user and you do not have a path defined without security, you will always be sent to login .

It is still necessary to define the path of /api_endpoint to the configuration.

    
answered by 21.06.2018 / 20:53
source