BAEL-8507 Merge spring-data-spring-security into another appropriate module
- Removed project spring-data-spring-security and moved its content in spring-security-mvc-boot
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.security;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import com.baeldung.models.AppUser;
|
||||
|
||||
public class AppUserPrincipal implements UserDetails {
|
||||
|
||||
private final AppUser user;
|
||||
|
||||
//
|
||||
|
||||
public AppUserPrincipal(AppUser user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return user.getUsername();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return user.getPassword();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
final List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("User"));
|
||||
return authorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public AppUser getAppUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.security;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.data.repositories.UserRepository;
|
||||
|
||||
@Component
|
||||
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication arg2) throws IOException, ServletException {
|
||||
userRepository.updateLastLogin(new Date());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.security;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.data.repositories.UserRepository;
|
||||
import com.baeldung.models.AppUser;
|
||||
|
||||
@Service
|
||||
public class CustomUserDetailsService implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext applicationContext;
|
||||
private UserRepository userRepository;
|
||||
|
||||
public CustomUserDetailsService() {
|
||||
super();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void completeSetup() {
|
||||
userRepository = applicationContext.getBean(UserRepository.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(final String username) {
|
||||
final AppUser appUser = userRepository.findByUsername(username);
|
||||
if (appUser == null) {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
return new AppUserPrincipal(appUser);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user