I have a springboot application that I can run locally with no issues with authentication. However when I run this build in a WAR and on a tomcat server the behavior is different.
It seems that the filterchain is not being triggered, can someone help me?
I have the following HelloController for example:
@RestController
@RequestMapping("/api/hello")
public class HelloController {
@GetMapping("/hi")
public String hello() {
return "hi";
}
@GetMapping("/bye")
@PreAuthorize("hasRole('USER')")
public String bye() {
return "bye";
}
}
And an WebSecurityConfig as follows:
@EnableWebSecurity
@Configuration
public class WebSecurityConfig
{
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
.antMatchers("/api/hello/**").permitAll()
.anyRequest().authenticated();
http.authenticationProvider(authenticationProvider());
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
So when i call the endpoint
localhost:8080/api/hello/hi
it returns with hi as expected (because its public).
When i call this endpoint when deployed on tomcat, it gives me:
Full authentication is required to access this resource
--edit 1--
When i comment the SecurityFilterChain, its making no difference. I guess its not right configured...
-- edit 2 --
I'm using spring-boot 2.7.5 and tomcat 8.5
Do i forget something? Thanks in advance!