簡體   English   中英

Spring 安全登錄始終為 200

[英]Spring security login always 200

我嘗試使用 spring 安全性登錄我的 REST 服務器,/login POST 請求總是返回 HTTP 200。 即使輸入登錄的用戶不存在

    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info(s);
        UserDetails result = usersRepository.findByUsername(s).orElseThrow(() -> new UsernameNotFoundException("User not found"));
        log.info(result.toString());
        return result;
    }
        fetch("/login?username=" + this.state.username + "&password=" + this.state.password,
            {
                method: 'POST'
            }).then(resp => {
                if (resp.ok) {
                    localStorage.setItem("user", this.state.username);
                    this.props.history.push("/user");
                } else {
                    this.showError(resp.statusText);
                }
            });

您的 Spring Security 的 AuthenticationProvider 不會在 UsernameNotFound 的情況下引發所需的異常,隨后您的所有身份驗證請求都將成功。 代碼首先應該拋出所需的異常,並且應該有 AuthenticationFailureHandler 來處理這些異常。 為了認證成功,必須有 AuthenticationSuccessHandler 來顯示用戶登錄並將用戶重定向到其主頁。

暫無
暫無

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

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