I am getting 403 forbidden using spring boot 3.0M4 and java 17

Viewed 19
  package org.tnc.application.security.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import org.tnc.application.security.serviceimpl.UserDetailsServiceImpl;
    
    @SuppressWarnings("deprecation")
  //@Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {   
        @Autowired
        UserDetailsServiceImpl userDetailsServiceImpl;  
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsServiceImpl);
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.httpBasic();
            http.authorizeRequests().mvcMatchers(HttpMethod.GET,"/hello").hasAnyRole("USER","ADMIN");
            http.csrf().disable();
        }
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
    }

after providing the authentication details in basic auth section I am getting the error. The user details are fetched form database and pass through UserDetailsServiceImpl. I am attaching both the classes.

package org.tnc.application.security.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.tnc.application.security.model.User;
import org.tnc.application.security.service.UserService;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByEmail(username);      
        if(user == null)
            throw new UsernameNotFoundException("User not present");
        System.out.println("name of the user "+user.getPassword());
        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), user.getRoles());
    }

}

UserDetailsServiceImpl is also attached which will validate and parse the user details.

0 Answers
Related