Spring Boot Custom Authorization Header request not going through at all

Viewed 1907

So I have a Spring Boot application and I am sending a request to it using PostMan. It is using Spring Security along with JWT for authentication. I'm trying to get authorization to work but am running into issues. Spring is able to login the user and return a token fine. But when I put the token in the header it's not working at all. I get no response from the server. When the token is removed, it works fine. Right now all requests should be able to go through regardless of being logged in or not.

My Spring Web Configuration:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurity extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .addFilter(new JWTAuthenticationFilter(authenticationManager()))
        .addFilter(new JWTAuthorizationFilter(authenticationManager()));
    }
}

The REST path I'm trying to access:

@RestController("threadService")
@RequestMapping("/api/thread")
public class ThreadService {
    @RequestMapping(value="/list", method=RequestMethod.GET)
    public List<ThreadDetails> getThreadList() {
        logger.info("getThreadList");
        return threadDao.getThreadList();
    }
}

The failed GET request I'm issuing after I have logged in and gotten a token:

GET /api/thread/list HTTP/1.1
Host: localhost:8080
Authorization : Bearer (JWT token here)
Cache-Control: no-cache
Postman-Token: 69565839-4806-b4f6-9a03-11382a80c7da

The above request works fine when there is no Authorization in the header.

1 Answers

Not sure it is exactly the problem I was facing. When I want to communicate with the restservice exposed by spring boot application, the "Authorization" is not set. I followed the steps which are required to communicate but the value wont be passes through header.

The solution I found, the "common-codec" library was missing. Once I add the dependency in my web application, it start sending the "Authorization" in header to my spring boot application. Hope this helps to someone.

Related