Home Spring Security, Authentication Success handler
Post
Cancel

Spring Security, Authentication Success handler

AuthenticationSuccessHandler

  • 인증에 성공했을 때 어떤 동작을 취할것인지 설정하는 핸들러


CustomSuccessHandler

1
2
3
4
5
6
7
8
9
@Slf4j
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    response.sendRedirect("/main");
  }
}


SecurityConfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Slf4j
@Configuration
@RequiredArgsConstructor
public class AngrySecurityConfiguration {

  //    ...

  @Bean
  public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
    httpSecurity(http)
      .authorizeHttpRequests(
        //    ...
      )
      .formLogin(formLogin -> formLogin
        .successHandler(customAuthenticationSuccessHandler)
      )
      .httpBasic(withDefaults());

    return http.build();
  }

  private HttpSecurity httpSecurity(HttpSecurity http) throws Exception {
    //    ...
  }


  @Bean
  public PasswordEncoder passwordEncoder() {
    //    ...
  }

}


MainController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Slf4j
@Controller
@RequestMapping(path = "/main")
public class MainController {

  @GetMapping
  public String defaultMain(Authentication authentication, Model model) {
    authenticationPrintHelper(authentication);
    String name = authentication.getName();
    String greeting = String.format("%s님 반갑습니다", name);
    model.addAttribute("greeting", greeting);
    return "/main/index";
  }

  private void authenticationPrintHelper(Authentication authentication) {
    String name = authentication.getName();
    log.info("name = {}", name);
  }

}


/main/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

<header>
  <p th:text="${greeting}"></p>
</header>

</body>
</html>

success_handler


테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@SpringBootTest
@AutoConfigureMockMvc
public class MainControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @Test
  void main_greeting_test() throws Exception {

    MvcResult mvcResult = mockMvc.perform(
        get("/main")
          .with(httpBasic("guest@gmail.com", "1q2w3e4r!"))
      )
      .andExpect(status().isOk())
      .andReturn();

    ModelAndView modelAndView = mvcResult.getModelAndView();
    Assertions.assertNotNull(modelAndView);

    String viewName = modelAndView.getViewName();
    Assertions.assertEquals("/main/index", viewName);

    Map<String, Object> model = modelAndView.getModel();
    Assertions.assertNotNull(model.get("greeting"));

  }

}

test_success

This post is licensed under CC BY 4.0 by the author.