Generated project

This commit is contained in:
start.vaadin.com
2021-07-25 10:44:30 +00:00
commit 6e3f448aa3
50 changed files with 10663 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package com.cubetiqs.fusion;
import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.server.PWA;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import com.vaadin.flow.theme.Theme;
/**
* The entry point of the Spring Boot application.
*
* Use the @PWA annotation make the application installable on phones, tablets
* and some desktop browsers.
*
*/
@SpringBootApplication
@Theme(value = "fusionmanagement")
@PWA(name = "Fusion Management", shortName = "Fusion Management", offlineResources = {"images/logo.png"})
public class Application extends SpringBootServletInitializer implements AppShellConfigurator {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,45 @@
package com.cubetiqs.fusion.data;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import com.vaadin.fusion.Nonnull;
@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue
@Nonnull
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
if (id != null) {
return id.hashCode();
}
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof AbstractEntity)) {
return false; // null or other class
}
AbstractEntity other = (AbstractEntity) obj;
if (id != null) {
return id.equals(other.id);
}
return super.equals(other);
}
}

View File

@@ -0,0 +1,16 @@
package com.cubetiqs.fusion.data;
public enum Role {
USER("user"), ADMIN("admin");
private String roleName;
private Role(String roleName) {
this.roleName = roleName;
}
public String getRoleName() {
return roleName;
}
}

View File

@@ -0,0 +1,22 @@
package com.cubetiqs.fusion.data.endpoint;
import java.util.Optional;
import com.cubetiqs.fusion.data.entity.User;
import com.cubetiqs.fusion.security.AuthenticatedUser;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import com.vaadin.fusion.Endpoint;
import org.springframework.beans.factory.annotation.Autowired;
@Endpoint
@AnonymousAllowed
public class UserEndpoint {
@Autowired
private AuthenticatedUser authenticatedUser;
public Optional<User> getAuthenticatedUser() {
return authenticatedUser.get();
}
}

View File

@@ -0,0 +1,61 @@
package com.cubetiqs.fusion.data.entity;
import javax.persistence.Entity;
import com.cubetiqs.fusion.data.AbstractEntity;
import com.vaadin.fusion.Nonnull;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.FetchType;
import com.cubetiqs.fusion.data.Role;
import javax.persistence.Lob;
@Entity
public class User extends AbstractEntity {
@Nonnull
private String username;
@Nonnull
private String name;
@JsonIgnore
private String hashedPassword;
@ElementCollection(fetch = FetchType.EAGER)
@Nonnull
private Set<Role> roles;
@Nonnull
@Lob
private String profilePictureUrl;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHashedPassword() {
return hashedPassword;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public String getProfilePictureUrl() {
return profilePictureUrl;
}
public void setProfilePictureUrl(String profilePictureUrl) {
this.profilePictureUrl = profilePictureUrl;
}
}

View File

@@ -0,0 +1,57 @@
package com.cubetiqs.fusion.data.generator;
import com.vaadin.flow.spring.annotation.SpringComponent;
import com.cubetiqs.fusion.data.service.UserRepository;
import com.cubetiqs.fusion.data.entity.User;
import java.util.Collections;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.cubetiqs.fusion.data.Role;
import java.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.vaadin.artur.exampledata.DataType;
import org.vaadin.artur.exampledata.ExampleDataGenerator;
@SpringComponent
public class DataGenerator {
@Bean
public CommandLineRunner loadData(PasswordEncoder passwordEncoder, UserRepository userRepository) {
return args -> {
Logger logger = LoggerFactory.getLogger(getClass());
if (userRepository.count() != 0L) {
logger.info("Using existing database");
return;
}
int seed = 123;
logger.info("Generating demo data");
logger.info("... generating 2 User entities...");
User user = new User();
user.setName("John Normal");
user.setUsername("user");
user.setHashedPassword(passwordEncoder.encode("user"));
user.setProfilePictureUrl(
"https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=128&h=128&q=80");
user.setRoles(Collections.singleton(Role.USER));
userRepository.save(user);
User admin = new User();
admin.setName("John Normal");
admin.setUsername("admin");
admin.setHashedPassword(passwordEncoder.encode("admin"));
admin.setProfilePictureUrl(
"https://images.unsplash.com/photo-1607746882042-944635dfe10e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=128&h=128&q=80");
admin.setRoles(Collections.singleton(Role.ADMIN));
userRepository.save(admin);
logger.info("Generated demo data");
};
}
}

View File

@@ -0,0 +1,17 @@
package com.cubetiqs.fusion.data.service;
import com.cubetiqs.fusion.data.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import com.vaadin.fusion.Nonnull;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.FetchType;
import com.cubetiqs.fusion.data.Role;
import javax.persistence.Lob;
public interface UserRepository extends JpaRepository<User, Integer> {
User findByUsername(String username);
}

View File

@@ -0,0 +1,30 @@
package com.cubetiqs.fusion.data.service;
import com.cubetiqs.fusion.data.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vaadin.artur.helpers.CrudService;
import com.vaadin.fusion.Nonnull;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.FetchType;
import com.cubetiqs.fusion.data.Role;
import javax.persistence.Lob;
@Service
public class UserService extends CrudService<User, Integer> {
private UserRepository repository;
public UserService(@Autowired UserRepository repository) {
this.repository = repository;
}
@Override
protected UserRepository getRepository() {
return repository;
}
}

View File

@@ -0,0 +1,47 @@
package com.cubetiqs.fusion.security;
import java.util.Optional;
import com.cubetiqs.fusion.data.entity.User;
import com.cubetiqs.fusion.data.service.UserRepository;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.server.VaadinServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Component;
@Component
public class AuthenticatedUser {
@Autowired
private UserRepository userRepository;
private UserDetails getAuthenticatedUser() {
SecurityContext context = SecurityContextHolder.getContext();
Object principal = context.getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
UserDetails userDetails = (UserDetails) context.getAuthentication().getPrincipal();
return userDetails;
}
return null;
}
public Optional<User> get() {
UserDetails details = getAuthenticatedUser();
if (details == null) {
return Optional.empty();
}
return Optional.of(userRepository.findByUsername(details.getUsername()));
}
public void logout() {
UI.getCurrent().getPage().setLocation(SecurityConfiguration.LOGOUT_URL);
SecurityContextLogoutHandler logoutHandler = new SecurityContextLogoutHandler();
logoutHandler.logout(VaadinServletRequest.getCurrent().getHttpServletRequest(), null, null);
}
}

