Add a "JSONified" error management.
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
package org.codiki.core.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||||
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
import org.springframework.web.context.request.ServletWebRequest;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller that catch errors from spring rest or spring security and others, and transform them to JSON response.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/error")
|
||||||
|
public class CustomErrorController implements ErrorController {
|
||||||
|
|
||||||
|
private final ErrorAttributes errorAttributes;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public CustomErrorController(ErrorAttributes errorAttributes) {
|
||||||
|
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
|
||||||
|
this.errorAttributes = errorAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorPath() {
|
||||||
|
return "/error";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public Map<String, Object> error(HttpServletRequest request){
|
||||||
|
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
|
||||||
|
String trace = (String) body.get("trace");
|
||||||
|
if(trace != null){
|
||||||
|
String[] lines = trace.split("\n\t");
|
||||||
|
body.put("trace", lines);
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean getTraceParameter(HttpServletRequest request) {
|
||||||
|
String parameter = request.getParameter("trace");
|
||||||
|
if (parameter == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !"false".equals(parameter.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
|
||||||
|
return errorAttributes.getErrorAttributes(new ServletWebRequest(request), includeStackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,6 @@ public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
|||||||
@Override
|
@Override
|
||||||
public void commence(HttpServletRequest request, HttpServletResponse response,
|
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||||
AuthenticationException authException) throws IOException {
|
AuthenticationException authException) throws IOException {
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,18 +45,12 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests()
|
http.authorizeRequests()
|
||||||
|
// Permits all
|
||||||
.antMatchers(
|
.antMatchers(
|
||||||
"/api/account/login",
|
"/api/account/login",
|
||||||
"/api/account/logout",
|
"/api/account/logout",
|
||||||
"/api/account/signin"
|
"/api/account/signin"
|
||||||
).permitAll()
|
).permitAll()
|
||||||
.antMatchers(
|
|
||||||
"/api/images/uploadAvatar",
|
|
||||||
"/api/images/myImages",
|
|
||||||
"/api/posts/myPosts",
|
|
||||||
"/api/account/changePassword",
|
|
||||||
"/api/account/"
|
|
||||||
).authenticated()
|
|
||||||
.antMatchers(
|
.antMatchers(
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
"/api/categories",
|
"/api/categories",
|
||||||
@@ -66,6 +60,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||||||
"/api/images/**",
|
"/api/images/**",
|
||||||
"/api/posts/**"
|
"/api/posts/**"
|
||||||
).permitAll()
|
).permitAll()
|
||||||
|
.antMatchers(
|
||||||
|
"/api/images/uploadAvatar",
|
||||||
|
"/api/images/myImages",
|
||||||
|
"/api/account/changePassword",
|
||||||
|
"/api/account/",
|
||||||
|
"/api/posts/myPosts",
|
||||||
|
"/api/posts/preview",
|
||||||
|
"/api/posts/"
|
||||||
|
).authenticated()
|
||||||
.anyRequest().permitAll()
|
.anyRequest().permitAll()
|
||||||
.and()
|
.and()
|
||||||
// Allow to avoid login form at authentication failure from Angular app
|
// Allow to avoid login form at authentication failure from Angular app
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ logging:
|
|||||||
|
|
||||||
server:
|
server:
|
||||||
# use-forward-headers=true
|
# use-forward-headers=true
|
||||||
|
error:
|
||||||
|
whitelabel:
|
||||||
|
enabled: false # Disable html error responses.
|
||||||
port: 8080
|
port: 8080
|
||||||
# ssl:
|
# ssl:
|
||||||
# key-store: /home/takiguchi/Developpement/Java/codiki/keystore.p12
|
# key-store: /home/takiguchi/Developpement/Java/codiki/keystore.p12
|
||||||
|
|||||||
Reference in New Issue
Block a user