Home » Java » java – Http failure response for http url: 401 Unauthorized-Exceptionshub

java – Http failure response for http url: 401 Unauthorized-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

When ever I use the following method I am able to call my service from angular

   @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable().authorizeRequests().antMatchers("/").permitAll();
    }

but when I change the configure method as below I am getting 401 error.

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable()
                // dont authenticate this particular request
                .authorizeRequests().antMatchers("/authenticate", "/register","/basicauth").permitAll().
                // all other requests need to be authenticated
                        anyRequest().authenticated().and().
                // make sure we use stateless session; session won't be used to
                // store user's state.
                        exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Add a filter to validate the tokens with every request
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

How to overcome this problem.

my error as below

enter image description here

How to&Answers:

You need to specify the entire endpoint in antMatchers(). As far as I can see, the url you’re trying to reach is .../api/v1/basicauth => change the /basicauth string to /api/v1/basicauth.