AbstractSecurityInterceptor.class.beforeInvocation()를 2번 실행하는 경우


@Order(0)
@Configuration
@EnableWebSecurity
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
@Autowired private SecurityResourceService securityResourceService;
@Autowired private FormAuthenticationDetailsSource authenticationDetailsSource;
@Autowired private AuthenticationSuccessHandler customAuthenticationSuccessHandler;
@Autowired private AuthenticationFailureHandler customAuthenticationFailureHandler;
private String[] permitAllPattern = {"/", "/login", "/user/login/**"};
@Bean
public PermitAllFilter customFilterSecurityInterceptor() throws Exception {
PermitAllFilter permitAllFilter = new PermitAllFilter(permitAllPattern);
permitAllFilter.setSecurityMetadataSource(urlFilterInvocationSecurityMetadataSource());
permitAllFilter.setAccessDecisionManager(affirmativeBased());
permitAllFilter.setAuthenticationManager(authenticationManagerBean());
return permitAllFilter;
}
private AccessDecisionManager affirmativeBased() {
AffirmativeBased affirmativeBased = new AffirmativeBased(getAccessDecisionVoters());
return affirmativeBased;
}
private List<AccessDecisionVoter<?>> getAccessDecisionVoters() {
List<AccessDecisionVoter<? extends Object>> accessDecisionVoters = new ArrayList<>();
accessDecisionVoters.add(new IpAddressVoter(securityResourceService));
accessDecisionVoters.add(roleVoter());
return accessDecisionVoters;
}
@Bean
public RoleHierarchyVoter roleVoter() {
RoleHierarchyVoter roleHierarchyVoter = new RoleHierarchyVoter(roleHierarchy());
return roleHierarchyVoter;
}
@Bean
public RoleHierarchyImpl roleHierarchy() {
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
return roleHierarchy;
}
@Bean
public FilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource() throws Exception {
return new UrlFilterInvocationSecurityMetadataSource(urlResourcesMapFactoryBean().getObject(), securityResourceService);
}
private UrlResourcesMapFactoryBean urlResourcesMapFactoryBean() {
UrlResourcesMapFactoryBean urlResourcesMapFactoryBean = new UrlResourcesMapFactoryBean();
urlResourcesMapFactoryBean.setSecurityResourceService(securityResourceService);
return urlResourcesMapFactoryBean;
}
@Bean
public AuthenticationProvider authenticationProvider() {
return new FormAuthenticationProvider(passwordEncoder());
}
@Bean
public AccessDeniedHandler accessDeniedHandler() {
CustomAccessDeniedHandler accessDeniedHandler = new CustomAccessDeniedHandler();
accessDeniedHandler.setErrorPage("/denied");
return accessDeniedHandler;
}
@Bean
public AuthenticationSuccessHandler ajaxAuthenticationSuccessHandler() {
return new AjaxAuthenticationSuccessHandler();
}
@Bean
public AuthenticationFailureHandler ajaxAuthenticationFailureHandler() {
return new AjaxAuthenticationFailureHandler();
}
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authenticationProvider());
}
@Override
public void configure(WebSecurity web) {
web.ignoring()
.requestMatchers(PathRequest.toStaticResources()
.atCommonLocations());
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/mypage").hasRole("USER")
.antMatchers("/messages").hasRole("MANAGER")
.antMatchers("/config").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login_proc")
.authenticationDetailsSource(authenticationDetailsSource)
.successHandler(customAuthenticationSuccessHandler)
.failureHandler(customAuthenticationFailureHandler)
.permitAll()
;
http.exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
.accessDeniedPage("/denied")
.accessDeniedHandler(accessDeniedHandler())
.and()
.addFilterBefore(customFilterSecurityInterceptor(), FilterSecurityInterceptor.class)
;
http.csrf()
.disable();
customConfigurer(http);
}
private void customConfigurer(HttpSecurity http) throws Exception {
http.apply(new AjaxLoginConfigurer<>())
.successHandlerAjax(ajaxAuthenticationSuccessHandler())
.failureHandlerAjax(ajaxAuthenticationFailureHandler())
.setAuthenticationManager(authenticationManagerBean())
.loginProcessingUrl("/api/login");
}
}
Answer 1
0
안녕하세요, 인프런 AI 인턴입니다.
AOP 관련 설정에서 AopSecurityController.preAuthorize()가 두 번 실행되는 문제에 대해 질문해 주셨습니다. 우선, 이와 관련한 질문은 아래의 유사한 형태로 언급된 적이 있습니다. 스프링 시큐리티 설정에서 불필요하게 두 번 호출되는 것을 방지하기 위한 구체적인 해결 방법에 대해 설명드리겠습니다.
관련 질문
설명 및 조치
스프링 시큐리티의 `WebSecurity` 설정에서 정적 리소스(`/static`, `/css`, `/js`, `/images`, `/webjars`)를 무시하도록 설정하셨습니다. 하지만 `AopSecurityController.preAuthorize()`가 두 번 실행되는 문제는 시큐리티 설정 상에서 불필요한 내부 호출이 있을 수 있습니다. 이를 방지하기 위해 설정의 중복 부분이나 불필요한 컴포넌트 주입이 없는지 검토해 보셔야 합니다. 1. **Web Security 설정 확인**: - 예를 들어, 동일한 패턴이나 URL에 대해 여러 번 매칭이 발생하지 않도록 설정합니다. 2. **AOP 설정 검토**: - AOP 설정이 중복 실행되거나 불필요한 지점에서 실행되지 않도록 어드바이스 호출 조건이나 포인트컷을 검토하십시오. 3. **역할 기반 접근 제어**: - 특정 메소드에서 동일한 접근 권한이 두 번 체크되고 있는지 확인해 보세요. 이와 관련하여 추가적인 정보가 필요하시다면, 관련 코드를 분석하여 말씀드릴 수 있습니다.위 방법으로 해결되지 않거나 명확한 오류 원인을 찾기 어려우시면, 궁금한 점을 더 구체적으로 알려주시면 도움이 될 것입니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.
시큐리티 공부 버전 질문
0
173
1
[해결 방법] MethodSecurityConfig.customMethodSecurityMetadataSource() 호출하지 않는 이슈
0
183
1
강의 코드가 왜이렇게 뒤죽박죽인가요...
0
246
1
메인 페이지로 접속해도 login url로 리다이렉트가 되지 않습니다..
0
233
1
파라미터값이 넘어가지 않습니다 ....
0
372
1
security filterChain 설정 질문이 있습니다.
0
328
1
소스 부분 질문 드립니다.
0
206
2
섹션4 7번 강의 문제가 있는거 같네요.
0
342
2
파일이 수시로 이름이 바껴있네요 ㄷㄷ
0
302
1
HttpSessionSecurityContextRepository를 사용안하는 문제
0
553
2
error , exception 이 잘 안됩니다.
0
275
2
thymeleaf tag 질문합니다.
0
194
2
버전업하면서 deprecated된 것들이 너무많아요
0
476
1
spring security 패치 관련
0
435
1
모바일을 사용할때 토큰말고 세션
0
843
2
DB 연동한 인가 부분에 대한 질문입니다!
0
262
1
Ajax방식도 똑같이 Session방식을 사용하는건가요?
0
305
1
Config 파일 생성 시 질문이 있습니다.
0
223
1
강사님 몇일동안 구글 검색만 100개 했는데도 이유를 모르겠습니다..
1
427
2
403 에러 뜹니다.
0
809
2
login_proc의 존재에 대한 간략한 설명입니다
0
272
1
top.html에 로그인 링크를 만들어서 로그인을 해봤습니다
0
275
2
안녕하세요. DB에 저장될 때 이해 안 가는 값이 있어서 질문드립니다!
0
186
1
안녕하세요. 익명 객체와 세션 관련 궁금증입니다.
0
288
1