View File

@@ -0,0 +1,38 @@
package com.cubetiqs.fusion.security;
import com.vaadin.flow.spring.security.VaadinWebSecurityConfigurerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends VaadinWebSecurityConfigurerAdapter {
public static final String LOGOUT_URL = "/";
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// Public access
http.authorizeRequests().antMatchers("/").permitAll();
super.configure(http);
setLoginView(http, "/login", LOGOUT_URL);
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
web.ignoring().antMatchers("/images/logo.png");
}
}

View File

@@ -0,0 +1,40 @@
package com.cubetiqs.fusion.security;
import java.util.List;
import java.util.stream.Collectors;
import com.cubetiqs.fusion.data.entity.User;
import com.cubetiqs.fusion.data.service.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("No user present with username: " + username);
} else {
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getHashedPassword(),
getAuthorities(user));
}
}
private static List<GrantedAuthority> getAuthorities(User user) {
return user.getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role.getRoleName()))
.collect(Collectors.toList());
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,7 @@
server.port=${PORT:8080}
logging.level.org.atmosphere = warn
spring.mustache.check-template-location = false
# To improve the performance during development.
# For more information https://vaadin.com/docs/flow/spring/tutorial-spring-configuration.html#special-configuration-parameters
# vaadin.whitelisted-packages= org/vaadin/example

View File

@@ -0,0 +1,6 @@
_____ _ __ __ _
| ___| _ _ ___ (_) ___ _ __ | \/ | __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_
| |_ | | | |/ __|| | / _ \ | '_ \ | |\/| | / _` || '_ \ / _` | / _` | / _ \| '_ ` _ \ / _ \| '_ \ | __|
| _| | |_| |\__ \| || (_) || | | | | | | || (_| || | | || (_| || (_| || __/| | | | | || __/| | | || |_
|_| \__,_||___/|_| \___/ |_| |_| |_| |_| \__,_||_| |_| \__,_| \__, | \___||_| |_| |_| \___||_| |_| \__|
|___/