33import kr .seok .security .form .common .FormAuthenticationDetailsSource ;
44import kr .seok .security .form .handler .FormAccessDeniedHandler ;
55import kr .seok .security .form .provider .FormAuthenticationProvider ;
6+ import kr .seok .security .metadatasource .UrlFilterInvocationSecurityMetadataSource ;
67import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .beans .factory .annotation .Qualifier ;
89import org .springframework .boot .autoconfigure .security .servlet .PathRequest ;
910import org .springframework .context .annotation .Bean ;
1011import org .springframework .context .annotation .Configuration ;
12+ import org .springframework .security .access .AccessDecisionManager ;
13+ import org .springframework .security .access .AccessDecisionVoter ;
14+ import org .springframework .security .access .vote .AffirmativeBased ;
15+ import org .springframework .security .access .vote .RoleVoter ;
16+ import org .springframework .security .authentication .AuthenticationManager ;
1117import org .springframework .security .authentication .AuthenticationProvider ;
1218import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
1319import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1723import org .springframework .security .crypto .factory .PasswordEncoderFactories ;
1824import org .springframework .security .crypto .password .PasswordEncoder ;
1925import org .springframework .security .web .access .AccessDeniedHandler ;
26+ import org .springframework .security .web .access .intercept .FilterInvocationSecurityMetadataSource ;
27+ import org .springframework .security .web .access .intercept .FilterSecurityInterceptor ;
2028import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
2129import org .springframework .security .web .authentication .AuthenticationSuccessHandler ;
2230
31+ import java .util .Arrays ;
32+ import java .util .List ;
33+
2334@ Configuration
2435@ EnableWebSecurity
2536public class SecurityConfig extends WebSecurityConfigurerAdapter {
@@ -57,27 +68,26 @@ public void configure(WebSecurity web) {
5768 protected void configure (HttpSecurity http ) throws Exception {
5869 http
5970 .authorizeRequests ()
60- .antMatchers ("/" , "/users" , "/user/login/**" ).permitAll ()
61- .antMatchers ("/mypage" ).hasRole ("USER" )
62- .antMatchers ("/messages" ).hasRole ("MANAGER" )
63- .antMatchers ("/config" ).hasRole ("ADMIN" )
64- .anyRequest ().authenticated ()
71+ .antMatchers ("/**" ).permitAll ()
72+ .anyRequest ().authenticated ();
6573
66- . and ()
74+ http
6775 .formLogin ()
6876 .loginPage ("/login" )
69- /* form 태그의 action url */
70- .loginProcessingUrl ("/login_proc" )
71- /* request의 상세 값을 Details로 추가 하기 위한 작업 */
72- .authenticationDetailsSource (formAuthenticationDetailsSource )
73- /* 사용자 정의 Success Handler */
74- .successHandler (formAuthenticationSuccessHandler )
77+ .loginProcessingUrl ("/login_proc" ) /* form 태그의 action url */
78+ .authenticationDetailsSource (formAuthenticationDetailsSource ) /* request의 상세 값을 Details로 추가 하기 위한 작업 */
79+ .successHandler (formAuthenticationSuccessHandler ) /* 사용자 정의 Success Handler */
7580 .failureHandler (formAuthenticationFailureHandler )
7681 .permitAll ()
7782 ;
7883 http
7984 .exceptionHandling ()
8085 .accessDeniedHandler (accessDeniedHandler ());
86+
87+ /* FilterSecurityInterceptor 사용자 정의 */
88+ http
89+ .addFilterBefore (customFilterSecurityInterceptor (), FilterSecurityInterceptor .class );
90+
8191 http .csrf ().disable ();
8292 }
8393
@@ -96,4 +106,32 @@ protected void configure(AuthenticationManagerBuilder auth) {
96106 /* 사용자 정의된 Provider 처리 */
97107 .authenticationProvider (authenticationProvider ());
98108 }
109+
110+ @ Override
111+ public AuthenticationManager authenticationManagerBean () throws Exception {
112+ return super .authenticationManagerBean ();
113+ }
114+
115+ @ Bean
116+ public FilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource () {
117+ return new UrlFilterInvocationSecurityMetadataSource ();
118+ }
119+
120+ @ Bean
121+ public FilterSecurityInterceptor customFilterSecurityInterceptor () throws Exception {
122+ FilterSecurityInterceptor filterSecurityInterceptor = new FilterSecurityInterceptor ();
123+ filterSecurityInterceptor .setSecurityMetadataSource (urlFilterInvocationSecurityMetadataSource ());
124+ filterSecurityInterceptor .setAccessDecisionManager (affirmativeBased ());
125+ filterSecurityInterceptor .setAuthenticationManager (authenticationManagerBean ());
126+ return filterSecurityInterceptor ;
127+ }
128+
129+ private AccessDecisionManager affirmativeBased () {
130+ return new AffirmativeBased (getAccessDecisionVoters ());
131+ }
132+
133+ private List <AccessDecisionVoter <?>> getAccessDecisionVoters () {
134+ return Arrays .asList (new RoleVoter ());
135+ }
136+
99137}
0 commit comments