我在我的应用程序中使用JWT实现Spring安全,当进行未经授权的调用时,它将返回以下响应
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}响应json如下所示
{
"timestamp": 1497832267379,
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/path"
}不是这样,我可以发送自己的自定义响应,比如:
{
"code":401,
"message":"The request is unauthorized"
}任何帮助都是非常感谢的。
编辑
我将代码更新为以下格式:
@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();
}我的地位课程如下:
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个响应,但屏幕上没有显示任何内容。完全是空白的。任何帮助都是非常感谢的!
发布于 2017-06-19 08:20:48
您可以尝试添加控制器通知。
@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
@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
发布于 2017-06-19 03:13:11
这应该适用于你:
@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;
}
}请注意,您需要
https://stackoverflow.com/questions/44620783
复制相似问题