java - 如何关闭 Spring Boot 编码器
问题描述
我需要在我的 Spring Boot 项目中关闭编码器。这似乎很容易 - 只需删除方法和变量,但随后出现错误:
没有为 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(尽管绝对不建议将其用于生产)。这不会散列您的密码输入,这似乎是您想要的。
@Bean
public PasswordEncoder passwordEncoder() {
return new NoOpPasswordEncoder.getInstance();
}
推荐阅读
- php - 致命错误:在布尔值上调用成员函数 myfunction()
- vue.js - Vue.js - 较少的文件导入引发“模块构建失败”错误,无法解析“../assets/file.less”
- boolean - 如何解决“量词层次结构以通用量词结尾”?
- python - 如何优化这个 numpy 数组操作?
- url - 将 HTTPS 域重定向到新的 HTTPS 域
- xml - 如何在我的代码中使用正确的 Max 函数?
- json - 从表格数据转换为 json 时避免转义字符
- javascript - 跟踪多个收件人的电子邮件
- angular - 使用 RouteReuseStrategy 保持组件状态
- tabulator - 制表符列标题对齐