BAEL-3283 - Refactored the code as per review comments.
This commit is contained in:
parent
f6c3f9ebcb
commit
9b44df148d
|
@ -6,7 +6,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
@ -29,16 +28,13 @@ public class DataSourceConfig {
|
||||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(dataSource);
|
em.setDataSource(dataSource);
|
||||||
em.setPackagesToScan("com.baeldung.domain");
|
em.setPackagesToScan("com.baeldung.domain");
|
||||||
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
|
||||||
return em;
|
return em;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
|
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
|
||||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
return new JpaTransactionManager(emf);
|
||||||
transactionManager.setEntityManagerFactory(emf);
|
|
||||||
return transactionManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -39,20 +39,12 @@ public class ThemeMVCConfig implements WebMvcConfigurer {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
// @Profile("cookie")
|
|
||||||
public ThemeResolver themeResolver() {
|
public ThemeResolver themeResolver() {
|
||||||
UserPreferenceThemeResolver themeResolver = new UserPreferenceThemeResolver();
|
UserPreferenceThemeResolver themeResolver = new UserPreferenceThemeResolver();
|
||||||
themeResolver.setDefaultThemeName("light");
|
themeResolver.setDefaultThemeName("light");
|
||||||
return themeResolver;
|
return themeResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Bean
|
|
||||||
// @Profile("database")
|
|
||||||
// public ThemeResolver databaseThemeResolver() {
|
|
||||||
// UserPreferenceThemeResolver themeResolver = new UserPreferenceThemeResolver();
|
|
||||||
// themeResolver.setDefaultThemeName("light");
|
|
||||||
// return themeResolver;
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configureViewResolvers(ViewResolverRegistry resolverRegistry) {
|
public void configureViewResolvers(ViewResolverRegistry resolverRegistry) {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.springframework.web.servlet.ThemeResolver;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class UserPreferenceThemeResolver implements ThemeResolver {
|
public class UserPreferenceThemeResolver implements ThemeResolver {
|
||||||
|
|
||||||
|
@ -34,55 +35,40 @@ public class UserPreferenceThemeResolver implements ThemeResolver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String resolveThemeName(HttpServletRequest request) {
|
public String resolveThemeName(HttpServletRequest request) {
|
||||||
String themeName = (String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME);
|
String themeName = findThemeFromRequest(request).orElse(findUserPreferredTheme().orElse(getDefaultThemeName()));
|
||||||
if (themeName != null) {
|
|
||||||
return themeName;
|
|
||||||
}
|
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
||||||
|
|
||||||
if (authentication != null && authentication.isAuthenticated() && isNotAnonymous(authentication)) {
|
|
||||||
User user = (User) authentication.getPrincipal();
|
|
||||||
UserPreference userPreference = userPreferenceRepository.findById(user.getUsername()).orElse(null);
|
|
||||||
|
|
||||||
if (userPreference != null) {
|
|
||||||
themeName = userPreference.getTheme();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall back to default theme.
|
|
||||||
if (themeName == null) {
|
|
||||||
themeName = getDefaultThemeName();
|
|
||||||
}
|
|
||||||
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
|
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, themeName);
|
||||||
return themeName;
|
return themeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNotAnonymous(Authentication authentication) {
|
private Optional<String> findUserPreferredTheme() {
|
||||||
return !isAnonymous(authentication);
|
Authentication authentication = SecurityContextHolder.getContext()
|
||||||
|
.getAuthentication();
|
||||||
|
UserPreference userPreference = getUserPreference(authentication).orElse(new UserPreference());
|
||||||
|
return Optional.ofNullable(userPreference.getTheme());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAnonymous(Authentication authentication) {
|
private Optional<String> findThemeFromRequest(HttpServletRequest request) {
|
||||||
return authentication != null && authentication.getPrincipal() instanceof String && "anonymousUser".equals(authentication.getPrincipal());
|
return Optional.ofNullable((String) request.getAttribute(THEME_REQUEST_ATTRIBUTE_NAME));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<UserPreference> getUserPreference(Authentication authentication) {
|
||||||
|
return isAuthenticated(authentication) ? userPreferenceRepository.findById(((User) authentication.getPrincipal()).getUsername()) : Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAuthenticated(Authentication authentication) {
|
||||||
|
return authentication != null && authentication.isAuthenticated();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String theme) {
|
public void setThemeName(HttpServletRequest request, HttpServletResponse response, String theme) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext()
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
.getAuthentication();
|
||||||
if (authentication != null && authentication.isAuthenticated() && isNotAnonymous(authentication)) {
|
if (isAuthenticated(authentication)) {
|
||||||
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, theme);
|
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, theme);
|
||||||
User user = (User) authentication.getPrincipal();
|
UserPreference userPreference = getUserPreference(authentication).orElse(new UserPreference());
|
||||||
|
userPreference.setUsername(((User) authentication.getPrincipal()).getUsername());
|
||||||
UserPreference userPreference = userPreferenceRepository.findById(user.getUsername()).orElse(new UserPreference());
|
|
||||||
|
|
||||||
userPreference.setUsername(user.getUsername());
|
|
||||||
userPreference.setTheme(StringUtils.hasText(theme) ? theme : null);
|
userPreference.setTheme(StringUtils.hasText(theme) ? theme : null);
|
||||||
userPreferenceRepository.save(userPreference);
|
userPreferenceRepository.save(userPreference);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!StringUtils.hasText(theme)) {
|
|
||||||
request.setAttribute(THEME_REQUEST_ATTRIBUTE_NAME, getDefaultThemeName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
insert into users values('john', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1);
|
insert into users values('john', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1);
|
||||||
insert into users values('admin', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1);
|
insert into users values('admin', '$2a$10$cjcbIX/aLe12PpGZ.vQfweLiB7K1QTC5enTk3oD0deCMdtj2Sx.im', 1);
|
||||||
|
|
||||||
insert into authorities values('sandip', 'USER');
|
insert into authorities values('john', 'USER');
|
||||||
insert into authorities values('admin', 'USER');
|
insert into authorities values('admin', 'USER');
|
||||||
insert into authorities values('admin', 'ADMIN');
|
insert into authorities values('admin', 'ADMIN');
|
Loading…
Reference in New Issue