春季服务返回200但邮递员获得404

时间:2018-03-26 06:24:58

标签: spring rest spring-security jwt postman

我是Spring REST的新手,我想要做的是为项目创建REST端点。我还根据项目要求将JWT Spring Security实施到服务中。 第一个REST端点是/ LOGIN,在通过此服务验证用户凭据后,会在标头中将令牌分配给客户端。此令牌将保留会话以进行任何进一步的REST调用身份验证。这项服务按预期正在开展工作。

要调用的下一个REST服务是GET_CURRENT_USER,它执行验证令牌以及进一步作业的工作。我在邮递员中使用令牌来调用GET_CURRENT_USER服务,服务代码工作得很好,我通过我的服务返回代码200和预期的JSON。

但在邮递员处,我收到了404未找到的错误。我尝试过:

  1. 删除了CORS过滤器

  2. 将index.jsp添加到项目中,我在postman中获取了索引页而不是404(但是没有帮助)。

  3. 在GET_CURRENT_USER之后跟踪进一步的来电,但这导致了如此多的进一步调用,并且无法追踪到404的确切覆盖我的回复。

  4. 尝试在代码和POSTMAN中使用GET和POST方法,但没有运气。我正在使用所有标题和其他内容。

  5. 无法找出问题所在的位置。我无法共享代码,但可以回答所有相关问题。

    任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

当我提供默认的index.jsp时,404错误消失了,我的项目中缺少该错误。但它并没有解决我的问题,因为我需要在结果中使用Rest响应(JSON)。我从DispatcheServlet和Filter链等的长代码调试中学到了什么...如果我们在休息调用后需要JSON响应,并且我们不打算发送任何视图响应(如jsps),我们应该实现AuthenticationSuccessHandler并返回空隙。

最初我的spring-secuyrity.xml如下:

      <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">
<global-method-security pre-post-annotations="enabled" />
  <http pattern="/abc/login" security="none"/>
  <http pattern="/abc/**" entry-point-ref="jwtAuthenticationEntryPoint" create-session="stateless">
<csrf disabled="true"/>
<custom-filter before="FORM_LOGIN_FILTER" ref="jwtAuthorizationTokenFilter"/>
    </http>
  <beans:bean id="jwtAuthorizationTokenFilter" class="com.abc.xyz.controller.JwtAuthorizationTokenFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="<MyUserDetailsService>">
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>  
<beans:bean id="encoder"/>
<class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="10" />
</beans:bean>
</beans:beans>

我把它改成如下:

          <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">
  <global-method-security pre-post-annotations="enabled" />
  <http pattern="/abc/login" security="none"/>
  <http pattern="/abc/**" entry-point-ref="jwtAuthenticationEntryPoint" create-session="stateless">
        <csrf disabled="true"/>
        <custom-filter before="FORM_LOGIN_FILTER" ref="jwtAuthorizationTokenFilter"/>
    </http>
    <beans:bean id="jwtAuthorizationTokenFilter" class="com.abc.xyz.controller.JwtAuthorizationTokenFilter">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="authenticationSuccessHandler" ref="authSuccessHandler" />
    </beans:bean>
   <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="<MyUserDetailsService>">
            <password-encoder ref="encoder" />
        </authentication-provider>
    </authentication-manager>  

    <beans:bean id="encoder"
        class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <beans:constructor-arg name="strength" value="10" />
    </beans:bean>

    <!-- Auth Success handler -->
    <beans:bean id="authSuccessHandler" class="com.abc.xyz.controller.JwtAuthenticationSuccessHandler" />   

    </beans:beans>

JWT:

@Component
public class JwtAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
       // Not to do anything
    }
}

在我们设置2个bean属性的上面的代码中,不需要在JwtAuthorizationTokenFilter中

   

JwtAuthorizationTokenFilter扩展到AbstractAuthenticationProcessingFilter 并且我相信它通过反射设置了AuthenticationManager,AbstractAuthenticationProcessingFilter的successHandler

以下是我在AbstractAuthenticationProcessingFilter

中看到的以下两种方法
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
      this.authenticationManager = authenticationManager;
     }
     public void setAuthenticationSuccessHandler(
       AuthenticationSuccessHandler successHandler) {
      Assert.notNull(successHandler, "successHandler cannot be null");
      this.successHandler = successHandler;
     }
相关问题