0

my code is working fine in spring version 1.5.6.RELEASE. But when i upgrade version to 2.0.0, some of my methods have been deprecated but works fine. when i change my deprecated method by looking from There is no PasswordEncoder mapped for the id "null" with database authentication it starts giving me the error "there is no passwordencoder"

Here is my code.

WebConfig

@Configurable
@EnableWebSecurity
public class WebConfig extends WebSecurityConfigurerAdapter {

@Autowired
AppUserDetailsService appUserDetailsService;

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

@Bean
public static DelegatingPasswordEncoder passwordEncoder() {
 return (DelegatingPasswordEncoder) DelegatingPasswordEncoder.encode("noop");
}

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(appUserDetailsService).passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

@SuppressWarnings("deprecation")
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("http://localhost:4200");

        }
    };
}
@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and()
    .authorizeRequests()
    .antMatchers("/account/register","/account/login","/logout").permitAll()
    .anyRequest().fullyAuthenticated().and()
    .logout()
    .permitAll()
    .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "POST"))
    .and()
    .httpBasic().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and()
    .csrf().disable();
}

}

Account Controller

@RestController
@RequestMapping("account")
public class AccountController {

public static final Logger logger = LoggerFactory.getLogger(AccountController.class);

@Autowired
private UserService userService;

@CrossOrigin
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ResponseEntity<?> createUser(@RequestBody User newUser) {
    if (userService.find(newUser.getUsername()) != null) {
        logger.error("username Already exist " + newUser.getUsername());
        return new ResponseEntity(
                new CustomErrorType("user with username " + newUser.getUsername() + "already exist "),
                HttpStatus.CONFLICT);
    }
    newUser.setRole("USER");

    return new ResponseEntity<User>(userService.save(newUser), HttpStatus.CREATED);
}

@CrossOrigin
@RequestMapping("/login")
public Principal user(Principal principal) {
    logger.info("user logged "+principal);
    return principal;
}
}

User Service

@Service
public class UserService {

@Autowired
UserRepository userRepository;

public User save(User user) {
    return userRepository.saveAndFlush(user);
}

public User update(User user) {
    return userRepository.save(user);
}

public User find(String userName) {
    return userRepository.findOneByUsername(userName);
}
}

I see all the related answers but they are available for inMemory authentication. Spring security : migrating 4.0 to 5.0 - Error -There is no PasswordEncoder mapped for the id “null” Spring Security 5 : There is no PasswordEncoder mapped for the id "null" Spring Boot PasswordEncoder Error

Please help. I'm using mysql database.

Talha Zahid
  • 13
  • 1
  • 2
  • 3

1 Answers1

0

From the documentation :

NoOpPasswordEncoder : A password encoder that does nothing. Useful for testing where working with plain text passwords may be preferred.

They were deprecated because they aren't secured. As i see, if you still want to use "noop" password encoder, just build your own password encoder implementation.

https://github.com/spring-projects/spring-security/blob/master/crypto/src/main/java/org/springframework/security/crypto/password/NoOpPasswordEncoder.java

Hope this helps.