簡體   English   中英

Spring Security:手動添加 UsernamePasswordAuthenticationFilter 觸發登錄后重定向

[英]Spring Security: manually added UsernamePasswordAuthenticationFilter triggers redirect after login

我已經開始學習 Spring Security。 我的目的是開發一個可以與 Spring Web 后端交互的單獨前端。

我目前在使用用戶名/密碼登錄時遇到一些問題。 簡而言之,我可以驗證憑據,但是我收到 403 Forbidden 錯誤。 似乎與登錄后重定向的某些行為有關。

以下是我當前的設置,應該很簡單:

在我的身份驗證服務SecurityFilterChain配置中,我禁用了 csrf 保護和默認的 Spring Security 表單登錄頁面。 同時,我允許對/login的請求以允許 POST 用戶名和密碼:

http.csrf().disable()
            .formLogin().disable()
            .authorizeRequests()
            .antMatchers("/login", "/error").permitAll()
            .anyRequest().authenticated();

之后,在相同的配置中,我定義了一個簡單的身份驗證管理器來檢查默認的 Spring 用戶名/密碼過濾器是否正常工作。 根據我在研究期間閱讀的內容,過濾器已添加到過濾器鏈中,如果登錄頁面被禁用,默認情況下它似乎不存在。

UsernamePasswordAuthenticationFilter filter =
    new UsernamePasswordAuthenticationFilter(authentication -> {
    String username = authentication.getPrincipal().toString();
    String password = authentication.getCredentials().toString();

    if (username.isEmpty() || password.isEmpty()) {
        throw new BadCredentialsException("Username or password not specified");
    }

    if (!username.equals("u") || !password.equals("p")) {
        throw new BadCredentialsException("Wrong username or password");
    }

    return new UsernamePasswordAuthenticationToken(username, null);
});

http.addFilter(filter);

現在,如果我 POST 到/login?username=u&password=p我得到 403 Forbidden 作為響應。

如果問題在身份驗證管理器內部,我已經逐行手動檢查了問題,但是使用正確的憑據,它會正確返回用戶名/密碼令牌。

我不確定在令牌返回到鏈的其余部分后我應該期待什么,但是我通過啟用日志調試注意到服務器似乎正在嘗試重定向我,這是我沒有的完全期待。

這是調試日志,它顯示在登錄后觸發了重定向:

[nio-8079-exec-3] o.s.security.web.FilterChainProxy        : Securing POST /login?username=u&password=p
[nio-8079-exec-3] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
[nio-8079-exec-3] w.a.UsernamePasswordAuthenticationFilter : Set SecurityContextHolder to UsernamePasswordAuthenticationToken [Principal=u, Credentials=[PROTECTED], Authenticated=false, Details=null, Granted Authorities=[]]
[nio-8079-exec-3] o.s.s.web.DefaultRedirectStrategy        : Redirecting to http://localhost:8079/
[nio-8079-exec-3] w.c.HttpSessionSecurityContextRepository : Stored SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=u, Credentials=[PROTECTED], Authenticated=false, Details=null, Granted Authorities=[]]] to HttpSession [org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper@7ba6bfb2]
[nio-8079-exec-3] w.c.HttpSessionSecurityContextRepository : Stored SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=u, Credentials=[PROTECTED], Authenticated=false, Details=null, Granted Authorities=[]]] to HttpSession [org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper@7ba6bfb2]
[nio-8079-exec-3] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
[nio-8079-exec-4] o.s.security.web.FilterChainProxy        : Securing POST /
[nio-8079-exec-4] w.c.HttpSessionSecurityContextRepository : Retrieved SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=u, Credentials=[PROTECTED], Authenticated=false, Details=null, Granted Authorities=[]]]
[nio-8079-exec-4] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=u, Credentials=[PROTECTED], Authenticated=false, Details=null, Granted Authorities=[]]]
[nio-8079-exec-4] o.s.s.w.s.HttpSessionRequestCache        : Loaded matching saved request http://localhost:8079/
[nio-8079-exec-4] o.s.s.a.dao.DaoAuthenticationProvider    : Failed to authenticate since no credentials provided
[nio-8079-exec-4] o.s.s.w.s.HttpSessionRequestCache        : Saved request http://localhost:8079/ to session
[nio-8079-exec-4] o.s.s.w.a.Http403ForbiddenEntryPoint     : Pre-authenticated entry point called. Rejecting access
[nio-8079-exec-4] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
[nio-8079-exec-4] w.c.HttpSessionSecurityContextRepository : Did not store empty SecurityContext
[nio-8079-exec-4] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
[nio-8079-exec-4] o.s.security.web.FilterChainProxy        : Securing POST /error
[nio-8079-exec-4] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
[nio-8079-exec-4] o.s.s.w.a.AnonymousAuthenticationFilter  : Set SecurityContextHolder to anonymous SecurityContext
[nio-8079-exec-4] o.s.security.web.FilterChainProxy        : Secured POST /error
[nio-8079-exec-4] w.c.HttpSessionSecurityContextRepository : Did not store anonymous SecurityContext
[nio-8079-exec-4] w.c.HttpSessionSecurityContextRepository : Did not store anonymous SecurityContext
[nio-8079-exec-4] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request

我可以做些什么來阻止 Spring 在登錄后重定向我的請求?

當身份驗證成功時,Spring Security 會嘗試將您重定向到http://localhost:port/ ,這個/端點是安全的,因為您配置anyRequest().authenticated()

當您return new UsernamePasswordAuthenticationToken(username, null); 在您的實現中,該UsernamePasswordAuthenticationToken構造函數會將用戶設置為NOT authenticated 為了進行身份驗證,您必須提供一些權限,例如: return new UsernamePasswordAuthenticationToken(username, null, Set.of(new SimpleGrantedAuthority("ROLE_USER"));甚至更好地return UsernamePasswordAuthenticationToken.authenticated(username, null, Set.of(new SimpleGrantedAuthority("ROLE_USER")); .

話雖如此,在這種情況下您應該使用formLogin ,因為您所做的基本上是相同的。 在談論安全性時,如果您已經編寫了自己的身份驗證機制,則不建議您編寫它。

暫無
暫無

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

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