繁体   English   中英

Spring Security Rest基本身份验证

[英]Spring Security Rest Basic authentication

我正在使用Spring security 4基于XML的配置。

这是我的配置:

      <security:http use-expressions="true" authentication-manager-ref="authenticationManager" entry-point-ref="authenticationEntryPoint">
            <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
            <security:form-login authentication-success-handler-ref="authenticationSuccessHandler"
                                 authentication-failure-handler-ref="authenticationFailureHandler"
                                 />
            <security:logout success-handler-ref="logoutSuccessHandler"/>
            <security:csrf disabled="true"/>
        </security:http>

        <security:authentication-manager id="authenticationManager">
            <security:authentication-provider>
                <security:user-service>
                    <security:user name="username" authorities="ROLE_USER" password="password"/>
                </security:user-service>
            </security:authentication-provider>
        </security:authentication-manager>

        <bean id="authenticationEntryPoint" class="package.CustomBasicAuthenticationEntryPoint">

authenticationEntryPoint具有以下实现:

public class CustomBasicAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {

        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
}

问题是当我尝试进行身份验证时:

http://localhost:8080/myApp/api/j_spring_security_check j_password=password&j_username=username with body: j_password=password&j_username=username

由于我的自定义入口点,我总是有401错误状态 在我看来, Spring安全性并没有调用身份验证管理器 我错过了什么吗?

谢谢你的帮助。

更新

感谢您的回答,我一直在使用Spring Security 3.2,我将j_username,j_password和j_spring_security_check更改为用户名,密码和登录名。 我仍然有同样的问题:401代码状态:即使我尝试使用表单(POST)进行身份验证,Spring Security也会调用自定义authenticationEntryPoint。

对于Spring Security版本4,默认登录处理URL为:

http://localhost:8080/myApp/login

(早期版本中使用了j_spring_security_check 。)

请注意,这是表单登录 ,与HTTP基本身份验证无关。

在Spring 4表单字段中,默认名称从j_usernamej_password更改为usernamepassword 您应该在请求中更改它,或者在xml配置中明确设置它。 login-processing-url默认也从j_spring_security_check更改为login 在这里你可以找到一些关于它的信息。

问题摘要:

  1. 正如@ holmis83所写,默认登录处理URL是login ,请参阅Spring Scurity Reference

    • login-processing-url映射到UsernamePasswordAuthenticationFilterfilterProcessesUrl属性。 默认值为“/ login”。
  2. 正如@pomkine所写,默认参数是usernamepassword ,请参阅Spring Security Reference

    • password-parameter包含密码的请求参数的名称。 默认为“密码”。
    • username-parameter包含用户名的请求参数的名称。 默认为“用户名”。
  3. 默认HTTP方法是POST ,请参阅UsernamePasswordAuthenticationFilter #setPostOnly

    定义此过滤器是否仅允许HTTP POST请求。 如果设置为true,并且收到的验证请求不是POST请求,则会立即引发异常并且不会尝试验证。 将调用unsuccessfulAuthentication()方法,就像处理失败的身份验证一样。 默认为true但可以由子类覆盖。

  4. 正如@ holmis83所写,您使用form-login ,这不是HTTP基本身份验证 ,请参阅Spring Security Reference

    如果要使用基本身份验证而不是表单登录,请将配置更改为

     <http use-expressions="false"> <intercept-url pattern="/**" access="ROLE_USER" /> <http-basic /> </http> 

    然后,基本身份验证将优先,并将用于在用户尝试访问受保护资源时提示登录。 如果您希望使用表单登录,则此配置中仍可使用表单登录,例如通过嵌入在其他网页中的登录表单。

在我的web.xml文件中,Spring Security过滤链的url-pattern是: /api/*

我必须在我的配置文件(登录表单)中将相同的url-pattern添加到login-processing-url: "/api/"以使其工作:

login-processing-url="/api/login" 

谢谢你的回答。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM