一次又一次地出现同样的错误-“编码的密码看起来不像bcrypt”

ftf50wuq  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(639)

即使输入了正确的电子邮件和密码,同样的错误也会一再出现。
更多参考请看我的报告-https://github.com/ajitlol404/smartcontactmanager/tree/master/smartcontactmanager

中的错误console:-

控制台出错

myconfiguration.java文件

package com.springboot.Configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class MyConfiguration extends WebSecurityConfigurerAdapter{
    @Bean
    public BCryptPasswordEncoder passwordEncoder()
    {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public UserDetailsService getUserDetailsService()
    {
        return new UserDetailsServiceImpl();
    }
    @Bean
    public DaoAuthenticationProvider authenticationProvider()
    {
        DaoAuthenticationProvider daoAuthenticationProvider= new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());

    }

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

        http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN")
        .antMatchers("/user/**").hasRole("USER")
        .antMatchers("/**").permitAll().and().formLogin().and().csrf().disable();

    }
}

家庭控制器.java

package com.springboot.Controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.springboot.Entity.User;
import com.springboot.Helper.Message;
import com.springboot.Repository.UserRepository;

@Controller
public class HomeController 
{
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private UserRepository userrepo;

    @GetMapping("/")
    public String home(Model model)
    {
        model.addAttribute("title", "Home - Smart Contact Manager");
        return "home";
    }

    @GetMapping("/about")
    public String about(Model model)
    {
        model.addAttribute("title", "About - Smart Contact Manager");
        return "about";
    }

    @GetMapping("/signup")
    public String signup(Model model)
    {
        model.addAttribute("title", "Signup - Smart Contact Manager");
        model.addAttribute("user", new User());
        return "signup";
    }

    @PostMapping("/do_register")
    public String doregister(@Valid @ModelAttribute("user") User user,BindingResult bresult,@RequestParam(value = "agreement",defaultValue = "false")boolean agreement,Model model,HttpSession session)
    {
        try {

            if(!agreement)
            {
                System.out.println("You have not agreed the term and condition.");
                throw new Exception("You have not agreed the term and condition.");
            }

            if(bresult.hasErrors())
            {
                System.out.println(bresult.toString());
                model.addAttribute("user", user);
                return "signup";
            }

            user.setRole("ROLE_USER");
            user.setEnabled(true);
            user.setImageUrl("degau.jpg");
            user.setPassword(passwordEncoder.encode(user.getPassword()));

            User result = this.userrepo.save(user);

            System.out.println("User details :- "+user);

            model.addAttribute("user", new User());
            session.setAttribute("message", new Message("Successfully Registered!!", "alert-success"));

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("user", user);
            session.setAttribute("message", new Message("Something Went Wrong !!"+ e.getMessage(),"alert-danger"));
            return "signup";
        }

        return "signup";
    }
}

用户存储库.java

package com.springboot.Repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.springboot.Entity.User;

public interface UserRepository extends JpaRepository<User, Long>{

    @Query("select u from User u where u.email=:email")
    public User getUserByUserName(@Param("email")String email);
}
ibrsph3r

ibrsph3r1#

您没有在问题中共享这个类,但是查看您提供的github存储库,您发现这个类不匹配 CustomUserDetails .

public class CustomUserDetails implements UserDetails
{
    // omitted ...

    @Override
    public String getPassword() {

        return user.getEmail();
    }

    @Override
    public String getUsername() {

        return user.getPassword();
    }

    // omitted ...
}

我想 getPassword() 应该返回密码和 getUsername() 你应该回邮件。

相关问题