OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2022-01-06 2a505f9dd1dffdba10876c9e33aae5f79561f342
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package kr.wisestone.owl.config;
 
import kr.wisestone.owl.config.security.filter.AjaxSessionExpiredFilter;
import kr.wisestone.owl.config.security.handler.AjaxAuthenticationEntryPoint;
import kr.wisestone.owl.config.security.handler.AjaxAuthenticationFailureHandler;
import kr.wisestone.owl.config.security.handler.AjaxAuthenticationSuccessHandler;
import kr.wisestone.owl.config.security.handler.AjaxLogoutSuccessHandler;
import kr.wisestone.owl.config.security.service.UserSecurityService;
import kr.wisestone.owl.config.security.strategy.SecuritySessionExpiredStrategy;
import kr.wisestone.owl.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.session.ConcurrentSessionFilter;
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;
 
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 
    private static final String REMEMBER_ME_KEY = "rememberMe";
 
    @Autowired
    private AjaxAuthenticationSuccessHandler ajaxAuthenticationSuccessHandler;
 
    @Autowired
    private AjaxAuthenticationFailureHandler ajaxAuthenticationFailureHandler;
 
    @Autowired
    private AjaxLogoutSuccessHandler ajaxLogoutSuccessHandler;
 
    @Autowired
    private AjaxAuthenticationEntryPoint authenticationEntryPoint;
 
    @Autowired
    private UserSecurityService userSecurityService;
 
    @Autowired
    private SessionRegistry sessionRegistry;
 
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(this.userSecurityService)
        .passwordEncoder(this.passwordEncoder());
    }
 
    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(this.userSecurityService);
        authenticationProvider.setPasswordEncoder(this.passwordEncoder());
        return authenticationProvider;
    }
 
    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
 
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
 
    @Bean
    public AjaxSessionExpiredFilter ajaxSessionExpiredFilter() {
        return new AjaxSessionExpiredFilter(this.sessionRegistry);
    }
 
    @Bean
    public SecuritySessionExpiredStrategy securitySessionExpiredStrategy() {
        return new SecuritySessionExpiredStrategy(Constants.SESSION_EXPIRE_REDIRECT_URL);
    }
 
    @Bean
    public SpringSessionRememberMeServices springSessionRememberMeServices() {
        SpringSessionRememberMeServices springSessionRememberMeServices =
                new SpringSessionRememberMeServices();
 
        springSessionRememberMeServices.setAlwaysRemember(false);
        springSessionRememberMeServices.setRememberMeParameterName(REMEMBER_ME_KEY);
        return springSessionRememberMeServices;
    }
 
    @Override
    public void configure(WebSecurity web) {
        web.ignoring()
                .antMatchers("/**/*.{js,css}");
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/security/login")
                .successHandler(this.ajaxAuthenticationSuccessHandler)
                .failureHandler(this.ajaxAuthenticationFailureHandler)
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll();
 
        http.logout()
                .clearAuthentication(true)
                .logoutUrl("/security/logout")
                .logoutSuccessHandler(this.ajaxLogoutSuccessHandler)
                .deleteCookies("JSESSIONID", REMEMBER_ME_KEY)
                .invalidateHttpSession(true)
                .permitAll();
 
        http.headers()
                .frameOptions()
                .disable()
                .and()
                .csrf()
                .disable()
                .httpBasic()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(this.authenticationEntryPoint);
 
        http.authorizeRequests()
                .antMatchers("/kakaoOAuth2CallBack").permitAll()
                .antMatchers("/naverOAuth2CallBack").permitAll()
                .antMatchers("/googleOAuth2CallBack").permitAll()
                .antMatchers("/facebookOAuth2CallBack").permitAll()
                .antMatchers("/user/addSocialLogin").permitAll()
                .antMatchers("/user/add").permitAll()
                .antMatchers("/user/getUserSession").permitAll()
                .antMatchers("/user/returnEmailPassword").permitAll()
                .antMatchers("/workspace/find").permitAll()
                .antMatchers("/workspace/findPrimaryWorkspace").permitAll()
                .antMatchers("/guide/detail").permitAll()
                .antMatchers("/language/change").permitAll()
                .antMatchers("/security/*").permitAll()
                .antMatchers("/api/issue").permitAll()
                .antMatchers("/api/issue/*").permitAll()
                .antMatchers("/**/*").authenticated();
 
//        http.addFilter(new CustomAuthenticationFilter());
//        http.addFilterBefore(new CustomAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
 
 
        http.rememberMe()
                .rememberMeServices(this.springSessionRememberMeServices());
 
 
        http.sessionManagement()
                .maximumSessions(1)
                .maxSessionsPreventsLogin(false)
                .expiredSessionStrategy(this.securitySessionExpiredStrategy())
                .sessionRegistry(this.sessionRegistry).and().and()
                .addFilterBefore(this.ajaxSessionExpiredFilter(), ConcurrentSessionFilter.class);
    }
}