Pica Smart
Pica Smart

Reputation: 53

Getting Unauthorized error: Full authentication is required to access this resource

I'm implementing JWT and Spring Security for authentication in my application.

I have 3 roles: Admin, Moderator and User.

For example, after logging with user role, I got the home page, but once I go to hit the user space by clicking on a button, I got:

2020-09-04 09:01:22.819 ERROR 10148 --- [nio-8080-exec-5] c.b.s.security.jwt.AuthEntryPointJwt : Unauthorized error: Full authentication is required to access this resource

the file webSecurityConfig.java is:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        // securedEnabled = true,
        // jsr250Enabled = true,
        prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Autowired
    private AuthEntryPointJwt unauthorizedHandler;

    @Bean
    public AuthTokenFilter authenticationJwtTokenFilter() {
        return new AuthTokenFilter();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(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/test/**").permitAll()
            .anyRequest().authenticated();

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}

The file application.properties, I put:

spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect=...
spring.jpa.hibernate.ddl-auto=update
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 86400000

In Browser console, I got that exception.

Could you please help me solving that issue ?. Big thanks.

Upvotes: 5

Views: 29324

Answers (3)

Luigi Woodhouse
Luigi Woodhouse

Reputation: 391

You probably are testing in wrong in postman or amneasia or whatever API testing tool you are using. If you are using postman :

1.Select the Header tab

2.Click the button that says "hide auto-generated headers"

3.Type "Authorization" (without quotes) under the "Key" column.

4.Type "Bearer" under the "Value" column then paste the token. Ensure there is a space between "Bearer" and the "Token".

5.Send the request

Upvotes: 1

Zoubeir
Zoubeir

Reputation: 21

You have to update WebSecurityConfig according to your Controller mapping.

      .antMatchers("/**").permitAll()
      .anyRequest().authenticated();

Upvotes: 2

Saria Essid
Saria Essid

Reputation: 1248

You should comment @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')").

Tell about the result ?.

Upvotes: -2

Related Questions