简体   繁体   English

如何关闭 Spring 启动编码器

[英]How to turn off Spring Boot Encoder

I need to turn off encoder in my Spring boot project.我需要在 Spring 启动项目中关闭编码器。 It seems easy - just delete methods and variables, but then I get the error:这似乎很容易 - 只需删除方法和变量,但随后出现错误:

There is no PasswordEncoder mapped for the id "null"没有为 id "null" 映射 PasswordEncoder

Now, i have hardcoded user login and (encoded) password in my database.现在,我的数据库中有硬编码的用户登录名和(编码的)密码。 Password is stored as many literals and numbers, and in password word it means "123", and i need password to be "123" in database.密码存储了许多文字和数字,在密码词中它的意思是“123”,我需要密码在数据库中是“123”。 Here is my code:这是我的代码:

@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;
    }
}

I can give any other classes if needed如果需要,我可以提供任何其他课程

Instead of your BCryptPassworencoder, use the following (though that's absolutely not recommended for production).使用以下代码代替您的 BCryptPassworencoder(尽管绝对不建议将其用于生产)。 This will not hash your password inputs, which is what you seem to want.这不会 hash 你的密码输入,这似乎是你想要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM