首页 文章

Spring Security Rest基本身份验证

提问于
浏览
4

我正在使用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);
    }
}

The problem is 当我尝试进行身份验证时:

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

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

谢谢你的帮助 .

Update

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

4 回答

  • 1

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

    http://localhost:8080/myApp/login

    j_spring_security_check 在早期版本中使用过 . )

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

  • 2

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

  • 3

    问题摘要:

    login-processing-url映射到UsernamePasswordAuthenticationFilter的filterProcessesUrl属性 . 默认值为“/ login” .

    password-parameter包含密码的请求参数的名称 . 默认为“密码” . username-parameter包含用户名的请求参数的名称 . 默认为“用户名” .

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

    如果要使用基本身份验证而不是表单登录,请将配置更改为<http use-expressions =“false”>
    <intercept-url pattern =“/ **”access =“ROLE_USER”/>

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

  • 0

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

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

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

    谢谢你的回答 .

相关问题