当前位置: 首页 > 知识库问答 >
问题:

一次又一次的错误-“编码的密码看起来不像BCrypt”

沃博裕
2023-03-14

即使输入了正确的电子邮件和密码,同样的错误也会一次又一次地出现。

更多参考请参见我的回复-https://github.com/ajitlol404/smartcontactmanager/tree/master/smartcontactmanager

控制台错误

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();
        
    }
}
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";
    }
}
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);
}

共有1个答案

农诚
2023-03-14

检查密码字段的宽度。带有{bcrypt}指示符的bcrypt加密哈希为68个字符。

 类似资料:
  • 我正在使用OAuth2和JPA编写spring boot REST安全API。当访问访问令牌时,我会收到警告,因为编码密码看起来不像BCrypt。当我点击邮递员http://localhost:8080/oauth/token的URL时?grant_type=password&username=user&password=user i get 我已经定义了Bean和存储库。我已经使用了secret

  • 我无法使用正确的详细信息登录,因为程序不断声明编码的密码看起来不像bcrypt。有人知道怎么解决这个吗?我正在使用JDBC身份验证。 我也有正确的数据库表,有足够的空间用于编码密码。我不确定哪里出了问题。 JSP表单: 安全配置: 登录控制器 我的数据库:这里

  • 我最近遵循了Spring boot安全中的身份验证和授权教程,我想我在谈到sql时迷路了。尽管它没有显示任何错误,即使我输入了正确的用户名和密码,它仍然显示错误的凭据。这是我的代码: UserDetailsServiceImpl.java WebUserMapper.java WebSecurityConfig.java 这是我的数据库: 它返回以下错误: 密码是123。我不知道为什么它不工作,即

  • null 整个代码块: 代码主要摘自:http://www.bjw.co.nz/developer/misc/82-general-dev/1203-decrypting-a-saml-encrypted-assertion

  • 问题内容: 我正在尝试使用JWT实施spring AuthorizationServer。我能够生成JWT令牌并登录,直到将BCrypt添加到混合中为止。现在,当我尝试登录时,我从API获得“错误的凭据”。 OAuth2Configuration.java WebSecurityConfig.java SeedData.java 谢谢你的帮助。 问题答案: 我需要进行以下更改才能使其正常工作。如果

  • 我有一个spring boot 1.5.9授权服务器,它使用BCrypt存储密码。我正在尝试迁移到2.0,但是,我不再能够检索授权令牌。 服务器的响应是: 控制台输出以下内容:。 这部分应用程序以前工作得很好。我所做的唯一更改是build.gradle文件(更改、添加插件和添加。 中的密码哈希逻辑可在两个单独的配置文件中找到: 而且 OAuthUser类: 我知道spring的安全有很大的变化,但