Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Adding Authentication

Marvin Deutz
Marvin Deutz
8,349 Points

401 Unauthorized

Since nobody responded to the person with almost the same topic. Here I am trying my luck.

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    DetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(User.PASSWORD_ENCODER);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated()
                .and().httpBasic()
                .and().csrf().disable();
    }
}

Can't find the mistake. I guess it has to do with the version? Would be nice if someone could confirm that. Craig Dennis?

My guess is the issue lies within the UserService. Would you mind posting that too?

Marvin Deutz
Marvin Deutz
8,349 Points

Somehow I didnt get a notification for your answer. Will post it when back home.

2 Answers

Did you fix the parameter order for the User class constructor, as mentioned in the first ~20 seconds of the video?

When saving data in DataBaseLoader it assumes first parameter is username, but in previous videos the constructor was autogenerated with firstName as first parameter - so you would only have usernames like "Jacob", "Norman" etc in the database.

Marvin Deutz
Marvin Deutz
8,349 Points

Yeah I did :/ Is it working for you? Would it be possible for you to share your code?

Jeremiah Shore
Jeremiah Shore
31,168 Points

thanks for pointing this out!

thanks you very much

I am still getting a 401 no matter what i try, either username or first name

package com.teamtreehouse.core;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.http.HttpMethod.OPTIONS;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration  {

@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
    http
            .csrf().disable()
            .authorizeHttpRequests((requests) -> requests
                    .requestMatchers(OPTIONS).permitAll() // allow CORS option calls for Swagger UI
                    .requestMatchers("/openapi/openapi.yml").permitAll()
                    .anyRequest().authenticated())
            .httpBasic();
    return http.build();
   }
}

Here is the list of students

 List<User_Entity> students = Arrays.asList(
                new User_Entity("jacobproffer", "Jacob",  "Proffer", "password", new String[] {"ROLE_USER"}),
                new User_Entity("mlnorman", "Mike",  "Norman", "password", new String[] {"ROLE_USER"}),
                new User_Entity("k_freemansmith", "Karen",  "Freeman-Smith", "password", new String[] {"ROLE_USER"}),
                new User_Entity("seth_lk", "Seth",  "Kroger", "password", new String[] {"ROLE_USER"}),
                new User_Entity("mrstreetgrid", "Java",  "Vince", "password", new String[] {"ROLE_USER"}),
                new User_Entity("anthonymikhail", "Tony",  "Mikhail", "password", new String[] {"ROLE_USER"}),
                new User_Entity("boog690", "AJ",  "Teacher", "password", new String[] {"ROLE_USER"}),
                new User_Entity("faelor", "Erik",  "Faelor Shafer", "password", new String[] {"ROLE_USER"}),
                new User_Entity("christophernowack", "Christopher",  "Nowack", "password", new String[] {"ROLE_USER"}),
                new User_Entity("calebkleveter", "Caleb",  "Kleveter", "password", new String[] {"ROLE_USER"}),
                new User_Entity("richdonellan", "Rich",  "Donnellan", "password", new String[] {"ROLE_USER"}),
                new User_Entity("albertqerimi", "Albert",  "Qerimi", "password", new String[] {"ROLE_USER"})
        );

With this error

There is no PasswordEncoder mapped for the id "null"