@PreAuthorize hasPermission返回自定义访问被拒绝的消息,而不是访问被拒绝的页面

时间:2018-07-21 17:20:44

标签: java spring spring-security

  • 尝试实现返回定制的拒绝消息以使用有角度的客户端 * PermissionEvaluator具有实现对特定fieldName进行授权的实现(假设用户单击按钮,他必须从DB中获取自定义消息)

问题是:

hasPermission返回带有“访问被拒绝”消息的html页面

是否可以将定制的消息返回给客户端 消息-> {statusCode,消息,actionName}

@Url.GetCropUrl(Model.Content, "yourImagePropertyName", "fac")  

1 个答案:

答案 0 :(得分:0)

尝试通过

解决此问题
    @Component
    @ControllerAdvice
    public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{
    ...

    @ExceptionHandler(value = { MyAccessDeniedException.class })
        public void commence(HttpServletRequest request, HttpServletResponse response, MyAccessDeniedException ex ) throws IOException {
            String json = new ObjectMapper().writeValueAsString(ex.getRequestModel());
response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.getWriter().write(json);
        response.flushBuffer();
    }