2015-11-06 211 views
0
  1. 我有非常基本的简单的Spring Boot Rest应用程序。
  2. 我需要在Spring Security中实现自定义认证:对于每个REST请求,我需要检查每个请求(“用户名”和“密码”)的特定标题中的用户名和密码。如何禁用或重写Spring Boot中的RequestCacheAwareFilter

  3. 所以我实现的自定义AuthEntryPoint:

    @Service 
    public class CustomAuthEntryPoint implements AuthenticationEntryPoint { 
        @Override 
        public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException { 
         String username = httpServletRequest.getHeader("username"); 
         String password = httpServletRequest.getHeader("password"); 
         if (!username.equals("admin") || !password.equals("admin")) { 
          throw new RuntimeException("", new BadCredentialsException("Wrong password")); 
         } 
        } 
    }
  4. 所以,我才明白,那是RequestCacheAwareFilter缓存第一个请求和头也存储在缓存中。所以如果我用错误的传球发出请求,然后用正确的传球发送请求,我仍会得到一个异常。

那么,我该如何重写CacheAwareFilter或禁用它?还是我在做一些完全错误的事情?

+0

我刚才提出的应用无国籍喜欢这里:http://stackoverflow.com/questions/2504590/how-can-i-use-spring -security-without-sessions 现在一切都还好。 – vmakarenko

回答

2

使用自定义WebSecurityConfigurerAdapter到请求缓存设置为NullRequestCache:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.requestCache() 
      .requestCache(new NullRequestCache()); 
    } 
} 
相关问题