java - 使用 WebSecurityConfigurerAdapter 而不是 WebMvcC

我的应用有以下配置:

@EnableTransactionManagement
@EnableWebSecurity
public class SecurityApiConfiguration
  extends WebSecurityConfigurerAdapter {
  ...
}

我想添加一个 HandlerInterceptorAdapter。这可能使用 WebSecurityConfigurerAdapter 吗?我只看到使用 WebMvcConfigurerAdapter 的示例。

最佳答案

我在使用 Spring-boot 2.0 时也遇到了同样的问题,因为它不支持的原因如名称本身所说的 HandleInterceptor,总是与 WebMVCConfigurationAdpater 相关联。另一方面,由于我们使用 WebSecurityConfigureAdapter 扩展支持过滤器,我们使用 OncePerRequestFilter

public class SampleInterceptorFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        //Your custom logic
       }

   }


    @Bean
    public SampleInterceptorFilter sampleInterceptorFilter() {
        return new SampleInterceptorFilter();
    }

    @Bean
    protected FilterRegistrationBean sampleInterceptorFilterRegistor() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(sampleInterceptorFilter()());
        bean.setName("SOME_NAME_FILTER");
        bean.setEnabled(false);
        return bean;
    }


    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable();

        //Here we need to configure accordingly order of filter plays vital role
        http.addFilterAfter(cyzfilter(), ExceptionTranslationFilter.class).addFilterAfter(xyzFilter(),
                ServiceUserPreAuthFilter.class).addFilterBefore(SampleInterceptorFilter(), ServiceUserPreAuthFilter.class);

    }

关于java - 使用 WebSecurityConfigurerAdapter 而不是 WebMvcConfigurerAdapter 添加 HandlerInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044105/

相关文章:

dns - 如何让 DNS 从本地主机解析与从远程主机解析相同的内容?

android - 滚动距离 - 无障碍服务 ACTION_SCROLL_FORWARD?

git - bitbucket - 看不到 merge 分支的提交

android - Titanium SDK 5.4.0.GA 在 Android 平台的控制台上显

office365 - outlook.com rest api 不正确的组织者电子邮件

android - 使用带参数的字符串资源 (strings.xml) 时出现问题

r - 无法加载互联网例程

jquery - ng2-charts 实时数据动画

amazon-web-services - 您如何动态授予 AWS Lambda 函数访问资源的权限

android - 构建android源代码(AOSP)问题