使用Spring Boot的简单密码加密


  1. 下面是我的@Bean方法
package com.naveen.entity;
import org.springframework.context.annotation.Bean;
public class PasswordEncoder {

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public String encode(String passWord) {
// TODO Auto-generated method stub
return null;
}
}
  1. 然后,我创建了BCryptPasswordEncoder类,如下所示
package com.naveen.entity;
import org.springframework.stereotype.Component;
@Component
public class BCryptPasswordEncoder extends PasswordEncoder {
}
  1. 然后,我已经自动连接了我的Controller类&我在saveUser()中添加了加密功能,如下所示
public class UserController {
@Autowired 
PasswordEncoder passwordEncoder;
@PostMapping("/saveUser")
public int saveUser(@RequestBody User user) throws Exception {
String encryptedPassword =passwordEncoder.encode(user.getPassWord());
user.setPassWord(encryptedPassword);
userService.saveUser(user);
System.out.println("Inserted data with id: "+ user.getId());
return 1;
}
}
  1. Service类如下
@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}

}

当我运行代码时,我得到了以下错误:

Field passwordEncoder in com.naveen.controller.UserController required a bean of type 'com.naveen.entity.PasswordEncoder' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.naveen.entity.PasswordEncoder' in your configuration.

请帮我解决哪里出了问题。

添加到PumpkinX的答案中,将您的语句按以下顺序排列:

String encryptedPassword =passwordEncoder.encode(user.getPassWord());
user.setPassWord(encryptedPassword);
userService.saveUser(user);
System.out.println("Inserted data with id: "+ user.getId());

注意,userService.saveUser(user);在您读取并编码数据后?由于您有这个top语句,它导致它具有null值。

我知道我迟到了。但是您缺少encode函数的实现。

只需将编码函数替换为以下内容即可。

public String encode(String passWord) {
BCryptPasswordEncoder bCryptPasswordEncoder =
new BCryptPasswordEncoder(10, new SecureRandom());
return bCryptPasswordEncoder.encode(passWord);
}

这将加密密码,而不是返回null值。

如果你想要一个真正的加密算法或加密机制来将密码存储在yml文件中或将敏感数据传递给你的应用程序,你可以尝试使用JASYPT进行Spring Boot

Jasypt是一个在运行时加密和解密spring引导属性的库,而不必担心如何处理加密逻辑和解密逻辑。

我刚刚扭曲了你的代码,能够对密码进行编码

  1. Bean类

    public class PasswordEncoder {
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
    }
    

    }

  2. BCryptPasswordEncoder.java

public class PasswordEncoder {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
  1. 编码密码
@Autowired
BCryptPasswordEncoder passwordEncoder;
@Transactional(rollbackFor = Exception.class)
public UserResource saveDto(UserResource userResource) {
userResource.setPassword(passwordEncoder.bCryptPasswordEncoder().encode(userResource.getPassword()));
return userinfo.save(userResource);
}
  1. 编码密码-示例

您需要将@Component注释添加到BCryptPasswordEncoder

import org.springframework.stereotype.Component;
@Component
public class BCryptPasswordEncoder extends PasswordEncoder{
}

那就行了。

最新更新