I'm trying to implement Basic Auth (from Spring Security) to my newbie project. To create an user I send a JSON to `POST` at `/register`, then the user is created and put into H2 in-memory database - this part works fine. Then I have this method in my controller:

```java
@RequestMapping(value = "/username", method = RequestMethod.GET)
public String currentUserName(Principal principal) {
    return principal.getName();
}

```

which returns logged in person's name. I'm using Postman for this - just picking Basic Auth from the droplist, putting username and password of this created user (it is in the database, I checked) and I get Access Denied response - so I guess I'm not logging in correctly or maybe my Spring config isn't right?

I'm using Spring Boot and have 3 Spring Security configs:

**UserAuthService.java** 

```java
@Service
@Transactional
public class UserAuthService implements UserDetailsService {

    private UserRepository userRepository;

    @Autowired
    public UserAuthService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("Could not find the user: " + username); 
        }

        return new org.springframework.security.core.userdetails.User(
            user.getUsername(),
            user.getPassword(),
            true,
            true,
            true,
            true,
            AuthorityUtils.createAuthorityList("USER"));
    }
}

```

**AccountConfiguration.java**

```java
// Spring Security uses accounts from our database
@Configuration
public class AccountConfiguration extends GlobalAuthenticationConfigurerAdapter {

    private UserDetailsService userAuthService;

    @Autowired
    public AccountConfiguration(UserDetailsService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userAuthService);
    }
}

```

And last one to just pick what is authorized and what isn't:

**WebConfiguration.java**

```java
@EnableWebSecurity
@Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // allow everyone to register an account; /console is just for testing
        http
            .authorizeRequests()
                .antMatchers("/register", "/console/**").permitAll();

        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated();

        // making H2 console working
        http
            .headers()
                .frameOptions().disable();

        /*
        https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
        for non-browser APIs there is no need to use csrf protection
        */
        http
            .csrf().disable();
    }
}

```

Here is link to GitHub project if someone wants to check other configs - I'm new to this so maybe I didn't put something relevant in this post:
<https://github.com/doublemc/ToDoWebApp>