簡體   English   中英

Spring自定義身份驗證過濾器和提供者不調用控制器方法

[英]Spring custom authentication filter and provider not invoking controller method

我正在嘗試使用最新版本的 Spring Boot、Web 和 Security 來實現自定義身份驗證邏輯,但我遇到了一些問題。 我在類似的問題/教程中嘗試了許多解決方案,但沒有成功或了解實際發生的情況。

我正在創建一個具有無狀態身份驗證的 REST 應用程序,即有一個 REST 端點(/web/auth/login),它需要用戶名和密碼並返回一個字符串令牌,然后在所有其他 REST 端點(/api/ **) 來識別用戶。 我需要實現自定義解決方案,因為將來身份驗證會變得更加復雜,我想了解 Spring Security 的基礎知識。

為了實現令牌身份驗證,我正在創建一個自定義過濾器和提供程序:

過濾器:

public class TokenAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

public TokenAuthenticationFilter() {
    super(new AntPathRequestMatcher("/api/**", "GET"));
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
    String token = request.getParameter("token");
    if (token == null || token.length() == 0) {
        throw new BadCredentialsException("Missing token");
    }

    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(token, null);

    return getAuthenticationManager().authenticate(authenticationToken);
}
}

提供者:

@Component
public class TokenAuthenticationProvider implements AuthenticationProvider {
@Autowired
private AuthenticationTokenManager tokenManager;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String token = (String)authentication.getPrincipal();
    return tokenManager.getAuthenticationByToken(token);
}

@Override
public boolean supports(Class<?> authentication) {
    return UsernamePasswordAuthenticationToken.class.equals(authentication);
}
}

配置:

@EnableWebSecurity
@Order(1)
public class TokenAuthenticationSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private TokenAuthenticationProvider authProvider;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/api/**")
    .csrf().disable()
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and().addFilterBefore(authenticationFilter(), BasicAuthenticationFilter.class);
}

@Bean
public TokenAuthenticationFilter authenticationFilter() throws Exception {
    TokenAuthenticationFilter tokenProcessingFilter = new TokenAuthenticationFilter();
    tokenProcessingFilter.setAuthenticationManager(authenticationManager());
    return tokenProcessingFilter;
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authProvider);
}
}

提供程序中使用的 AuthenticationTokenManager (以及登錄過程中):

@Component
public class AuthenticationTokenManager {
private Map<String, AuthenticationToken> tokens;

public AuthenticationTokenManager() {
    tokens = new HashMap<>();
}

private String generateToken(AuthenticationToken authentication) {
    return UUID.randomUUID().toString();
}

public String addAuthentication(AuthenticationToken authentication) {
    String token = generateToken(authentication);
    tokens.put(token, authentication);
    return token;
}

public AuthenticationToken getAuthenticationByToken(String token) {
    return tokens.get(token);
}

}

會發生什么:我將請求中的有效令牌附加到“/api/bla”(這是一個返回一些 Json 的 REST 控制器)。 過濾器和提供程序都被調用。 問題是,瀏覽器被重定向到“/”而不是調用 REST 控制器的請求方法。 這似乎發生在 SavedRequestAwareAuthenticationSuccessHandler 中,但為什么要使用此處理程序?

我試過了

  • 實現一個空的成功處理程序,導致 200 狀態代碼並且仍然沒有調用控制器
  • 在簡單的 GenericFilterBean 中進行身份驗證並通過 SecurityContextHolder.getContext().setAuthentication(authentication) 設置身份驗證對象,這會導致“憑據錯誤”錯誤頁面。

我想了解為什么在我對令牌進行身份驗證后沒有調用我的控制器。 除此之外,是否有一種“Spring”方式來存儲令牌而不是將其存儲在 Map 中,例如 SecurityContextRepository 的自定義實現?

我真的很感激任何提示!

可能有點晚了,但我遇到了同樣的問題並補充說:

@Override
protected void successfulAuthentication(
        final HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain, final Authentication authResult)
        throws IOException, ServletException {
    chain.doFilter(request, response);
}

我的 AbstractAuthenticationProcessingFilter 實現成功了。

在構造函數中使用 setContinueChainBeforeSuccessfulAuthentication(true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM