WebSecurityConfig.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.its.api.webapp.config;
  2. import com.its.api.webapp.handler.LoginFailureHandler;
  3. import com.its.api.webapp.handler.LoginSuccessHandler;
  4. import com.its.api.webapp.security.WebPasswordEncoder;
  5. import com.its.api.webapp.security.WebSessionListener;
  6. import com.its.api.webapp.service.WebLoginService;
  7. import lombok.RequiredArgsConstructor;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  12. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  13. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  14. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  15. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  16. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  17. import org.springframework.security.core.session.SessionRegistry;
  18. import org.springframework.security.core.session.SessionRegistryImpl;
  19. import org.springframework.security.crypto.password.PasswordEncoder;
  20. import javax.servlet.http.HttpSessionListener;
  21. @Configuration
  22. @EnableWebSecurity
  23. @RequiredArgsConstructor
  24. //@EnableGlobalMethodSecurity(securedEnabled = true)
  25. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  26. private final WebLoginService loginService;
  27. private final LoginSuccessHandler loginSuccessHandler;
  28. private final LoginFailureHandler loginFailureHandler;
  29. @Override
  30. public void configure(WebSecurity web) {
  31. // static 디렉터리의 하위 파일 목록은 인증 무시 ( = 항상통과 )
  32. web.ignoring().antMatchers("/js/**", "/images/**", "/libs/**", "/css/**", "/application/fonts/**");
  33. //web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations()); // 정적 리소스 접근 가능하게
  34. web.ignoring().antMatchers(HttpMethod.GET, "/api/**"); // GET Method 는 모두 통과
  35. }
  36. @Override
  37. protected void configure(HttpSecurity http) throws Exception {
  38. http.csrf()
  39. .disable()
  40. ; // REST API 호출 유효하게(POST...)
  41. http
  42. .authorizeRequests()
  43. // SWAGGER 권한 설정
  44. .antMatchers("/swagger-ui.html", "/swagger/**", "/swagger-resources/**", "/webjars/**", "/v2/api-docs").permitAll()
  45. // 웹소켓 권한 설정하지
  46. .antMatchers("/ws/**").permitAll()
  47. // API 권한 설정하지
  48. //.antMatchers("/api/**").permitAll()
  49. // 지도 URI 권한 설정하지
  50. .antMatchers("/MAPDATA/**").permitAll()
  51. // 페이지 권한 설정
  52. .antMatchers("/application/facility/**", "/facility/**").permitAll()
  53. .antMatchers("/application/wall/**", "/wall/**").permitAll()
  54. .antMatchers("/application/login/**").permitAll()
  55. .anyRequest().authenticated()
  56. .and()
  57. .formLogin()
  58. .loginPage("/application/login/login.html")
  59. .loginProcessingUrl("/login.do")
  60. .defaultSuccessUrl("/application/op/00.main/main.html", true)
  61. .usernameParameter("username")
  62. .passwordParameter("password")
  63. .successHandler(this.loginSuccessHandler)
  64. .failureHandler(this.loginFailureHandler)
  65. .permitAll()
  66. .and()
  67. // .logout()
  68. // .invalidateHttpSession(true)
  69. // .deleteCookies("JSESSIONID")
  70. // .logoutSuccessUrl("/login.do")
  71. // .permitAll()
  72. // .and()
  73. // .exceptionHandling()
  74. // .accessDeniedPage("/login.do")
  75. // .and()
  76. // .headers()
  77. // .defaultsDisabled()
  78. // .frameOptions()
  79. // .sameOrigin()
  80. // .cacheControl();
  81. // .and() // 로그아웃 설정
  82. // .logout()
  83. // .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  84. // .logoutSuccessUrl("/login")
  85. // .invalidateHttpSession(true)
  86. // .deleteCookies("JSESSIONID")
  87. // .and()
  88. // // 403 예외처리 핸들링
  89. // .exceptionHandling().accessDeniedPage("/login");
  90. ;
  91. // http.sessionManagement()
  92. // .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  93. // .invalidSessionUrl("/login")
  94. // .sessionFixation()
  95. // .migrateSession()
  96. // .maximumSessions(5)
  97. // .maxSessionsPreventsLogin(true)
  98. // .expiredUrl("/login")
  99. // .sessionRegistry(sessionRegistry())
  100. // ;
  101. }
  102. public DaoAuthenticationProvider daoAuthenticationProvider() {
  103. DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
  104. authenticationProvider.setUserDetailsService(this.loginService);
  105. authenticationProvider.setPasswordEncoder(passwordEncoder());
  106. // loadUserByUsername 의 UsernameNotFoundException 이 BadCredentialsException 로 발생함.
  107. // Exception 을 catch 하기 위해서는 아래를 false 로 설정하면 됨.
  108. authenticationProvider.setHideUserNotFoundExceptions(true);
  109. return authenticationProvider;
  110. }
  111. @Override
  112. public void configure(AuthenticationManagerBuilder auth) throws Exception {
  113. // loadUserByUsername 의 UsernameNotFoundException 를 처리하기 위해
  114. // AuthenticationProvider 를 빈으로 등록해서 사용자 로그인 처리를 수행한다.
  115. //auth.userDetailsService(this.loginService).passwordEncoder(passwordEncoder());
  116. auth.authenticationProvider(daoAuthenticationProvider());
  117. }
  118. @Bean
  119. public PasswordEncoder passwordEncoder() {
  120. return new WebPasswordEncoder();
  121. }
  122. @Bean
  123. public SessionRegistry sessionRegistry() {
  124. return new SessionRegistryImpl();
  125. }
  126. @Bean
  127. public HttpSessionListener httpSessionListener(){
  128. return new WebSessionListener();
  129. }
  130. }