Logowanie do aplikacji spring boot z spring security

0

Cześć!
Mam problem z logowaniem do aplikacji z włączonym spring security. Gdy go wyłącze to mogę przejść z /login na /MainMenu jeśli logowanie jest poprawne. Oto mój mapping:

@PostMapping("/login")
	public ModelAndView logIn(@RequestParam(value = "login") String login,
			@RequestParam(value = "internalNumber", required = false) String internalNumber,
			@RequestParam(value = "password") String password) {

		logger.info("login: " + login);
		logger.info("internal number: " + internalNumber);
		logger.info("password: " + password);

		operator = operatorRepository.findByNazwa(login);

		
		ModelAndView mav = new ModelAndView();
		
		if (operator == null) {
			mav.setViewName("index");
			mav.addObject("message", "Nie ma takiego uzytkownika");
		} else if (login.isEmpty() || password.isEmpty()) {
			mav.setViewName("index");
			mav.addObject("message", "Podałeś pusty login lub hasło!");
		} else if (login.equals(operator.getNazwa()) && password.equals(operator.getPassword())) {
			mav.setViewName("redirect:/MainMenu");
		} else {
			mav.setViewName("index");
			mav.addObject("message", "Złe hasło lub login!");
		}
		return mav;
	}

a tak wygląda security config:

package local.vlex.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	@Override
    protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
        http.authorizeRequests()
                .antMatchers("/", "/login", "/assets/**").permitAll()
                .anyRequest().authenticated()
                .and().exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint())
                ;
    }

    @Bean
    AuthenticationEntryPoint authenticationEntryPoint() {
        return new AuthenticationEntryPoint() {
            @Override
            public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                 AuthenticationException e) throws IOException, ServletException {
                httpServletResponse.sendError(404);
            }
        };
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

Dlaczego po poprawnym zalogowaniu nie zostaję przekierowany na stronę /MainMenu?

0

Usuń endpoint "/login" z metody antMatchers() i zastąp to metodą .formLogin() możesz dla niej napisać swoje succes/failure handlery. Spring domyślnie zezwala wszystkim na "/login" i wysyła po nim credentiale, ponadto nie musisz pisać kontrolera dla tego urla.

1 użytkowników online, w tym zalogowanych: 0, gości: 1