我的用户存储在数据库中,用户类如下所示
@Entity
data class User(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
val id:Long = 0,
val joinDate: LocalDateTime,
val userName:String,
val firstName:String,
val email:String,
val password:String,
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "users_roles", joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "id")], inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")])
val roles:List<Role> = listOf(),
@Column(name = "enabled")
var enabled:Boolean = false
)
@Repository
interface UserRepository:JpaRepository<User, Long> {
fun findByEmail(email:String):User?
fun findByUserName(userName:String):User?
}
我已经完成了注册设置,但是登录是我苦苦挣扎的部分。我在网上找到了几个示例,但是它们都是用于硬编码登录的,而我却找不到数据库的任何示例。我过去(几个月前-不再有项目)已经做到了这一点,并且我记得不仅仅使用PostMapping来做到这一点,而是使用这些方法
@Override
public void configure(AuthenticationManagerBuilder auth)
and
@Override
public void configure(HttpSecurity http)
来自WebSecurityConfigurerAdapter。
如果有人知道我可以从哪里开始,那将不胜感激。谢谢
答案 0 :(得分:0)
您可以通过以下方式配置Web适配器:
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.denyAll();
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(this.userDetailsService);
}
}
在用户详细信息服务中,您可以验证用户详细信息:
public class UserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// this should return spring security user details, and throw an error if not found
}
}