#Bean Definition

1 messages ยท Page 1 of 1 (latest)

mossy sonnet
#

Hi, I have a bean called PasswordEncoder but now its throwing up the error 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

restive brambleBOT
#

<@&1004656351647117403> please have a look, thanks.

restive brambleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

mossy sonnet
#

The action it requires is

#

Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.

#

which I have

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

And I auto wire it into my class

    @Autowired
    private PasswordEncoder passwordEncoder;
lucid tree
#

Hi, have you checked if your imports are both the same?

#

and that the class that exposes ur bean is annotated as @Configuration?

mossy sonnet
#

Yup both the same also I used it before and it worked fine just haevn't been on the projecty for like 3 4 weeks

#

Its my secuirty config

#

@EnableWebSecurity
public class SecurityConfiguration {

#

EnableWebSecuirty auto adds Confuration

#

The full error is this

Description:

Field passwordEncoder in com.techtanium.backend.auth.AuthService required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.
lucid tree
#

Try annotating your SecurityConfiguration with @Configuration

mossy sonnet
#

That seems to have fixed it but broken every single one of my unit tests

lucid tree
#

interesting

mossy sonnet
#

Everything returning 404 NOT FOUND

lucid tree
#

not 401?

mossy sonnet
#

Nope

#

expected: 200 OK
but was: 404 NOT_FOUND

lucid tree
#

are your tests annotated as springboottest?

mossy sonnet
#

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class UserControllerTest {

#

Correct

lucid tree
#

and if you do a manual test? does it work as expected?

mossy sonnet
#

Ignore that sorry

lucid tree
#

np ๐Ÿ˜„

mossy sonnet
#

It does work ๐Ÿ™‚

lucid tree
#

can you post the controller and test method?

mossy sonnet
#

Just one of them?

lucid tree
#

one of the ones that are not working

mossy sonnet
#

Yeah okay one sec

#

Let me get back to you actually as I am facing so many other issues now ๐Ÿ˜„

lucid tree
#

okay, good luck on that ๐Ÿ™‚ make sure to ping me @lucid tree like this. otherwise i might not notice

mossy sonnet
#

Yes will do thank you for the help so far!

mossy sonnet
#
    @Test
    public void whenPostRegister_emailContainsCapitals_returnUserWithoutCapitals() {
        User user = createValidUser();
        user.setEmail("[email protected]");
        User inDB = userService.save(user);
        authenticate("test-username");
        ResponseEntity<Map<String, Object>> response = login(new ParameterizedTypeReference<>() {});

        Map<String, Object> body = response.getBody();
        String email = (String) body.get("email");
        assertThat(email).isEqualTo("[email protected]");
    }
#
    private void authenticate(String username) {
        testRestTemplate.getRestTemplate()
                .getInterceptors().add(new BasicAuthenticationInterceptor(username, "P4ssword"));
    }

    public <T> ResponseEntity<T> login(ParameterizedTypeReference<T> responseType) {
        return testRestTemplate.exchange(API_1_LOGIN, HttpMethod.POST,null, responseType);
    }
#
@RestController
public class LoginController {

    @Autowired
    AuthService authService;

    @PostMapping("/api/1.0/login")
    AuthResponse handleAuthentication(@RequestBody Credentials credentials) throws AuthException {
        return authService.authenticate(credentials);
    }

}

#

@lucid tree So I managed to figure out the other issues and solved most of it now just this problem left I am getting a 415 but confused as the media type is text and its the same issue for all the other tests in this class had a look at it for awhile now and even debugging it cant see the problem

mossy sonnet
lucid tree
#

i mean spring boot comes with most of the stuff autoconfigured so maybe it worked, just not with your implementation ๐Ÿ˜„ i think i cant help you with the 415 atm since im only on phone now.

mossy sonnet
#

Humm maybe but I would assume I would of had to change something but the only thing I changed was from Dev Springboot 3.0.0-M1 to 3.0.2 so maybe that was it and all good will see if anyone else can but thank you for all ur help!

#

I will be figuring it out to so might get it ;D