Java 无法从数据库获取数据
当我尝试登录时,会收到hibernate日志消息 User.javaJava 无法从数据库获取数据,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,当我尝试登录时,会收到hibernate日志消息 User.java @Entity @Table(name = "users") public class User { @Id @Column(name = "username", unique = true, nullable = false, length = 45) private String username; @Column(name = "password", nullable = false, len
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "username", unique = true,
nullable = false, length = 45)
private String username;
@Column(name = "password",
nullable = false, length = 100)
private String password;
@Column(name = "enabled", nullable = false)
private boolean enabled;
/*@OneToOne(cascade=CascadeType.ALL)*/
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_roles",
joinColumns = {@JoinColumn(name="user_name", referencedColumnName="username")},
inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")})
private Set<UserRole> userRole = new HashSet<UserRole>(0);
public Set<UserRole> getUserRole() {
return this.userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
getter and setter
我正在添加SpringSecurityConfig.java,并且使用了哈希密码。这是问题的根源吗
Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
@Qualifier("userDetailsService")
UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**")
.access("hasRole('ROLE_ADMIN')").and().formLogin()
.loginPage("/login").failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403");
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
}
你有堆栈跟踪吗 数据库连接正常吗
数据库中的密码是散列的 对不起,我没有堆栈跟踪。是的,数据库连接工作正常,密码被哈希,我使用了PasswordEncoder
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
select
user0_.username as username1_2_,
user0_.enabled as enabled2_2_,
user0_.password as password3_2_
from
users user0_
where
user0_.username=?
Hibernate:
select
userrole0_.user_name as user_nam2_1_0_,
userrole0_.role_id as role_id1_1_0_,
userrole1_.id as id1_0_1_,
userrole1_.role as role2_0_1_
from
user_roles userrole0_
inner join
roles userrole1_
on userrole0_.role_id=userrole1_.id
where
userrole0_.user_name=?
Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
@Qualifier("userDetailsService")
UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**")
.access("hasRole('ROLE_ADMIN')").and().formLogin()
.loginPage("/login").failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403");
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
}