Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring安全Rest基本身份验证_Java_Spring_Spring Security - Fatal编程技术网

Java Spring安全Rest基本身份验证

Java Spring安全Rest基本身份验证,java,spring,spring-security,Java,Spring,Spring Security,我正在使用SpringSecurity4XML配置 这是我的配置: <security:http use-expressions="true" authentication-manager-ref="authenticationManager" entry-point-ref="authenticationEntryPoint"> <security:intercept-url pattern="/**" access="hasRole('R

我正在使用SpringSecurity4XML配置

这是我的配置:

      <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">
当我尝试验证时,问题是

http://localhost:8080/myApp/api/j_spring_security_check
带正文:
j_password=password&j_username=username

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

谢谢你的帮助

更新


感谢您的回答,我一直在使用SpringSecurity3.2,我将j_用户名、j_密码和j_Spring_安全检查更改为用户名、密码和登录。我仍然有相同的问题:401代码状态:即使我尝试使用表单(POST)进行身份验证,Spring Security仍在调用自定义authenticationEntryPoint。

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

http://localhost:8080/myApp/login

j_spring_security_check
在早期版本中使用。)


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

在Spring 4表单字段中,默认名称从
j_用户名
j_密码
更改为
用户名
密码
。您应该在请求中更改它,或者在xml配置中显式设置它。另外,登录处理url默认值从
j\u spring\u security\u check
更改为
login
。 您可以找到一些关于它的信息。

问题摘要:

  • 如前所述,默认登录处理URL为
    login
    ,请参阅:

    • 登录处理url映射到
      UsernamePasswordAuthenticationFilter
      FilterProcessURL
      属性。默认值为“/登录”
  • 如前所述,默认参数为
    用户名
    密码
    ,请参阅:

    • 密码参数包含密码的请求参数的名称。默认为“密码”
    • 用户名参数包含用户名的请求参数的名称。默认为“用户名”
  • 默认HTTP方法是
    POST
    ,请参阅:

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

  • 如前所述,您使用的是
    表单登录
    ,而这不是,请参阅:

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

    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER" />
        <http-basic />
    </http>
    
    
    
    然后,基本身份验证将优先,并将用于在用户尝试访问受保护的资源时提示登录。如果您希望使用表单登录,则表单登录在此配置中仍然可用,例如通过嵌入在另一个网页中的登录表单


  • 在我的
    web.xml
    文件中,Spring Security filterchain的url模式是:
    /api/*

    我必须向配置文件(登录表单)中的登录处理url添加相同的url模式:
    “/api/”
    ,以使其正常工作:

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

    谢谢你的回答

    谢谢,我不想使用基本身份验证,我正在为REST应用程序配置spring安全性。当我尝试使用POST进行身份验证时,它会给我一个401代码状态。我知道基本身份验证和表单登录之间的区别。
    login-processing-url="/api/login"