0
ログイン時以外はすべてのリクエストでインターセプタが実行されます。Spring Securityのログイン時にHandlerInterceptorAdapterが実行されない
インターセプター:WebMvcConfigurerAdapter
オン
public class MultitenantHandler extends HandlerInterceptorAdapter {
private static final Logger log = LoggerFactory.getLogger(MultitenantHandler.class);
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler){
String origin = req.getHeader("Origin");
log.debug("Origin: "+origin);
if (origin == null) {
origin = "localhost";
}
int indexDot = origin.indexOf(".");
int indexDash = origin.indexOf("://") + 3;
String tenant = "";
if (indexDot == -1) {
tenant = "experter";
log.warn("Using default tenant");
TenantContext.setCurrentTenant(tenant);
} else {
tenant = origin.substring(indexDash, indexDot);
log.info("Using tenant: " + tenant);
TenantContext.setCurrentTenant(tenant);
}
return true;
}
}
私はこの方法を登録します。
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MultitenantHandler());
}
それは私のセキュリティの設定です:
@Configuration
@EnableWebSecurity
@Profile({"development", "demo", "default"})
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final Logger log = LoggerFactory.getLogger(SecurityConfig.class);
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTLogoutSuccessHandler logoutSuccessHandler;
@Autowired
private JWTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private JWTAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private StatelessAuthenticationFilter statelessAuthFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
http.formLogin().permitAll()
.successHandler(authenticationSuccessHandler)
.failureHandler(authenticationFailureHandler);
http.logout().permitAll()
.logoutSuccessHandler(logoutSuccessHandler);
http.addFilterBefore(statelessAuthFilter, UsernamePasswordAuthenticationFilter.class);
http.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/v2/api-docs").hasRole("ADMIN")
.antMatchers("/login").permitAll()
.antMatchers("/login/changePassword").permitAll()
.antMatchers("/user/image").permitAll()
.antMatchers("/social/login/facebook").permitAll()
.antMatchers("/actuator/**").hasRole("ADMIN")
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/**").hasRole("USER");
log.info("Configuration of http complete.");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder);
}
私はインターセプタが実行されない/login
を要求すると、他の要求では、傍受者は正常に動作しないことさえあります。
URLリクエストに応じてデータベースを設定する必要があるため、リクエストの前にインターセプタを実行する必要があります。
さらに詳しい情報が必要な場合は、私がここに投稿できることを教えてください。
スプリングセキュリティは、 'DispatcherServlet'とあなたが持っているコントローラ/ハンドラの前に実行されるフィルタを使って実装されています。もちろん、それは実行されません。 'HandlerInterceptor'ではなくフィルターで実装してください。 –
ありがとうございます@ M.Deinum。しかし、すぐに私が知っているように、フィルタはこのメソッドの動作を変更しません。 http://stackoverflow.com/a/35856496/2463802 "フィルタは、マルチパートフォームやGZIP圧縮などのリクエストコンテンツとビューコンテンツの処理に適しています。これは通常、特定のコンテンツタイプにフィルタをマップする必要がある場合に表示されます(例えば、画像)、またはすべての要求に対応します。 –
フィルターの理解には欠陥があります。あなたが達成したいことはハンドラインターセプタでは実現できません。フィルタを使用する必要があります。フィルターは 'Handlerinterceptor'よりもはるかに多くを行うことができ、前述のように、Spring Securityはフィルターを使って実装されています。そのような' HandlerInterceptor'は単にオプションではありません。 –