首页 > 解决方案 > 如何关闭 Spring Boot 编码器

问题描述

我需要在我的 Spring Boot 项目中关闭编码器。这似乎很容易 - 只需删除方法和变量,但随后出现错误:

没有为 id "null" 映射 PasswordEncoder

现在,我的数据库中有硬编码的用户登录名和(编码的)密码。密码存储了许多文字和数字,在密码词中它的意思是“123”,我需要密码在数据库中是“123”。这是我的代码:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsServiceImpl userDetailsService;
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.csrf().disable();
        http.authorizeRequests().
                antMatchers("/", "/login", "/logout", "productPage", "contactsPage", "/register", "/add_person").permitAll();
        http.authorizeRequests().
                antMatchers("/admin","/view_person")
                .access("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER_ADMIN')");
        http.authorizeRequests().
                and().exceptionHandling().accessDeniedPage("/403");
        http.authorizeRequests().and().formLogin()//
                .loginProcessingUrl("/j_spring_security_check")
                .loginPage("/login")//
                .defaultSuccessUrl("/productPage")//
                .failureUrl("/login?error=true")//
                .usernameParameter("username")//
                .passwordParameter("password")
                .and().logout().logoutUrl("/logout").logoutSuccessUrl("/logoutSuccessful")
                 .and().rememberMe().key("secret").tokenValiditySeconds(500000);

    }
}


@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AppUserDao appUserDAO;

    @Autowired
    private AppRoleDao appRoleDAO;


    @Override
    public UserDetails loadUserByUsername(String name)  {
        AppUser appUser = this.appUserDAO.findUserAccount(name);

        if (appUser == null) {
            System.out.println("User not found! " + name);
            throw new UsernameNotFoundException("User " + name + " was not found in the database");
        }

        System.out.println("Found User: " + appUser);

        List<String> roleNames = this.appRoleDAO.getRoleNames(appUser.getId());

        List<GrantedAuthority> grantList = new ArrayList<>();
        if (roleNames != null) {
            for (String role : roleNames) {
                GrantedAuthority authority = new SimpleGrantedAuthority(role);
                grantList.add(authority);
            }
        }

        UserDetails userDetails = new User(appUser.getName(),
                appUser.getPassword(), grantList);

        return userDetails;
    }
}

如果需要,我可以提供任何其他课程

标签: javaspring-bootencoding

解决方案


使用以下代码代替您的 BCryptPassworencoder(尽管绝对不建议将其用于生产)。这不会散列您的密码输入,这似乎是您想要的。

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new NoOpPasswordEncoder.getInstance();
    }

推荐阅读