gpt4 book ai didi

java - Spring Security - 编码密码看起来不像 BCrypt

转载 作者:行者123 更新时间:2023-12-02 01:05:01 25 4
gpt4 key购买 nike

我无法使用正确的详细信息登录,因为程序不断指出编码的密码看起来不像 bcrypt。有谁知道如何解决这个问题?我正在使用 JDBC 身份验证。

我也有正确的数据库表,有足够的空间用于编码密码。我不确定哪里出了问题。

JSP 格式:

                <div class="form-group">
<div class="form-label-group">
<label for="inputUser">Username: </label> <input name="username"
type="text" path="username" id="inputUser" class="form-control"
placeholder="Username" required="required" autofocus="autofocus">
</div>
</div>

<div class="form-group">
<div class="form-label-group">
<label for="inputPassword">Password: </label>
<input name="username" type="password" path="password"
id="inputPassword" class="form-control" placeholder="Password"
required="required">
</div>
</div>

<div class="form-group">
<div class="checkbox">
<label> <input type="checkbox" value="remember-me">
Remember Password
</label>
</div>
</div>

<input type="submit" value="Login"/>

</form:form>

安全配置:

@Autowired
private DataSource securityDataSource;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(securityDataSource).passwordEncoder(passwordEncoder());
}

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

http.authorizeRequests().antMatchers("/management/**").authenticated().and().formLogin().loginPage("/login")
.loginProcessingUrl("/processLogin").defaultSuccessUrl("/management/dashboard").permitAll();

http.exceptionHandling().accessDeniedPage("/access-denied");

}

@Bean
@Autowired
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

登录 Controller

@Controller
public class LoginController {

@Autowired
private UserServiceImpl userService;


@GetMapping("/login")
public String showLoginForm(Model model) {

User user = new User();
model.addAttribute("user", user);

return "login";
}

@PostMapping("/processLogin")
public String processLogin(@ModelAttribute("user") User user, Model model) {

if (userService.findUser(user.getUsername(), user.getPassword()) != null) {
return "/management/dashboard";
} else {
return "/access-denied";
}
}
}

我的数据库here

最佳答案

我通过使用 BcryptPasswordEncoder 解决了我的问题,如下

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
User user = userRepository.findByuserName(userName);
if (user == null) {
throw new UsernameNotFoundException("userName" + userName + "Not found in the database");
}

return new org.springframework.security.core.userdetails.User(user.getName(), new BCryptPasswordEncoder().encode(user.getPassword()), getGrantedAuth(user));
}

关于java - Spring Security - 编码密码看起来不像 BCrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60166583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com