来自 UI 的 Rest Spring 身份验证调用:在请求正文中传递用户名和密码,而不是在 url 参数中,以使用 Spring Security 进行身份验证

Posted

技术标签:

【中文标题】来自 UI 的 Rest Spring 身份验证调用:在请求正文中传递用户名和密码,而不是在 url 参数中,以使用 Spring Security 进行身份验证【英文标题】:Rest Spring Authentication call from UI: Pass username and password in request body, not in url parameter, to authenticate using spring security 【发布时间】:2018-08-22 12:52:21 【问题描述】:

我正在使用 Spring 安全性进行身份验证。从 UI 如果我在请求正文中发布用户名和密码,那么我会遇到 HTTP 401 错误。但是,如果我在 URL 中发布用户名和密码,那么它工作正常。

请找到我的 Spring Security 配置:

     <http auto-config="true"  use-expressions="true">

        <form-login login-processing-url="/rest/auth/j_spring_security_check"
            login-page="/rest/requiredlogin" authentication-failure-handler-ref="authFailureHandler"
            authentication-success-handler-ref="authSuccessHandler"
            username-parameter="username" password-parameter="password" />


        <logout logout-url="/rest/auth/logout" invalidate-session="true" delete-cookies="true" success-handler-ref="authLogOutSuccessHandler"/>

        <csrf disabled="true" />
    </http>

请找到我的 UI 请求:

login(userName, password): Observable<any> 

    const loginData = 'username' : userName, 'password' : password;

    const authUrlParam = 'rest/auth/j_spring_security_check';
    return post(authUrlParam, loginData);
  

post(url: string, data: any): Observable<any> 
    console.log('Posting Data to Server url : ' + environment.serverUrl + url);

    const reqOp: RequestOptions = this.getHeaders();

    const returnResponse = this.http
      .post(environment.serverUrl + url, data, reqOp)
      .map((response: Response) => response.json().data)
      .catch(this.handleError);
    return returnResponse;
  

我正在使用以下技术:

春季:4.3.9.RELEASE Spring Security:4.2.3.RELEASE 角度:5.2.7

我正在寻找一种在正文中发布凭据的解决方案。否则,如果我在 URL 中发布,那么任何人都可以看到凭据。

【问题讨论】:

您是否尝试将 usernamepassword 作为 URL 搜索参数字符串而不是 JSON 数据传递给您的发布请求?例如使用 URLSearchParams() 我看到了。我要说的是而不是JSON,您是否尝试在帖子正文中传递username=someusername&amp;password=somepassword 字符串? @mj_1,它有效。谢谢。 【参考方案1】:

我终于找到了解决办法。

现在我的 loginData,即我发布到服务器进行身份验证的凭据,是一个简单的字符串,例如“username=someusername&password=somepassword”。另请注意,我在请求中发送了content-type: application/x-www-form-urlencoded

login(userName, password): Observable<any> 

    const loginData = 'username=' + userName + '&password=' + password;

    const authUrlParam = 'rest/auth/j_spring_security_check';
    return this.ajaxService.postForLogin(authUrlParam, loginData, 'application/x-www-form-urlencoded');
  

postForLogin(url: string, data: any, contentType: string): Observable<any> 
    console.log('Posting Data to Server url : ' + environment.serverUrl + url);

    const headers = new Headers();
    headers.append('Content-Type', contentType);
    const reqOp = new RequestOptions( headers: headers, withCredentials: true );

    const returnResponse = this.http
      .post(environment.serverUrl + url, data, reqOp)
      .map((response: Response) => response.json().data)
      .catch(this.handleError);
    return returnResponse;
  

【讨论】:

以上是关于来自 UI 的 Rest Spring 身份验证调用:在请求正文中传递用户名和密码,而不是在 url 参数中,以使用 Spring Security 进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring 安全性中使用 rest 服务进行身份验证和授权 [关闭]

使用 spring security 和 mongodb 进行 Rest 身份验证

Spring MVC REST + Spring Security + 基本身份验证

使用 Spring 和 Spring Security 进行 REST 身份验证

通过 REST 在 Spring MVC 中进行身份验证

Spring Data REST 中的身份验证和授权