I am developing authorization on the site using JWT tokens: the transfer of the access token and its validation are performed without errors in Postman, enter image description here but when setting HTTP header in browser
function sendHello() {
let result = document.querySelector('.st');
let res_token = result.toString();
fetch('http://localhost:8080/api/hello/user', {
method: 'GET',
headers: new Headers({
'Authorization': 'Bearer, ' + res_token,
'Content-Type': 'application/json'
})
});
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeHttpRequests(
authz -> authz
.antMatchers("/api/auth/login", "/api/auth/token").permitAll()
.anyRequest().authenticated()
.and()
.addFilterAfter(jwtFilter, UsernamePasswordAuthenticationFilter.class)
).build();
@PreAuthorize("hasAuthority('USER')")
@GetMapping("hello/user")
public ResponseEntity<String> helloUser() {
final JwtAuthentication authInfo = authService.getAuthInfo();
return ResponseEntity.ok("Hello user " + authInfo.getPrincipal() + "!");
}
I get an error:
There was an unexpected error (type=Forbidden, status=403).
Access Denied
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:77)
i tried to find error in request params in fetch. But the request in postman is similar:
curl --location 'https://localhost:8081/person/hello/user'\
--header 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbnRvbiIsImV4cCI6MTY5MzIyNzM4MSwicm9sZXMiOlsiVVNFUiJdLCJmaXJzdE5hbWUiOiLQkNC90YLQvtC9In0.s3drUolTZ1655iX7j3U4ecdqAZpID1YIlfwDG5_0xbXE7M7m8HjahSbT3-AszR9vbvrUxhpYIqFQjUQEPgCtyw'