java - Spring CORS error -
i'm building webapi spring , client reactjs. i'm trying post request authenticate oauth2, against webapi, keep getting
response preflight has invalid http status code 401
websecurityconfigureradapter:
@configuration public class websecurityconfig extends websecurityconfigureradapter { @bean public corsfilter corsfilter() { urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource(); corsconfiguration config = new corsconfiguration(); config.setallowcredentials(false); //updated false config.addallowedorigin("*"); config.addallowedheader("*"); config.addallowedmethod("get"); config.addallowedmethod("put"); config.addallowedmethod("post"); source.registercorsconfiguration("/**", config); return new corsfilter(source); } @override protected void configure(httpsecurity http) throws exception { http .csrf().disable() .exceptionhandling() .authenticationentrypoint((request, response, authexception) -> response.senderror(httpservletresponse.sc_unauthorized)) .and() .authorizerequests() .antmatchers(httpmethod.options).permitall() .antmatchers("/**").authenticated() .and() .httpbasic(); } }
my request is:
fetch( this.getauthendpoint('password'), { method:'post', headers: { 'access-control-allow-origin': '*', 'authorization': 'basic dg9ucjpzzwnyzxq=' }, body: json.stringify({ 'password': credentials.password, 'username': credentials.username, 'grant_type': 'password', 'scope': 'read write', 'client_secret': config.clientsecret, 'client_id': config.clientid })}) .then(response => { this.savetokens(response.data); return axios.get(config.apiurl + '/me'); }) .then(response => { this.loginsuccess(response.data.user); }) .catch(response => { this.loginerror(response); });
try doing http.cors()..
@override protected void configure(httpsecurity http) throws exception { http.cors().... }
reference https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
this add cors filter , return instead of passing requests security filters.
Comments
Post a Comment