【问题标题】:Spring Security Rest Basic authenticationSpring Security Rest 基本身份验证
【发布时间】:2016-03-20 03:42:56
【问题描述】:

我正在使用基于 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&amp;j_username=username

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

感谢您的帮助。

更新

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

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

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

    http://localhost:8080/myApp/login

    j_spring_security_check在早期版本中使用。)

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        问题总结:

        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 Basic Authentication,请参阅Spring Security Reference

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

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

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

        【讨论】:

        • 谢谢,我不想使用基本身份验证,我正在为 REST 应用程序配置 Spring Security。当我尝试使用 POST 进行身份验证时,它会给我一个 401 代码状态。我知道基本身份验证和表单登录有什么区别。
        【解决方案4】:

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

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

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

        感谢您的回答。

        【讨论】:

          猜你喜欢
          • 2016-04-30
          • 2013-05-13
          • 2011-02-11
          • 1970-01-01
          • 2013-01-11
          • 2015-07-22
          • 2014-03-18
          • 2011-08-16
          • 2021-01-27
          相关资源
          最近更新 更多