스프링 보안이 적용된 스프링 부트 응용 프로그램이 있습니다. 기본 레벨에서는 Spring의 튜토리얼과 거의 일치합니다. 내 질문은 홈 페이지를 쳤을 때 로그인 컨트롤러를 호출하는 것입니다 (두 번). 왜? 홈 페이지 만 보여주기 때문에 기본 설정에서 그렇게하는 것이 쉽지 않은 것입니다. 그러나 LoginController 클래스를 추가하면 홈 페이지를로드 할 때 두 번 호출됩니다. 이것을 확인하는 또 다른 방법은 login.html 템플릿에 잘못된 thymeleaf 태그를 추가하는 것입니다. 홈 페이지가 인증되지 않아도 오류가 발생합니다. 어떤 설명이라도 도움이 될 것입니다.스프링 보안을 통한 스프링 부트 응용 프로그램은 홈 페이지를로드 할 때 로그인 컨트롤러를 호출합니다.
내 코드 :
응용 프로그램 :
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
WebSecurityConfig :
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
HomeController :
이 이@Controller
public class HomeController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String getHomePage(Model model) {
return "home";
}
}
옵션 인 LoginController, 전화를받을 것이다 HITT 당신은 아마 LoginController
의 A RequestMapping
를 추가해야
@Controller
public class LoginController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public String getLoginPage(Model model) {
return "login";
}
}
홈 페이지를로드 할 때 로그인 컨트롤러가 두 번 호출되었음을 어떻게 확인 했습니까? 로그 에서요? (그렇다면 로그를 공유 할 수 있습니까?). – rhinds
중단 점을 넣거나 print 문을 ... 실제로 한 번 호출됩니다. html로 두 번 호출해야하는 뭔가가 있어야합니다. 그러나 이제는 한 번 호출됩니다. – ikcodez