簡體   English   中英

如何關閉 Spring 啟動編碼器

[英]How to turn off Spring Boot Encoder

我需要在 Spring 啟動項目中關閉編碼器。 這似乎很容易 - 只需刪除方法和變量,但隨后出現錯誤:

沒有為 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;
    }
}

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

使用以下代碼代替您的 BCryptPassworencoder(盡管絕對不建議將其用於生產)。 這不會 hash 你的密碼輸入,這似乎是你想要的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM