【问题标题】:Spring service returns 200 but getting 404 in postmanSpring服务返回200但在邮递员中得到404
【发布时间】:2018-09-04 05:26:15
【问题描述】:

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

下一个要调用的 REST 服务是 GET_CURRENT_USER,它负责验证令牌的工作,然后是其他工作。我正在使用邮递员中的令牌来调用 GET_CURRENT_USER 服务,服务代码工作正常,我通过我的服务返回代码 200 和预期的 JSON。

但在邮递员处,我收到 404 not found 错误。我尝试过的:

  1. 删除了 CORS 过滤器

  2. 将 index.jsp 添加到项目中,我在邮递员中得到索引页面而不是 404(但这没有帮助)。

  3. 在 GET_CURRENT_USER 之后跟踪更多调用,但这会导致更多调用,并且无法跟踪究竟是什么覆盖了我的 404 响应。

  4. 尝试在代码和 POSTMAN 中使用 GET 和 POST 方法,但没有成功。我正在使用所有必需的标题和其他东西。

不知道问题出在哪里。我不能分享代码,但可以回答所有相关的问题。

任何帮助将不胜感激。

【问题讨论】:

  • 请分享一些代码
  • 404,表示您正在尝试访问不可用的URL,您可以检查一下
  • 您应该检查您的网址,404 = 未找到
  • 也许您正在原谅 /LOGIN 端点之前的一些上下文路径,例如“localhost:8080/deploymentcontext/LOGIN”?

标签: spring rest spring-security jwt postman


【解决方案1】:

当我提供默认 index.jsp 时,404 错误消失了,而我的项目中缺少该 index.jsp。但这并没有解决我的问题,因为我需要 Rest response(JSON) 结果。我从 DispatcheServlet 和 Filter 链等的长代码调试中学到了什么。如果我们在 rest 调用后需要 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>

智威汤逊:

@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 我相信通过反射它设置了 AbstractAuthenticationProcessingFilter 的 authenticationManager、successHandler

这是我在 AbstractAuthenticationProcessingFilter 中看到的以下 2 个方法

public void setAuthenticationManager(AuthenticationManager authenticationManager) {
      this.authenticationManager = authenticationManager;
     }
     public void setAuthenticationSuccessHandler(
       AuthenticationSuccessHandler successHandler) {
      Assert.notNull(successHandler, "successHandler cannot be null");
      this.successHandler = successHandler;
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多