request.getRequestURI()는 항상 "/error"입니다.

로그인 여부를 결정하기 위해 로그인 인터페이스에서 요청을 가로채었습니다. 로그인하지 않은 경우 일부 인터페이스만 사용할 수 있습니다. 이 "부분 인터페이스"는 요청된 URI에 의해 판단됩니다.

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    

        String uri = request.getRequestURI();

        boolean match = new AntPathMatcher().match("/member/**", uri);
        if (match) {
    
     // member接口(登陆,注册)可以不用登陆就使用,否则需要登陆
            return true;
        }

        //获取登录的用户信息
        // .....

        if (attribute != null) {
    
    
           // 已经登陆了
           // .....
            return true;
        } else {
    
    
            //未登录,返回登录页面
           // .....
            return false;
        }
    }

즉, /member/ 인터페이스만 허용됩니다.
그런데 uri 를 통해 얻은 결과가 "/error"인 경우가 종종 발견되는데,
그 주된 이유는 다음과 같습니다.

  1. 요청 방법이 잘못되었습니다. 예를 들어 인터페이스는 POST 방법을 사용하지만 요청은 GET 방법이므로 경로가 일치하지 않고 "/error" 경로가 반환됩니다.
  2. 매개변수 파싱 실패 : POST 방식에서 흔히 발생하는 현상 프론트엔드 매개변수를 RequestBody로 변환하지 못하면 오류가 발생합니다. 오류가 가장 많이 발생하는 곳은 일부 객체입니다. 예를 들어 제가 저지른 오류는 시간을 변환할 수 없기 때문입니다. 데이터로.
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2022-09-07T04:29:48.042Z": not a valid representation (error: Failed to parse Date value '2022-09-07T04:29:48.042Z': Unparseable date: "2022-09-07T04:29:48.042Z"); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-09-07T04:29:48.042Z": not a valid representation (error: Failed to parse Date value '2022-09-07T04:29:48.042Z': Unparseable date: "2022-09-07T04:29:48.042Z")
 at [Source: (PushbackInputStream); line: 3, column: 12] (through reference chain: top.dumbzarro.greensource.member.entity.MemberEntity["birth"])]

간단히 말해서, 메소드를 올바르게 입력하지 못하면 uri는 "/error"가 됩니다.
모든 경로 매핑 오류로 인해 "로그인되지 않음" 오류가 프런트 엔드에 반환되는 것을 방지하려면 그러면 다음과 같이 인터셉터에 판단을 하나 더 추가할 수 있습니다.

@Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    

       String uri = request.getRequestURI();
       if(uri.equals("/error")){
    
    
           // 路径映射错误
           // .....
           return false;
       }

       boolean match = new AntPathMatcher().match("/member/**", uri);
       if (match) {
    
     // member接口(登陆,注册)可以不用登陆就使用,否则需要登陆
           return true;
       }

       //获取登录的用户信息
       // .....

       if (attribute != null) {
    
    
          // 已经登陆了
          // .....
           return true;
       } else {
    
    
           //未登录,返回登录页面
          // .....
           return false;
       }
   }

Je suppose que tu aimes

Origine blog.csdn.net/weixin_45654405/article/details/126743804
conseillé
Classement