I'm trying to create new POST endpoint in Spring secured by a Spring Security. The endpoint is working. If I provide correct data and credentials the server process the request and returns 200. If I provide incorrect credentials I get 405 and I expect 401.
I found something similar to my problem but I think it is not my case. Same question without response.
I also tried to change the controller's request method from POST to GET and changed the request from POST to GET and it worked! For wrong credentials a received 401 and for correct one 200.
Here is my configuration:
Controller
@Controller
@RequestMapping(value = "/api/v1")
@Secured(UserRoles.ROLE_USER)
public class ApiController {
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public ResponseEntity<String> handleRequest(@RequestBody DataBody dataBody, Model model, Locale locale) {
try{
//do something
return new ResponseEntity<>("received", HttpStatus.OK);
} catch (SomeProblemException e) {
return new ResponseEntity<>(e.toString(), HttpStatus.BAD_REQUEST);
}
}
Security Context
<!-- In Memory Authentication Manager -->
<sec:authentication-manager id="inMemoryAuthManager" >
<sec:authentication-provider>
<sec:user-service properties="WEB-INF/classes/usersRoles.properties"/>
<sec:password-encoder hash="bcrypt" />
</sec:authentication-provider>
</sec:authentication-manager>
<!-- Definition of access rules for API -->
<sec:http use-expressions="true" pattern="/api/**" create-session="stateless"
authentication-manager-ref="inMemoryAuthManager">
<sec:intercept-url pattern="/api/**"
access="hasRole('ROLE_USER')"/>
<sec:http-basic/>
</sec:http>
<!-- Definition of access rules for WEB GUI. -->
<sec:http use-expressions="true"
authentication-manager-ref="inMemoryAuthManager">
<!-- Resources and errors do not need authorization. -->
<sec:intercept-url pattern="/favicon.ico" access="permitAll"/>
<sec:intercept-url pattern="/css/**" access="permitAll"/>
<sec:intercept-url pattern="/img/**" access="permitAll"/>
<sec:intercept-url pattern="/js/**" access="permitAll"/>
<sec:intercept-url pattern="/fonts/**" access="permitAll"/>
<sec:intercept-url pattern="/error/**" access="permitAll"/>
<sec:intercept-url pattern="/login" access="permitAll"/>
<sec:intercept-url pattern="/login.do" access="permitAll"/>
<!-- The rest of pages need authorized user with role ROLE_USER. -->
<sec:intercept-url pattern="/**"
access="hasRole('ROLE_USER')"/>
<!-- Configuration of login page. -->
<sec:form-login login-page="/login"
login-processing-url="/login.do"
authentication-failure-url="/login?error=true"
default-target-url="/applications" username-parameter="username"
password-parameter="password"/>
<!-- Redirect from logout page. -->
<sec:logout logout-url="/logout"
logout-success-url="/login?logout=true" invalidate-session="true"
delete-cookies="JSESSIONID"/>
<!-- Redirect for forbidden page. -->
<sec:access-denied-handler error-page="/error?err=403"/>
<!-- Port configuration. -->
<sec:port-mappings>
<sec:port-mapping http="8080" https="8443"/>
</sec:port-mappings>
<sec:headers>
<sec:frame-options policy="DENY"/>
<sec:content-type-options/>
<sec:xss-protection block="true"/>
</sec:headers>
</sec:http>
HTTP Request (Postman)
POST /api/v1/test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Authorization: Basic bWFudGfmZjptYW50YQ==
User-Agent: PostmanRuntime/7.15.2
Accept: */*
Host: localhost:8080
{
"something": "data",
"somethingelse": "data"
}
HTTP Response (Postman)
Status 405
WWW-Authenticate: Basic realm="Spring Security Application"
Allow: GET
I use Spring Security 3.2.10-RELEASE. I tried to enable Spring Security log but I failed.
The problem was here:
@Controller
public class ErrorController {
@RequestMapping("/error")
public String error(@RequestParam(value = "err", required = false) Integer paramErrorCode, Locale locale,
ModelMap model, HttpServletRequest httpRequest) {
// Do something
}
I had a controller, which handles error screens, but it supported only GET method. When I changed it to both GET and POST it started work.
Solution:
@Controller
public class ErrorController {
@RequestMapping(value = "/error" method = {RequestMethod.GET, RequestMethod.POST})
public String error(@RequestParam(value = "err", required = false) Integer paramErrorCode, Locale locale,
ModelMap model, HttpServletRequest httpRequest) {
// Do something
}
Not sure what cause the redirection if web.xml
<error-page>
<location>/error</location>
</error-page>
or securitycontext.xml
<sec:access-denied-handler error-page="/error?err=403"/>