Ben de kullanıcı asıl nesnede ek veri kurtarmaya çalışıyorum. yaptım neBahar Güvenlik özel UserDetailsService ve özel kullanıcı sınıfı
oldu:
(e-posta adresi gibi vs) benim ek veri kaydedilir Varolan kullanıcı sınıfına "UserDetails" arabirimini uygular.
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
UserDAO userDAO;
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
User user = userDAO.findOneByUsername(username);
if (user == null)
throw new UsernameNotFoundException("username " + username
+ " not found");
System.out.println("---------------------> FOUND ------------->"
+ user.getEmail());
return user;
}
}
Son adım benim Güvenlik yapılandırmasında UserDetailsService eklemek oldu:
@Entity
public class User implements UserDetails {
Sonra ben bir UserDetailsService uygulamasını yarattı.
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService());
// ...
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.userDetailsService(userDetailsService());
// ...
}
@Override
protected UserDetailsService userDetailsService() {
return userDetailsService;
}
ben "loadUserByName" (çünkü "Found" çıktı) iki kez çağrılan benim konsolunda görüyoruz.
benim denetleyicisi başlıca nesneyi erişmeye çalıştığınızda
-> benim ek veri alamadımSystem.out.println(SecurityContextHolder.getContext()
.getAuthentication().getPrincipal());
. Kullanıcı nesnelerine yayınlamaya çalıştığımda, bir istisna getirilemedim.
Eksik olduğum bir şey var mı?
Önceden teşekkür ederiz. UserDetailsService eklemek daha
public class CustomUserDetails extends org.springframework.security.core.userdetails.User{
private User user;
public CustomUserDetails(User user, Collection<? extends GrantedAuthority> authorities) {
super(user.getName(), user.getPassword(), authorities);
this.user = user;
}
public CustomUserDetails(User user, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(user.getName(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.user = user;
}
public User getUser() {
return user;
}
}
: İnfertilite sınıf oluşturun
. Btw. Son kod parçacığım bana verir: "java.lang.ClassCastException: org.springframework.security.core.userdetails.User için gönderilemez .......... CustomUserDetails" – jvecsei
Uygulamanızın hatalarını ayıklayabilir ve Hangi sınıf dönecektir 'SecurityContextHolder.getContext(). getAuthentication(). getPrincipal()'? –
sınıf org.springframework.security.core.userdetails.Kullanıcı – jvecsei