首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎样才能在春天发出我自己的回应?

我怎样才能在春天发出我自己的回应?
EN

Stack Overflow用户
提问于 2017-06-19 00:29:40
回答 2查看 2K关注 0票数 1

我在我的应用程序中使用JWT实现Spring安全,当进行未经授权的调用时,它将返回以下响应

代码语言:javascript
运行
复制
@Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {

        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
    }

响应json如下所示

代码语言:javascript
运行
复制
{
"timestamp": 1497832267379,
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/path"
}

不是这样,我可以发送自己的自定义响应,比如:

代码语言:javascript
运行
复制
{
 "code":401,
 "message":"The request is unauthorized"

}

任何帮助都是非常感谢的。

编辑

我将代码更新为以下格式:

代码语言:javascript
运行
复制
 @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException {

                //response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");

                Status unauthorizedEntry = new Status();
                unauthorizedEntry.setCode(401);
                unauthorizedEntry.setMessage("Unauthorized Entry");
                Map<String, Object> unauthorizedEntryResponse = new HashMap<>();
                unauthorizedEntryResponse.put("status", unauthorizedEntry);
                objectMapper.writeValue(response.getOutputStream(), unauthorizedEntry);
                response.flushBuffer();
    }

我的地位课程如下:

代码语言:javascript
运行
复制
public class Status {

    int code;
    String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

现在我得到了200个响应,但屏幕上没有显示任何内容。完全是空白的。任何帮助都是非常感谢的!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-19 08:20:48

您可以尝试添加控制器通知。

代码语言:javascript
运行
复制
@RestController
@ControllerAdvice
public class ExceptionHandlerController {

    @ExceptionHandler(UsernameNotFoundException.class, DataAccessException.class)
    @ResponseStatus(HttpStatus.SC_UNAUTHORIZED)
    @ResponseBody ErrorInfo
    UnauthorizeExceptionInfo(HttpServletRequest req, Exception ex) {
        return new ErrorInfo(req.getRequestURL(), ex);
    } 
}

和ErrorInfo.class

代码语言:javascript
运行
复制
@JsonIgnore
public final StringBuffer url;
public final String ex;

public ErrorInfo(StringBuffer stringBuffer, Exception ex) {
    this.url = stringBuffer;
    this.ex = ex.getLocalizedMessage();
}

当您抛出一个新的UsernameNotFoundException时,控制器将处理响应。

我认为,如果密码/电子邮件不匹配,则会从loadUserByUsername中添加@Override的异常。

这里有更多详细信息:https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

票数 2
EN

Stack Overflow用户

发布于 2017-06-19 03:13:11

这应该适用于你:

代码语言:javascript
运行
复制
@Autowired
private ObjectMapper objectMapper;

@Override
public void commence(HttpServletRequest request,
                     HttpServletResponse response,
                     AuthenticationException authException) throws IOException {
    // notify client of response body content type
    response.addHeader("Content-Type", "application/json;charset=UTF-8");
    // set the response status code
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    // set up the response body
    Status unauthorized = new Status(HttpServletResponse.SC_UNAUTHORIZED,
                                     "The request is unauthorized");
    // write the response body
    objectMapper.writeValue(response.getOutputStream(), unauthorized);
    // commit the response
    response.flushBuffer();
}

public class Status {
    private int code;
    private String message;

    public Status(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

请注意,您需要

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44620783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档