WebSecurityConfig.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.its.bis.webapp.config;
  2. import com.its.bis.webapp.security.SessionListener;
  3. import com.its.bis.webapp.service.UserService;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.HttpMethod;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  11. import org.springframework.security.config.http.SessionCreationPolicy;
  12. import org.springframework.security.core.session.SessionRegistry;
  13. import org.springframework.security.core.session.SessionRegistryImpl;
  14. import org.springframework.web.cors.CorsConfiguration;
  15. import org.springframework.web.cors.CorsConfigurationSource;
  16. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  17. import javax.servlet.http.HttpSessionListener;
  18. @EnableWebSecurity
  19. @Configuration
  20. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  21. private final UserService userService;
  22. public WebSecurityConfig(UserService userService) {
  23. this.userService = userService;
  24. }
  25. @Override
  26. public void configure(WebSecurity web) throws Exception {
  27. web.ignoring().antMatchers("/favicon.ico");
  28. web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
  29. web.ignoring().antMatchers(HttpMethod.GET, "/api/**"); // GET Method 는 모두 통과
  30. web.ignoring().antMatchers(HttpMethod.POST, "/api/**"); // GET Method 는 모두 통과
  31. web.ignoring().antMatchers(HttpMethod.PUT, "/api/**"); // GET Method 는 모두 통과
  32. web.ignoring().antMatchers(HttpMethod.DELETE, "/api/**"); // GET Method 는 모두 통과
  33. }
  34. @Override
  35. protected void configure(HttpSecurity http) throws Exception {
  36. http
  37. .httpBasic().disable()
  38. .cors().configurationSource(corsConfigurationSource())
  39. .and()
  40. .csrf().disable()
  41. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  42. .and()
  43. .authorizeRequests()
  44. // SWAGGER 권한 설정
  45. .antMatchers("/swagger-ui.html", "/swagger/**", "/swagger-resources/**", "/webjars/**", "/v2/api-docs").permitAll()
  46. // 웹소켓 권한 설정하지
  47. .antMatchers("/ws/**").permitAll()
  48. .antMatchers("/api/**").permitAll()
  49. .anyRequest().permitAll()
  50. ;
  51. }
  52. // CORS 허용 적용
  53. @Bean
  54. public CorsConfigurationSource corsConfigurationSource() {
  55. CorsConfiguration corsConfig = new CorsConfiguration();
  56. corsConfig.setAllowCredentials(true); // cross origin 으로부터 인증을 위한 쿠키 정보를 받을지 여부
  57. corsConfig.addAllowedOriginPattern("*"); // addAllowedOrigin("*") 대신 사용, 허용할 origin 정보, Arrays.asList("http://localhost:8080")
  58. corsConfig.addAllowedHeader("*");
  59. corsConfig.addAllowedMethod("*"); // 허용할 http methods. Arrays.asList("GET", "POST")
  60. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  61. source.registerCorsConfiguration("/**", corsConfig);
  62. return source;
  63. }
  64. @Bean
  65. public SessionRegistry sessionRegistry() {
  66. return new SessionRegistryImpl();
  67. }
  68. @Bean
  69. public HttpSessionListener httpSessionListener() {
  70. return new SessionListener();
  71. }
  72. }