Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

java - Getting HTTP 403 error in a POST method with httpBasic auth using Spring Security

I made a simple api call with basic httpAuth:

 @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN")
                .and()
                .withUser("api").password(passwordEncoder().encode("api")).roles("API")
        ;
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/api/**").hasRole("API")
                .antMatchers("/admin/**").hasRole("ADMIN")
                .and().httpBasic()
        ;

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

This seems to works just fine with the admin pages. The browser ask for user/password and grant access.

When I try to use the api, only the GET method are working. This is the implementation:

 @RequestMapping(value = "/api/customerOrderConfirm", method = RequestMethod.POST)
    public ResponseEntity confirm(@RequestBody Activation activation) {
        try {
            ... do stuff
        } catch (Exception e) {
            logger.error("error executing command: " + e.toString(), e);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .contentType(MediaType.TEXT_PLAIN)
                    .body(e.toString());
        }
    }

    @RequestMapping(value = "/api/customerOrderConfirm", method = RequestMethod.GET)
    public ResponseEntity getConfirm() {
        return ResponseEntity.status(HttpStatus.OK)
                .body("OK");
    }

Those are the CURL messages. GET METHOD:

$ curl -i --user api:api http://localhost:8012/api/customerOrderConfirm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     2  100     2    0     0      2      0  0:00:01 --:--:--  0:00:01    14
HTTP/1.1 200 OK
Date: Wed, 13 Feb 2019 18:07:37 GMT
Set-Cookie: JSESSIONID=node0oysb3zf7zfxr14nkdv8ezzhky6.node0;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/plain;charset=utf-8
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Length: 2

OK

POST METHOD:

$ curl -i --user api:api -d '{ }' -H 'Content-Type: application/json' http://localhost:8012/api/customerOrderConfirm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   137    0   134  100     3    134      3  0:00:01 --:--:--  0:00:01  2914
HTTP/1.1 403 Forbidden
Date: Wed, 13 Feb 2019 18:07:25 GMT
Set-Cookie: JSESSIONID=node0169o0o7wk5u6v1ees33334z8uz5.node0;Path=/
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked

{"timestamp":"2019-02-13T18:07:25.830+0000","status":403,"error":"Forbidden","message":"Forbidden","path":"/api/customerOrderConfirm"}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I added in the security config:

.and().csrf().disable()

And now it's working


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...