簡體   English   中英

Spring Boot和Jersey,無法使ExceptionMapper工作

[英]Spring boot and Jersey, can't make ExceptionMapper work

我正在使用Spring Boot + Spring Security + Jersey。

我一直在嘗試做一些事情,使ExceptionMapper發生Unathorized錯誤,但似乎不起作用。 但是,我做過的其他Mapper都能完美地工作。

這是我的代碼:

未經授權的例外:

package com.ulises.usersserver.rest.exceptionsmappers;

import com.ulises.usersserver.rest.dto.ErrorDTO;
import com.ulises.usersserver.rest.dto.ErrorDTOBuilder;

import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import static com.ulises.usersserver.constants.Constants.REQUEST_ERROR_UNATHORIZED;

public class NotAuthorizedMapper implements ExceptionMapper<NotAuthorizedException> {
    @Override
    public Response toResponse(NotAuthorizedException e) {
        final ErrorDTO errorDTO = ErrorDTOBuilder.builder()
                .message(REQUEST_ERROR_UNATHORIZED)
                .build();
        return Response.status(Response.Status.UNAUTHORIZED)
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(errorDTO)
                .build();
    }
}

完美運行的其他自定義映射器:

package com.ulises.usersserver.rest.exceptionsmappers;

import com.ulises.usersserver.rest.dto.ErrorDTO;
import com.ulises.usersserver.rest.dto.ErrorDTOBuilder;
import com.ulises.usersserver.services.exceptions.UserNotFoundException;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import static com.ulises.usersserver.constants.Constants.REQUEST_ERROR_USER_DOESNT_EXIST;

public class UserNotFoundExceptionMapper implements ExceptionMapper<UserNotFoundException> {
    @Override
    public Response toResponse(UserNotFoundException e) {
        final ErrorDTO errorDTO = ErrorDTOBuilder.builder()
                .message(REQUEST_ERROR_USER_DOESNT_EXIST)
                .build();
        return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
    }
}

他們當然是在Jersey的配置中注冊

@Bean
public ResourceConfig jerseyConfig() {
    final ResourceConfig resourceConfig = new ResourceConfig();
                      ...
    resourceConfig.register(NotFoundMapper.class);
    resourceConfig.register(NotAuthorizedMapper.class);
                      ...

    return resourceConfig;
}

我似乎無法映射其他異常,例如InternalServerErrorException (我通過做ExceptionMapper<Exception>設法映射了這個ExceptionMapper<Exception> ,這對我來說似乎不太正確。

有人知道為什么會這樣嗎? 我在這里檢查了所有可能的問題,但沒有一個解決了這個問題。

提前致謝。

好的,澤西島似乎無法控制Spring Security的異常。

解決此問題的方法(必須進行大量挖掘)是覆蓋AuthenticationEntryPoint中的方法,如果未授權用戶,則S-Security會使用該方法返回401響應。

因此,我創建了以下實現該接口的類:

public class CustomEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response, AuthenticationException authException) throws IOException {
        JSONObject json = new JSONObject();
        json.put("Message", "You don't have access to view this section. Please, log in with an authorized account.");
        response.addHeader("Content-Type", "application/json");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.getWriter().println(json);
    }
}

然后只需將此配置添加到S-Security即可使用此類作為entry point

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().authenticationEntryPoint(new CustomEntryPoint());
         ..... any other config you had .....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM