Spring Boot Custom Exception showing most peculiar Behavior - spring

Note - I have already set the flag server.error.include-message=always in application.properties
I have made a custom exception which should give a message to client when raised. However, that does not seem to be working whenever error code is 401/403. In that case I only receive a 401/403 status code with no response body at all, like below.
As soon as I change the status code to anything else, I start getting proper response body, like this.
BadCredentialsException.java
#ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public class BadCredentialsException extends RuntimeException{
// Runtime exception just needs this, I guess :/
private static final long serialVersionUID = 1;
public BadCredentialsException(String message){
super(message);
}
}
See First antMatchers, that's where the concerned endpoint is.
SecurityConfiguration.java
#Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeRequests().
antMatchers(HttpMethod.POST, "/api/v2/user/login/**").permitAll().
antMatchers(HttpMethod.POST, "/api/v2/user/", "/api/v2/user", "/api/v2/user/change-role/**").hasAuthority("ROOT").
antMatchers(HttpMethod.GET, "/api/v2/user/", "/api/v2/user").hasAuthority("ROOT").
antMatchers(HttpMethod.POST, "/api/v1/customers/", "/api/v1/customers").hasAnyAuthority("ADMIN", "ROOT").
antMatchers(HttpMethod.GET, "/api/v1/customers/", "/api/v1/customers").hasAnyAuthority("EMPLOYEE", "ADMIN", "ROOT").
anyRequest().
authenticated().
and().
httpBasic();
}
My Thoughts and Observations
-> I can see my message in the terminal, that means the exception is definitely being raised. I have also tried logging something out in the BadCredentialsException file to see if its being raised or not, and yes it seems to be working.
-> There is a possibility that spring security might be deleting the response body on such status codes, although that's only a wild guess. I am going to try and disable spring security for a bit and see if I receive proper response body or not.
UPDATE - I have modified the post so that only currently relevant questions are being shown.

I'm not sure if I understood your post correctly.
But anyways, I advice you to use CURL or other http testing tool such as SoapUi to test the authentication.
Because I guess Postman caches the Cookies returned by the request.
A correct authentication attempt returns the header "Set-Cookie: JSESSIONID=SESSION ID".
Postman sends that cookie in the next requests, thats why the authentication still works after you change the password.
Only if you change the username the cookie is not sent.
Using CURL or SoapUi will prevent that from happen.
Regarding the body being returned or not, I guess the only way to control the response body using spring security is customizing the BasicAuthenticationEntryPoint.

Related

Why Rest End point is not showing any json data while using POST method in POSTMAN application?

I am trying to implement sample spring boot project and to ensure my endpoints are working properly, i'm using POSTMAN. When using POSTMAN , I am not able to see the response(i.e in Pretty) for a POST request. But the Status is 200 OK and I am able to see the result using GET request.
No Pretty response for POST request
GET Response ensuring that the previous POST request works fine
And my controller code is the following
#PostMapping("/message")
public Message createMessage(#RequestBody Message message)
{
return service.createMessage(message);
}
Can anyone help me to find out why I am not able to see the result while using POST method please?
Like Rafael says it is good to return a Response with the object entity. I haven't been working with Spring myself but with JavaEE and in JavaEE it is perfectly possible to return the object directly without using a Response. I use Responses anyways though, because it is much nicer to work with, and you can create your own custom responses and status codes.
Maybe check if your createUser service actually returns a message.
I don't know much about Spring, but usually what works for me is using a ResponseEntity as the object returned by the function. Also, maybe you should use #RestController as the annotation to your class controller
#PostMapping("/message")
public ResponseEntity<Message> createMessage(#RequestBody Message message)
{
Message msg = service.createMessage(message);
return ResponseEntity.ok(msg);
}

Spring PostMapping return 401 without body

I want to make a Post to write some data into the database, but all needed information is stored on the server, so my Post service requires no body:
#PostMapping("foo")
public #ResponseBody
RestResponse writeFoo() {
// WRITE AND RETURN
}
If I try to make a post request to this service I receive 401 even if I pass a valid token. If I change my exposed service to a GetMapping all works as expected. It seems that I can't manage a Post request with an empty body.
I've tried adding some fake parameters as
RestResponse writeFoo(#RequestBody(required = false) String fake)
but without success.
Any idea?
The issue you explain is most commonly the cause of bad (or missing?) configuration.
Pay attention that i.e. GET method is allowed by default by your REST API, while you need to specify other method types (i.e. PUT and POST), otherwise it won't work out of the box due to CORS.
The part where GET method works while POST method doesn't is a strong hint towards missing/incorrect CORS configuration. You can fix it quickly by adding some CORS filter and setup your response headers.
The official documentation should give you a good start, if you don't know where to look for: Spring docs - enabling CORS
UPDATE:
The issue is successfully resolved, check comments section for more info.
Short story - back-end configuration for CORS/CSRF token was set up correctly in this particular case, the issue occurred due to missing header (CSRF token) on the angular/front-end part of the webapp.

Spring Exception Handler returns partial response - maybe bug?

I am using spring boot 2.0.4 to build a REST interface.
Iam using ExceptionHandler to handle errors.
Problem:
When an Exception was raised while serialization a JsonMappingException is thrown by jackson. This exception I handle with a default exception Handler (catching all Exceptions).
#ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
#ResponseBody
#ExceptionHandler(Throwable.class)
public String handleThrowable(final Throwable throwable) {
return "SERVER ERROR";
}
Now I the have the strange behavior when I returning a large list of Objects, and one fails because of an RuntimeException, I do not get a 500 back. But I get a 200 with the first serialized objects and then the error message.
Example:
I have prepared a fully working example of the problem:
https://github.com/stefan0001/spring-exception-test/tree/master/src/main/java/test
When calling:
GET http://localhost:8080/test
Expected Result:
Status: 500
Body: SERVER ERROR
Actual Result:
Status: 200
Body:
[{"name":"0","throwException":false,"foo":"bar"},{"name":"1","throwException":false,"foo":"bar"},{"name":"2","throwException":false,"foo":"bar"},{"name":"3","throwException":false,"foo":"bar"},{"name":"4","throwException":false,"foo":"bar"},{"name":"5","throwException":false,"foo":"bar"},{"name":"6","throwException":false,"foo":"bar"},{"name":"7","throwException":false,"foo":"bar"},{"name":"8","throwException":false,"foo":"bar"},{"name":"9","throwException":false,"foo":"bar"},{"name":"10","throwException":false,"foo":"bar"},{"name":"11","throwException":false,"foo":"bar"},{"name":"12","throwException":false,"foo":"bar"},{"name":"13","throwException":false,"foo":"bar"},{"name":"14","throwException":false,"foo":"bar"},{"name":"15","throwException":false,"foo":"bar"},{"name":"16","throwException":false,"foo":"bar"},{"name":"17","throwException":false,"foo":"bar"},{"name":"18","throwException":false,"foo":"bar"},{"name":"19","throwException":false,"foo":"bar"},{"name":"20","throwException":false,"foo":"bar"},{"name":"21","throwException":false,"foo":"bar"},{"name":"22","throwException":false,"foo":"bar"},{"name":"23","throwException":false,"foo":"bar"},{"name":"24","throwException":false,"foo":"bar"},{"name":"25","throwException":false,"foo":"bar"},{"name":"26","throwException":false,"foo":"bar"},{"name":"27","throwException":false,"foo":"bar"},{"name":"28","throwException":false,"foo":"bar"},{"name":"29","throwException":false,"foo":"bar"},{"name":"30","throwException":false,"foo":"bar"},{"name":"31","throwException":false,"foo":"bar"},{"name":"32","throwException":false,"foo":"bar"},{"name":"33","throwException":false,"foo":"bar"},{"name":"34","throwException":false,"foo":"bar"},{"name":"35","throwException":false,"foo":"bar"},{"name":"36","throwException":false,"foo":"bar"},{"name":"37","throwException":false,"foo":"bar"},{"name":"38","throwException":false,"foo":"bar"},{"name":"39","throwException":false,"foo":"bar"},{"name":"40","throwException":false,"foo":"bar"},{"name":"41","throwException":false,"foo":"bar"},{"name":"42","throwException":false,"foo":"bar"},{"name":"43","throwException":false,"foo":"bar"},{"name":"44","throwException":false,"foo":"bar"},{"name":"45","throwException":false,"foo":"bar"},{"name":"46","throwException":false,"foo":"bar"},{"name":"47","throwException":false,"foo":"bar"},{"name":"48","throwException":false,"foo":"bar"},{"name":"49","throwException":false,"foo":"bar"},{"name":"50","throwException":false,"foo":"bar"},{"name":"51","throwException":false,"foo":"bar"},{"name":"52","throwException":false,"foo":"bar"},{"name":"53","throwException":false,"foo":"bar"},{"name":"54","throwException":false,"foo":"bar"},{"name":"55","throwException":false,"foo":"bar"},{"name":"56","throwException":false,"foo":"bar"},{"name":"57","throwException":false,"foo":"bar"},{"name":"58","throwException":false,"foo":"bar"},{"name":"59","throwException":false,"foo":"bar"},{"name":"60","throwException":false,"foo":"bar"},{"name":"61","throwException":false,"foo":"bar"},{"name":"62","throwException":false,"foo":"bar"},{"name":"63","throwException":false,"foo":"bar"},{"name":"64","throwException":false,"foo":"bar"},{"name":"65","throwException":false,"foo":"bar"},{"name":"66","throwException":false,"foo":"bar"},{"name":"67","throwException":false,"foo":"bar"},{"name":"68","throwException":false,"foo":"bar"},{"name":"69","throwException":false,"foo":"bar"},{"name":"70","throwException":false,"foo":"bar"},{"name":"71","throwException":false,"foo":"bar"},{"name":"72","throwException":false,"foo":"bar"},{"name":"73","throwException":false,"foo":"bar"},{"name":"74","throwException":false,"foo":"bar"},{"name":"75","throwException":false,"foo":"bar"},{"name":"76","throwException":false,"foo":"bar"},{"name":"77","throwException":false,"foo":"bar"},{"name":"78","throwException":false,"foo":"bar"},{"name":"79","throwException":false,"foo":"bar"},{"name":"80","throwException":false,"foo":"bar"},{"name":"81","throwException":false,"foo":"bar"},{"name":"82","throwException":false,"foo":"bar"},{"name":"83","throwException":false,"foo":"bar"},{"name":"84","throwException":false,"foo":"bar"},{"name":"85","throwException":false,"foo":"bar"},{"name":"86","throwException":false,"foo":"bar"},{"name":"87","throwException":false,"foo":"bar"},{"name":"88","throwException":false,"foo":"bar"},{"name":"89","throwException":false,"foo":"bar"},{"name":"90","throwException":false,"foo":"bar"},{"name":"91","throwException":false,"foo":"bar"},{"name":"92","throwException":false,"foo":"bar"},{"name":"93","throwException":false,"foo":"bar"},{"name":"94","throwException":false,"foo":"bar"},{"name":"95","throwException":false,"foo":"bar"},{"name":"96","throwException":false,"foo":"bar"},{"name":"97","throwException":false,"foo":"bar"},{"name":"98","throwException":false,"foo":"bar"},{"name":"99","throwException":false,"foo":"bar"},{"name":"100","throwException":false,"foo":"bar"},{"name":"101","throwException":false,"foo":"bar"},{"name":"102","throwException":false,"foo":"bar"},{"name":"103","throwException":false,"foo":"bar"},{"name":"104","throwException":false,"foo":"bar"},{"name":"105","throwException":false,"foo":"bar"},{"name":"106","throwException":false,"foo":"bar"},{"name":"107","throwException":false,"foo":"bar"},{"name":"108","throwException":false,"foo":"bar"},{"name":"109","throwException":false,"foo":"bar"},{"name":"110","throwException":false,"foo":"bar"},{"name":"111","throwException":false,"foo":"bar"},{"name":"112","throwException":false,"foo":"bar"},{"name":"113","throwException":false,"foo":"bar"},{"name":"114","throwException":false,"foo":"bar"},{"name":"115","throwException":false,"foo":"bar"},{"name":"116","throwException":false,"foo":"bar"},{"name":"117","throwException":false,"foo":"bar"},{"name":"118","throwException":false,"foo":"bar"},{"name":"119","throwException":false,"foo":"bar"},{"name":"120","throwException":false,"foo":"bar"},{"name":"121","throwException":false,"foo":"bar"},{"name":"122","throwException":false,"foo":"bar"},{"name":"123","throwException":false,"foo":"bar"},{"name":"124","throwException":false,"foo":"bar"},{"name":"125","throwException":false,"foo":"bar"},{"name":"126","throwException":false,"foo":"bar"},{"name":"127","throwException":false,"foo":"bar"},{"name":"128","throwException":false,"foo":"bar"},{"name":"129","throwException":false,"foo":"bar"},{"name":"130","throwException":false,"foo":"bar"},{"name":"131","throwException":false,"foo":"bar"},{"name":"132","throwException":false,"foo":"bar"},{"name":"133","throwException":false,"foo":"bar"},{"name":"134","throwException":false,"foo":"bar"},{"name":"135","throwException":false,"foo":"bar"},{"name":"136","throwException":false,"foo":"bar"},{"name":"137","throwException":false,"foo":"bar"},{"name":"138","throwException":false,"foo":"bar"},{"name":"139","throwException":false,"foo":"bar"},{"name":"140","throwException":false,"foo":"bar"},{"name":"141","throwException":false,"foo":"bar"},{"name":"142","throwException":false,"foo":"bar"},{"name":"143","throwException":false,"foo":"bar"},{"name":"144","throwException":false,"foo":"bar"},{"name":"145","throwException":false,"foo":"bar"},{"name":"146","throwException":false,"foo":"bar"},{"name":"147","throwException":false,"foo":"bar"},{"name":"148","throwException":false,"foo":"bar"},{"name":"149","throwException":false,"foo":"bar"},{"name":"150","throwException":false,"foo":"bar"},{"name":"151","throwException":false,"foo":"bar"},{"name":"152","throwException":false,"foo":"bar"},{"name":"153","throwException":false,"foo":"bar"},{"name":"154","throwException":false,"foo":"bar"},{"name":"155","throwException":false,"foo":"bar"},{"name":"156","throwException":false,"foo":"bar"},{"name":"157","throwException":false,"foo":"bar"},{"name":"158","throwException":false,"foo":"bar"},{"name":"159","throwException":false,"foo":"bar"},{"name":"160","throwException":false,"foo":"bar"},{"name":"161","throwException":false,"foo":"bar"},{"name":"162","throwException":false,"foo":"bar"},{"name":"163","throwException":false,"foo":"bar"},{"name":"164","throwException":false,"foo":"bar"},{"name":"165","throwException":false,"foo":"bar"},{"name":"166","throwException":false,"foo":"bar"},{"name":"167","throwException":false,"foo":"bar"},{"name":"168","throwException":false,"foo":"bar"},{"name":"169","throwException":false,"foo":"bar"},{"name":"170","throwException":false,"foo":"bar"},{"name":"171","throwException":false,"foo":"bar"},{"name":"172","throwException":false,"foo":"bar"},{"name":"173","throwException":false,"foo":"bar"},{"name":"174","throwException":false,"foo":"bar"},{"name":"175","throwException":false,"foo":"bar"},{"name":"176","throwException":false,"foo":"bar"},{"name":"177","throwException":false,"foo":"bar"},{"name":"178","throwException":false,"foo":"bar"},{"name":"179","throwException":false,"foo":"bar"},{"name":"180","throwException":false,"foo":"bar"},{"name":"181","throwException":false,"foo":"bar"},{"name":"182","throwException":false,"foo":"bar"},{"name":"183","throwException":false,"foo":"bar"},{"name":"184","throwException":false,"foo":"bar"},{"name":"185","throwException":false,"foo":"bar"},{"name":"186","throwException":false,"foo":"bar"},{"name":"187","throwException":false,"foo":"bar"},{"name":"188","throwException":false,"foo":"bar"},{"name":"189","throwException":false,"foo":"bar"},{"name":"190","throwException":false,"foo":"bar"},{"name":"191","throwException":false,"foo":"bar"},{"name":"192","throwException":false,"foo":"bar"},{"name":"193","throwException":false,"foo":"bar"},{"name":"194","throwException":false,"foo":"bar"},{"name":"195","throwException":false,"foo":"bar"},{"name":"196","throwException":false,"foo":"bar"},{"name":"197","throwException":false,"foo":"bar"},{"name":"198","throwException":false,"foo":"bar"},{"name":"199","throwException":false,"foo":"bar"},{"name":"200","throwException":false,"foo":"bar"},{"name":"201","throwException":false,"foo":"bar"},{"name":"202","throwException":false,"foo":"bar"},{"name":"203","throwException":false,"foo":"bar"},{"name":"204","throwException":false,"foo":"bar"},{"name":"205","throwException":false,"foo":"bar"},{"name":"206","throwException":false,"foo":"bar"},{"name":"207","throwException":false,"foo":"bar"},{"name":"208","throwException":false,"foo":"bar"},{"name":"209","throwException":false,"foo":"bar"},{"name":"210","throwException":false,"foo":"bar"},{"name":"211","throwException":false,"foo":"bar"},{"name":"212","throwException":false,"foo":"bar"},{"name":"213","throwException":false,"foo":"bar"},{"name":"214","throwException":false,"foo":"bar"},{"name":"215","throwException":false,"foo":"bar"},{"name":"216","throwException":false,"foo":"bar"},{"name":"217","throwException":false,"foo":"bar"},{"name":"218","throwException":false,"foo":"bar"},{"name":"219","throwException":false,"foo":"bar"},{"name":"220","throwException":false,"foo":"bar"},{"name":"221","throwException":false,"foo":"bar"},{"name":"222","throwException":false,"foo":"bar"},{"name":"223","throwException":false,"foo":"bar"},{"name":"224","throwException":false,"foo":"bar"},{"name":"225","throwException":false,"foo":"bar"},{"name":"226","throwException":false,"foo":"bar"},{"name":"227","throwException":false,"foo":"bar"},{"name":"228","throwException":false,"foo":"bar"},{"name":"229","throwException":false,"foo":"bar"},{"name":"230","throwException":false,"foo":"bar"},{"name":"231","throwException":false,"foo":"bar"},{"name":"232","throwException":false,"foo":"bar"},{"name":"233","throwException":false,"foo":"bar"},{"name":"234","throwException":false,"foo":"bar"},{"name":"235","throwException":false,"foo":"bar"},{"name":"236","throwException":false,"foo":"bar"},{"name":"237","throwException":false,"foo":"bar"},{"name":"238","throwException":false,"foo":"bar"},{"name":"239","throwException":false,"foo":"bar"},{"name":"240","throwException":false,"foo":"bar"},{"name":"241","throwException":false,"foo":"bar"},{"name":"242","throwException":false,"foo":"bar"},{"name":"243","throwException":false,"foo":"bar"},{"name":"244","throwException":false,"foo":"bar"},{"name":"245","throwException":false,"foo":"bar"},{"name":"246","throwException":false,"foo":"bar"},{"name":"247","throwException":false,"foo":"bar"},{"name":"248","throwException":false,"foo":"bar"},{"name":"249","throwException":false,"foo":"bar"},{"name":"250","throwException":false,"foo":"bar"},{"name":"251","throwException":false,"foo":"bar"},{"name":"252","throwException":false,"foo":"bar"},{"name":"253","throwException":false,"foo":"bar"},{"name":"254","throwException":false,"foo":"bar"},{"name":"255","throwException":false,"foo":"bar"},{"name":"256","throwException":false,"foo":"bar"},{"name":"257","throwException":false,"foo":"bar"},{"name":"258","throwException":false,"foo":"bar"},{"name":"259","throwException":false,"foo":"bar"},{"name":"260","throwException":false,"foo":"bar"},{"name":"261","throwException":false,"foo":"bar"},{"name":"262","throwException":false,"foo":"bar"},{"name":"263","throwException":false,"foo":"bar"},{"name":"264","throwException":false,"foo":"bar"},{"name":"265","throwException":false,"foo":"bar"},{"name":"266","throwException":false,"foo":"bar"},{"name":"267","throwException":false,"foo":"bar"},{"name":"268","throwException":false,"foo":"bar"},{"name":"269","throwException":false,"foo":"bar"},{"name":"270","throwException":false,"foo":"bar"},{"name":"271","throwException":false,"foo":"bar"},{"name":"272","throwException":false,"foo":"bar"},{"name":"273","throwException":false,"foo":"bar"},{"name":"274","throwException":false,"foo":"bar"},{"name":"275","throwException":false,"foo":"bar"},{"name":"276","throwException":false,"foo":"bar"},{"name":"277","throwException":false,"foo":"bar"},{"name":"278","throwException":false,"foo":"bar"},{"name":"279","throwException":false,"foo":"bar"},{"name":"280","throwException":false,"foo":"bar"},{"name":"281","throwException":false,"foo":"bar"},{"name":"282","throwException":false,"foo":"bar"},{"name":"283","throwException":false,"foo":"bar"},{"name":"284","throwException":false,"foo":"bar"},{"name":"285","throwException":false,"foo":"bar"},{"name":"286","throwException":false,"foo":"bar"},{"name":"287","throwException":false,"foo":"bar"},{"name":"288","throwException":false,"foo":"bar"},{"name":"289","throwException":false,"foo":"bar"},{"name":"290","throwException":false,"foo":"bar"},{"name":"291","throwException":false,"foo":"bar"},{"name":"292","throwException":false,"foo":"bar"},{"name":"293","throwException":false,"foo":"bar"},{"name":"294","throwException":false,"foo":"bar"},{"name":"295","throwException":false,"foo":"bar"},{"name":"296","throwException":false,"foo":"bar"},{"name":"297","throwException":false,"foo":"bar"},{"name":"298","throwException":false,"foo":"bar"},{"name":"299","throwException":false,"foo":"bar"},{"name":"300","throwException":false,"foo":"bar"},{"name":"301","throwException":false,"foo":"bar"},{"name":"302","throwException":false,"foo":"bar"},{"name":"303","throwException":false,"foo":"bar"},{"name":"304","throwException":false,"foo":"bar"},{"name":"305","throwException":false,"foo":"bar"},{"name":"306","throwException":false,"foo":"bar"},{"name":"307","throwException":false,"foo":"bar"},{"name":"308","throwException":false,"foo":"bar"},{"name":"309","throwException":false,"foo":"bar"},{"name":"310","throwException":false,"foo":"bar"},{"name":"311","throwException":false,"foo":"bar"},{"name":"312","throwException":false,"foo":"bar"},{"name":"313","throwException":false,"foo":"bar"},{"name":"314","throwException":false,"foo":"bar"},{"name":"315","throwException":false,"foo":"bar"},{"name":"316","throwException":false,"foo":"bar"},{"name":"317","throwException":false,"foo":"bar"},{"name":"318","throwException":false,"foo":"bar"},{"name":"319","throwException":false,"foo":"bar"},{"name":"320","throwException":false,"foo":"bar"},{"name":"321","throwException":false,"foo":"bar"},{"name":"322","throwException":false,"foo":"bar"},{"name":"323","throwException":false,"foo":"bar"},{"name":"324","throwException":false,"foo":"bar"},{"name":"325","throwException":false,"foo":"bar"},{"name":"326","throwException":false,"foo":"bar"},{"name":"327","throwException":false,"foo":"bar"},{"name":"328","throwException":false,"foo":"bar"},{"name":"329","throwException":false,"foo":"bar"},{"name":"330","throwException":false,"foo":"bar"},{"name":"331","throwException":false,"foo":"bar"},{"name":"332","throwException":false,"foo":"bar"},{"name":"333","throwException":false,"foo":"bar"},{"name":"334","throwException":false,"foo":"bar"},{"name":"335","throwException":false,"foo":"bar"},{"name":"336","throwException":false,"foo":"bar"},{"name":"337","throwException":false,"foo":"bar"},{"name":"338","throwException":false,"foo":"bar"},{"name":"339","throwException":false,"foo":"bar"},{"name":"340","throwException":false,"foo":"bar"},{"name":"341","throwException":false,"foo":"bar"},{"name":"342","throwException":false,"foo":"bar"},{"name":"343","throwException":false,"foo":"bar"},{"name":"344","throwException":false,"foo":"bar"},{"name":"345","throwException":false,"foo":"bar"},{"name":"346","throwException":false,"foo":"bar"},{"name":"347","throwException":false,"foo":"bar"},{"name":"348","throwException":false,"foo":"bar"},{"name":"349","throwException":false,"foo":"bar"},{"name":"350","throwException":false,"foo":"bar"},{"name":"351","throwException":false,"foo":"bar"},{"name":"352","throwException":false,"foo":"bar"},{"name":"353","throwException":false,"foo":"bar"},{"name":"354","throwException":false,"foo":"bar"},{"name":"355","throwException":false,"foo":"bar"},{"name":"356","throwException":false,"foo":"bar"},{"name":"357","throwException":false,"foo":"bar"},{"name":"358","throwException":false,"foo":"bar"},{"name":"359","throwException":false,"foo":"bar"},{"name":"360","throwException":false,"foo":"bar"},{"name":"361","throwException":false,"foo":"bar"},{"name":"362","throwException":false,"foo":"bar"},{"name":"363","throwException":false,"foo":"bar"},{"name":"364","throwException":false,"foo":"bar"},{"name":"365","throwException":false,"foo":"bar"},{"name":"366","throwException":false,"foo":"bar"},{"name":"367","throwException":false,"foo":"bar"},{"name":"368","throwException":false,"foo":"bar"},{"name":"369","throwException":false,"foo":"bar"},{"name":"370","throwException":false,"foo":"bar"},{"name":"371","throwException":false,"foo":"bar"},{"name":"372","throwException":false,"foo":"bar"},{"name":"373","throwException":false,"foo":"bar"},{"name":"374","throwException":false,"foo":"bar"},{"name":"375","throwException":false,"foo":"bar"},{"name":"376","throwException":false,"foo":"bar"},{"name":"377","throwException":false,"foo":"bar"},{"name":"378","throwException":false,"foo":"bar"},{"name":"379","throwException":false,"foo":"bar"},{"name":"380","throwException":false,"foo":"bar"},{"name":"381","throwException":false,"foo":"bar"},{"name":"382","throwException":false,"foo":"bar"},{"name":"383","throwException":false,"foo":"bar"},{"name":"384","throwException":false,"foo":"bar"},{"name":"385","throwException":false,"foo":"bar"},{"name":"386","throwException":false,"foo":"bar"},{"name":"387","throwException":false,"foo":"bar"},{"name":"388","throwException":false,"foo":"bar"},{"name":"389","throwException":false,"foo":"bar"},{"name":"390","throwException":false,"foo":"bar"},{"name":"391","throwException":false,"foo":"bar"},{"name":"392","throwException":false,"foo":"bar"},{"name":"393","throwException":false,"foo":"bar"},{"name":"394","throwException":false,"foo":"bar"},{"name":"395","throwException":false,"foo":"bar"},{"name":"396","throwException":false,"foo":"bar"},{"name":"397","throwException":false,"foo":"bar"},{"name":"398","throwException":false,"foo":"bar"},{"name":"399","throwException":false,"foo":"bar"},{"name":"400","throwException":false,"foo":"bar"},{"name":"401","throwException":false,"foo":"bar"},{"name":"402","throwException":false,"foo":"bar"},{"name":"403","throwException":false,"foo":"bar"},{"name":"404","throwException":false,"foo":"bar"},{"name":"405","throwException":false,"foo":"bar"},{"name":"406","throwException":false,"foo":"bar"},{"name":"407","throwException":false,"foo":"bar"},{"name":"408","throwException":false,"foo":"bar"},{"name":"409","throwException":false,"foo":"bar"},{"name":"410","throwException":false,"foo":"bar"},{"name":"411","throwException":false,"foo":"bar"},{"name":"412","throwException":false,"foo":"bar"},{"name":"413","throwException":false,"foo":"bar"},{"name":"414","throwException":false,"foo":"bar"},{"name":"415","throwException":false,"foo":"bar"},{"name":"416","throwException":false,"foo":"bar"},{"name":"417","throwException":false,"foo":"bar"},{"name":"418","throwException":false,"foo":"bar"},{"name":"419","throwException":false,"foo":"bar"},{"name":"420","throwException":false,"foo":"bar"},{"name":"421","throwException":false,"foo":"bar"},{"name":"422","throwException":false,"foo":"bar"},{"name":"423","throwException":false,"foo":"bar"},{"name":"424","throwException":false,"foo":"bar"},{"name":"425","throwException":false,"foo":"bar"},{"name":"426","throwException":false,"foo":"bar"},{"name":"427","throwException":false,"foo":"bar"},{"name":"428","throwException":false,"foo":"bar"},{"name":"429","throwException":false,"foo":"bar"},{"name":"430","throwException":false,"foo":"bar"},{"name":"431","throwException":false,"foo":"bar"},{"name":"432","throwException":false,"foo":"bar"},{"name":"433","throwException":false,"foo":"bar"},{"name":"434","throwException":false,"foo":"bar"},{"name":"435","throwException":false,"foo":"bar"},{"name":"436","throwException":false,"foo":"bar"},{"name":"437","throwException":false,"foo":"bar"},{"name":"438","throwException":false,"foo":"bar"},{"name":"439","throwException":false,"foo":"bar"},{"name":"440","throwException":false,"foo":"bar"},{"name":"441","throwException":false,"foo":"bar"},{"name":"442","throwException":false,"foo":"bar"},{"name":"443","throwException":false,"foo":"bar"},{"name":"444","throwException":false,"foo":"bar"},{"name":"445","throwException":false,"foo":"bar"},{"name":"446","throwException":false,"foo":"bar"},{"name":"447","throwException":false,"foo":"bar"},{"name":"448","throwException":false,"foo":"bar"},{"name":"449","throwException":false,"foo":"bar"},{"name":"450","throwException":false,"foo":"bar"},{"name":"451","throwException":false,"foo":"bar"},{"name":"452","throwException":false,"foo":"bar"},{"name":"453","throwException":false,"foo":"bar"},{"name":"454","throwException":false,"foo":"bar"},{"name":"455","throwException":false,"foo":"bar"},{"name":"456","throwException":false,"foo":"bar"},{"name":"457","throwException":false,"foo":"bar"},{"name":"458","throwException":false,"foo":"bar"},{"name":"459","throwException":false,"foo":"bar"},{"name":"460","throwException":false,"foo":"bar"},{"name":"461","throwException":false,"foo":"bar"},{"name":"462","throwException":false,"foo":"bar"},{"name":"463","throwException":false,"foo":"bar"},{"name":"464","throwException":false,"foo":"bar"},{"name":"465","throwException":false,"foo":"bar"},{"name":"466","throwException":false,"foo":"bar"},{"name":"467","throwException":false,"foo":"bar"},{"name":"468","throwException":false,"foo":"bar"},{"name":"469","throwException":false,"foo":"bar"},{"name":"470","throwException":false,"foo":"bar"},{"name":"471","throwException":false,"foo":"bar"},{"name":"472","throwException":false,"foo":"bar"},{"name":"473","throwException":false,"foo":"bar"},{"name":"474","throwException":false,"foo":"bar"},{"name":"475","throwException":false,"foo":"bar"},{"name":"476","throwException":false,"foo":"bar"},{"name":"477","throwException":false,"foo":"bar"},{"name":"478","throwException":false,"foo":"bar"},{"name":"479","throwException":false,"foo":"bar"},{"name":"480","throwException":false,"foo":"bar"},{"name":"481","throwException":false,"foo":"bar"},{"name":SERVER ERROR
Look at the SERVER ERROR at the end of the response body.
I don't know if it is bug or intentionally.
Looks like the response was allready written or something like that...
References:
SPRING JIRA ISSUE
GITHUB EXAMPLE
Once the HTTP response has been committed, there's no way to change the HTTP status or headers. At that point, that response might be sitting in network buffers or even been sent already to the client. At that point, there's no way to take it back.
So in this case, Spring cannot change the response.
From the client's point of view, if the response is not complete (its length reflects the Content-Length header, for example), then the RFC says the client should close the connection and consider the response incomplete.

Cannot put parameters in body for OAuth2 POST requests in a REST service

It seems I am missing something very basic here.
I made a REST Api that takes POST requests for generating tokens using the Apache Oltu OAuth2 service, that looks something like this :
#POST
#Consumes("application/x-www-form-urlencoded")
#Produces("application/json")
public Response authorize(#Context HttpServletRequest request) throws OAuthSystemException, IOException {
try {
OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);
OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
When I use HttpRequester or Postman to test the service, it works perfectly fine on condition that I input all authentication and OAuth2 parameters as input parameters, as an example :
https://localhost:8443/rest/OAuthService/token?grant_type=password&username=userfortest&password=Johhny1é&client_id=1234
However I read, that for any POST requests, all parameters should be
in the Body of the HTTP request and never sent through with the url as a simple parameter. When I try to pass it in the body of the HTTP request, so as to make the request secure (so the url is the same without parameters and all params are specified in the body), it seems like it doesn't receive anything from the body as it throws an exception, after
OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);
with the following message :
{"error_description":"Missing grant_type parameter value","error":"invalid_request"}
Is it the intended behaviour of Oltu/OAuth2 for the parameters to be passed through with the url? Or what am I doing wrong?
Thanks in advance.
Your answer is here: Unable to retrive post data using ,#Context HttpServletRequest when passed to OAuthTokenRequest using Oltu
I did exactly what he said and it worked perfectly.
You need modify Response authorize() parameters.

Spring Servlet Filter causing browser connection to be reset

I have a Spring web app that (for the time being) only has a single servlet filter (no controllers/methods, etc.) that is configured to intercept all requests:
// Groovy pseudo-code, but that shouldn't matter, all the logic is there
// and we know it works (no compiler errors) because I can go to localhost:8080
// in a browser and see it.
#Override
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
println 'I am being invoked to filter the response!'
HttpServletResponse httpServletResponse = response as HttpServletResponse
String htmlResponse = """
<html>
<head><title>Hello!</title></head>
<body>
Hello from filter land!
</body>
</html>
"""
httpServletResponse.writer.write(htmlResponse)
httpServletResponse.writer.flush()
}
When I start the app and point my browser to http://localhost:8080, I get the expected HTML (a message that reads "Hello from filter land!"). I also see the println show up in my console output.
I then tune my browser (FireFox) to use this proxy for all HTTP traffic by going to Preferences >> Network and using these configs:
I hit OK and then go to http://example.com and get a "The connection was reset" error. In the Web Developer >> Network tool, there isn't much info, either. Just basically shows a bad connection:
It's also important to note that I'm not seeing the println print to console output when I go to http://example.com (or any other HTTP site) via FireFox. So clearly the browser doesn't seem to be "hitting" the filter correctly.
Any ideas as to why my servlet filter is working against http://localhost:8080, but not when my browser is proxied to use it? Trying to first rule out if its an app-layer issue with how I'm using servlet filters and/or Spring.
You need to run a web proxy, before you can set proxy. One like this http://squidman.net/squidman/

Resources