简体   繁体   English

身份验证过滤器不适用于SpringBoot

[英]Authentication Filter not working with SpringBoot

I am developing an application with Spring Boot + spring security. 我正在使用Spring Boot + Spring Security开发应用程序。 For the same i have defined a filter as. 同样,我已经定义了一个过滤器。

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
        .authorizeRequests()
        .antMatchers("/authtoken").permitAll()
        .antMatchers("/authenticate**").permitAll()
        .antMatchers("/authfailure").permitAll()
        .antMatchers("/**").authenticated()
        .and()
            .addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class)
            .addFilterAfter(accessFilter, UsernamePasswordAuthenticationFilter.class)
            .authenticationProvider(tokenAuthenticationProvider)
            .antMatcher("/**").exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);

}

Where tokenFilter is an instance of AuthTokenFilter(class that has code for authentication and is injected to using @inject) . 其中tokenFilter是AuthTokenFilter(具有身份验证代码并使用@inject注入的类)的实例。

AuthTokenFilter implements methods of Filter interface as. AuthTokenFilter将Filter接口的方法实现为。

 public class AuthTokenFilter implements Filter {
 ServletContext sc ;
 @Override
  public void init(FilterConfig fc) throws ServletException {
    sc = fc.getServletContext();
   }

  @Override
   public void doFilter(ServletRequest req, ServletResponse res, FilterChain  fc) throws IOException, ServletException {
 ------- 
 ------- 
 }

My issue is when I run the app using SpringBoot , it runs fine. 我的问题是,当我使用SpringBoot运行应用程序时,它运行正常。 But when I create a war out of it and run the same in a tomcat, neither the init nor the doFilter are invoked for the request. 但是,当我从中创建战争并在雄猫中运行战争时,不会为请求调用init或doFilter。 Hence it returns unauthorized. 因此,它返回未经授权。

Same happens for accessFilter. accessFilter也会发生同样的情况。

Does anyone know the reason behind this behavior? 有人知道这种行为背后的原因吗? or What changes I need to make to get my app working on tomcat? 或需要进行哪些更改才能使我的应用程序在Tomcat上运行?

What Tomcat version you use? 您使用哪个Tomcat版本?

I am not sure, but try this: 我不确定,但是请尝试以下操作:

In declaration of class AuthTokenFilter, add the anotation @Component 在类AuthTokenFilter的声明中,添加注释@Component

I have one project with filter and only write: 我有一个带有过滤器的项目,只写了:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SimpleCORSFilter implements Filter ...

.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM