summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2024-07-15 23:40:21 -0400
committerrealtradam <[email protected]>2024-07-15 23:40:21 -0400
commit55a6616445b5697547792176e6a560bf19e69ba8 (patch)
tree31c486f2e927bd7de64b105f24e1bb26bed90771
parentb1581ed1046f4aef077aea4fe6a1b01e599125d2 (diff)
downloadspring-blog-55a6616445b5697547792176e6a560bf19e69ba8.tar.gz
spring-blog-55a6616445b5697547792176e6a560bf19e69ba8.zip
code cleanup
-rw-r--r--pom.xml4
-rw-r--r--src/main/java/com/blog/web/WebApplication.java4
-rw-r--r--src/main/java/com/blog/web/controllers/ArticleController.java46
-rw-r--r--src/main/java/com/blog/web/controllers/AuthController.java16
-rw-r--r--src/main/java/com/blog/web/dto/ArticleDto.java2
-rw-r--r--src/main/java/com/blog/web/dto/RegistrationDto.java3
-rw-r--r--src/main/java/com/blog/web/models/Article.java5
-rw-r--r--src/main/java/com/blog/web/models/Role.java6
-rw-r--r--src/main/java/com/blog/web/models/UserEntity.java9
-rw-r--r--src/main/java/com/blog/web/security/CustomUserDetailsService.java12
-rw-r--r--src/main/java/com/blog/web/security/SecurityConfig.java1
-rw-r--r--src/main/java/com/blog/web/security/SecurityUtil.java6
-rw-r--r--src/main/java/com/blog/web/services/ArticleService.java2
-rw-r--r--src/main/java/com/blog/web/services/UserService.java3
-rw-r--r--src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java7
-rw-r--r--src/main/java/com/blog/web/services/impl/UserServiceImpl.java22
16 files changed, 68 insertions, 80 deletions
diff --git a/pom.xml b/pom.xml
index 8ffe062..63bd4a5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -75,6 +75,10 @@
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/com/blog/web/WebApplication.java b/src/main/java/com/blog/web/WebApplication.java
index ddab585..0368f26 100644
--- a/src/main/java/com/blog/web/WebApplication.java
+++ b/src/main/java/com/blog/web/WebApplication.java
@@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
-
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
-
-}
+} \ No newline at end of file
diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java
index cb00a56..e60be61 100644
--- a/src/main/java/com/blog/web/controllers/ArticleController.java
+++ b/src/main/java/com/blog/web/controllers/ArticleController.java
@@ -27,7 +27,7 @@ public class ArticleController {
@GetMapping("/articles")
public String listArticles(Model model) {
List<ArticleDto> articles = articleService.findAllArticles();
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
model.addAttribute("user", user);
model.addAttribute("articles", articles);
return "index";
@@ -37,16 +37,16 @@ public class ArticleController {
public String showArticle(@PathVariable("articleId") long articleId, Model model) {
ArticleDto articleDto = articleService.findArticleById(articleId);
model.addAttribute("article", articleDto);
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
model.addAttribute("user", user);
return "articles/show";
}
@GetMapping("/articles/new")
public String createArticleForm(Model model) {
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
model.addAttribute("user", user);
- Article article = new Article();
+ final Article article = new Article();
model.addAttribute("article", article);
return "articles/new";
}
@@ -54,33 +54,31 @@ public class ArticleController {
@PostMapping("/articles/new")
public String saveArticle(@Valid @ModelAttribute("article") ArticleDto articleDto,
BindingResult result,
- Model model) {
- if(articleDto.getCreatedBy() == null) {
+ Model model)
+ {
+ // if un-logged in user tries to create an article
+ // redirect them to login page
+ UserEntity user = userService.getLoggedInUser();
+ if(userService.getLoggedInUser().getId() == null) {
return "redirect:/userlogin";
}
- if(result.hasErrors()) {
+ else if(result.hasErrors()) {
model.addAttribute("article", articleDto);
return "articles/new";
}
- articleService.saveArticle(articleDto);
- return "redirect:/articles";
- }
-
- private UserEntity getLoggedInUser() {
- UserEntity user = new UserEntity();
- String username = SecurityUtil.getSessionUser();
- if(username != null) {
- user = userService.findByUsername(username);
+ else {
+ articleService.saveArticle(articleDto);
+ return "redirect:/articles";
}
- return user;
}
+
@GetMapping("/articles/delete/{articleId}")
public String deleteArticle(@PathVariable("articleId") Long articleId) {
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
ArticleDto article = articleService.findArticleById(articleId);
UserEntity owner = article.getCreatedBy();
- if(owner.getId() == user.getId()) {
+ if(owner.equals(user)) {
articleService.delete(articleId);
}
return "redirect:/articles";
@@ -88,7 +86,7 @@ public class ArticleController {
@GetMapping("/articles/edit/{articleId}")
public String editArticleForm(@PathVariable("articleId") long articleId, Model model) {
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
model.addAttribute("user", user);
ArticleDto articleDto = articleService.findArticleById(articleId);
model.addAttribute("article", articleDto);
@@ -109,19 +107,13 @@ public class ArticleController {
@GetMapping("/articles/search")
public String searchArticle(@RequestParam(value = "search") String search, Model model) {
- UserEntity user = getLoggedInUser();
+ UserEntity user = userService.getLoggedInUser();
model.addAttribute("user", user);
List<ArticleDto> articles = articleService.searchArticles(search);
model.addAttribute("articles", articles);
return "index";
}
- @GetMapping("/userlogin")
- public String login(Model model) {
- UserEntity user = getLoggedInUser();
- model.addAttribute("user", user);
- return "auth/login";
- }
@GetMapping("/")
public String getArticles() {
diff --git a/src/main/java/com/blog/web/controllers/AuthController.java b/src/main/java/com/blog/web/controllers/AuthController.java
index bb7cb0d..a654ad1 100644
--- a/src/main/java/com/blog/web/controllers/AuthController.java
+++ b/src/main/java/com/blog/web/controllers/AuthController.java
@@ -2,8 +2,10 @@ package com.blog.web.controllers;
import com.blog.web.dto.RegistrationDto;
import com.blog.web.models.UserEntity;
+import com.blog.web.security.SecurityUtil;
import com.blog.web.services.UserService;
import jakarta.validation.Valid;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@@ -19,14 +21,16 @@ public class AuthController {
this.userService = userService;
}
- @GetMapping("/login")
- public String loginPage() {
+ @GetMapping("/userlogin")
+ public String login(Model model) {
+ UserEntity user = userService.getLoggedInUser();
+ model.addAttribute("user", user);
return "auth/login";
}
@GetMapping("/register")
public String getRegisterForm(Model model) {
- RegistrationDto user = new RegistrationDto();
+ final RegistrationDto user = new RegistrationDto();
model.addAttribute("user", user);
return "auth/register";
}
@@ -38,8 +42,7 @@ public class AuthController {
UserEntity existingUserEmail = userService.findByEmail(user.getEmail());
if(
existingUserEmail != null &&
- existingUserEmail.getEmail() != null &&
- !existingUserEmail.getEmail().isEmpty()
+ StringUtils.isBlank(existingUserEmail.getEmail())
) {
result.rejectValue("email", "There is already a user with this email");
}
@@ -47,8 +50,7 @@ public class AuthController {
UserEntity existingUsername = userService.findByUsername(user.getUsername());
if(
existingUsername != null &&
- existingUsername.getUsername() != null &&
- !existingUsername.getUsername().isEmpty()
+ StringUtils.isBlank(existingUsername.getUsername())
)
{
result.rejectValue("username", "There is already a user with this username");
diff --git a/src/main/java/com/blog/web/dto/ArticleDto.java b/src/main/java/com/blog/web/dto/ArticleDto.java
index 65ec6da..7e8b51a 100644
--- a/src/main/java/com/blog/web/dto/ArticleDto.java
+++ b/src/main/java/com/blog/web/dto/ArticleDto.java
@@ -3,8 +3,6 @@ package com.blog.web.dto;
import com.blog.web.models.UserEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
-import lombok.Builder;
-import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.validator.constraints.URL;
diff --git a/src/main/java/com/blog/web/dto/RegistrationDto.java b/src/main/java/com/blog/web/dto/RegistrationDto.java
index 6d37b57..ca9fed5 100644
--- a/src/main/java/com/blog/web/dto/RegistrationDto.java
+++ b/src/main/java/com/blog/web/dto/RegistrationDto.java
@@ -2,9 +2,6 @@ package com.blog.web.dto;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
-import lombok.Builder;
-import lombok.Data;
-import lombok.Getter;
public class RegistrationDto {
private Long id;
diff --git a/src/main/java/com/blog/web/models/Article.java b/src/main/java/com/blog/web/models/Article.java
index 02c8abf..2682994 100644
--- a/src/main/java/com/blog/web/models/Article.java
+++ b/src/main/java/com/blog/web/models/Article.java
@@ -1,10 +1,5 @@
package com.blog.web.models;
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
import jakarta.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
diff --git a/src/main/java/com/blog/web/models/Role.java b/src/main/java/com/blog/web/models/Role.java
index 7fa47b0..5b84fca 100644
--- a/src/main/java/com/blog/web/models/Role.java
+++ b/src/main/java/com/blog/web/models/Role.java
@@ -1,13 +1,7 @@
package com.blog.web.models;
import jakarta.persistence.*;
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import lombok.Setter;
-import org.apache.catalina.User;
-import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
diff --git a/src/main/java/com/blog/web/models/UserEntity.java b/src/main/java/com/blog/web/models/UserEntity.java
index c416cb6..ac7b288 100644
--- a/src/main/java/com/blog/web/models/UserEntity.java
+++ b/src/main/java/com/blog/web/models/UserEntity.java
@@ -1,10 +1,6 @@
package com.blog.web.models;
import jakarta.persistence.*;
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@@ -26,6 +22,11 @@ public class UserEntity {
)
private List<Role> roles = new ArrayList<>();
+ public boolean equals(UserEntity user)
+ {
+ return this.id == user.getId();
+ }
+
public Long getId() {
return id;
}
diff --git a/src/main/java/com/blog/web/security/CustomUserDetailsService.java b/src/main/java/com/blog/web/security/CustomUserDetailsService.java
index a7516f3..76c0ed8 100644
--- a/src/main/java/com/blog/web/security/CustomUserDetailsService.java
+++ b/src/main/java/com/blog/web/security/CustomUserDetailsService.java
@@ -21,12 +21,12 @@ public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- UserEntity user = userRepository.findFirstByUsername(username);
- if(user != null) {
- User authUser = new User(
- user.getEmail(),
- user.getPassword(),
- user.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName()))
+ UserEntity userEntity = userRepository.findFirstByUsername(username);
+ if(userEntity != null) {
+ final User authUser = new User(
+ userEntity.getEmail(),
+ userEntity.getPassword(),
+ userEntity.getRoles().stream().map((role) -> new SimpleGrantedAuthority(role.getName()))
.collect(Collectors.toList())
);
return authUser;
diff --git a/src/main/java/com/blog/web/security/SecurityConfig.java b/src/main/java/com/blog/web/security/SecurityConfig.java
index 6b85bb3..b459224 100644
--- a/src/main/java/com/blog/web/security/SecurityConfig.java
+++ b/src/main/java/com/blog/web/security/SecurityConfig.java
@@ -9,7 +9,6 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
-import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
diff --git a/src/main/java/com/blog/web/security/SecurityUtil.java b/src/main/java/com/blog/web/security/SecurityUtil.java
index 6e8a5b0..ef0b3d9 100644
--- a/src/main/java/com/blog/web/security/SecurityUtil.java
+++ b/src/main/java/com/blog/web/security/SecurityUtil.java
@@ -7,10 +7,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
public class SecurityUtil {
public static String getSessionUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- if(!(authentication instanceof AnonymousAuthenticationToken)) {
- String currentUsername = authentication.getName();
- return currentUsername;
- }
- return null;
+ return !(authentication instanceof AnonymousAuthenticationToken) ? authentication.getName() : null;
}
}
diff --git a/src/main/java/com/blog/web/services/ArticleService.java b/src/main/java/com/blog/web/services/ArticleService.java
index 2c1e9f4..aeedcb4 100644
--- a/src/main/java/com/blog/web/services/ArticleService.java
+++ b/src/main/java/com/blog/web/services/ArticleService.java
@@ -2,10 +2,8 @@ package com.blog.web.services;
import com.blog.web.dto.ArticleDto;
import com.blog.web.models.Article;
-
import java.util.List;
-
public interface ArticleService {
List<ArticleDto> findAllArticles();
diff --git a/src/main/java/com/blog/web/services/UserService.java b/src/main/java/com/blog/web/services/UserService.java
index 8515cb1..270c2ef 100644
--- a/src/main/java/com/blog/web/services/UserService.java
+++ b/src/main/java/com/blog/web/services/UserService.java
@@ -3,11 +3,12 @@ package com.blog.web.services;
import com.blog.web.dto.RegistrationDto;
import com.blog.web.models.UserEntity;
-
public interface UserService {
void saveUser(RegistrationDto registrationDto);
UserEntity findByEmail(String email);
UserEntity findByUsername(String username);
+
+ public UserEntity getLoggedInUser();
}
diff --git a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
index 1ddd6f0..5c45485 100644
--- a/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
+++ b/src/main/java/com/blog/web/services/impl/ArticleServiceImpl.java
@@ -17,13 +17,14 @@ import static com.blog.web.mappers.ArticleMapper.mapToArticleDto;
@Service
public class ArticleServiceImpl implements ArticleService {
- public ArticleServiceImpl(com.blog.web.repository.ArticleRepository articleRepository, com.blog.web.repository.UserRepository userRepository) {
+ final private ArticleRepository articleRepository;
+ final private UserRepository userRepository;
+
+ public ArticleServiceImpl(ArticleRepository articleRepository, UserRepository userRepository) {
this.userRepository = userRepository;
this.articleRepository = articleRepository;
}
- private ArticleRepository articleRepository;
- private UserRepository userRepository;
@Override
public List<ArticleDto> findAllArticles() {
diff --git a/src/main/java/com/blog/web/services/impl/UserServiceImpl.java b/src/main/java/com/blog/web/services/impl/UserServiceImpl.java
index b197af6..99b3bc8 100644
--- a/src/main/java/com/blog/web/services/impl/UserServiceImpl.java
+++ b/src/main/java/com/blog/web/services/impl/UserServiceImpl.java
@@ -5,17 +5,17 @@ import com.blog.web.models.Role;
import com.blog.web.models.UserEntity;
import com.blog.web.repository.RoleRepository;
import com.blog.web.repository.UserRepository;
+import com.blog.web.security.SecurityUtil;
import com.blog.web.services.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
-
import java.util.Arrays;
@Service
public class UserServiceImpl implements UserService {
- private UserRepository userRepository;
- private RoleRepository roleRepository;
- private PasswordEncoder passwordEncoder;
+ final private UserRepository userRepository;
+ final private RoleRepository roleRepository;
+ final private PasswordEncoder passwordEncoder;
public UserServiceImpl(
UserRepository userRepository,
@@ -36,7 +36,7 @@ public class UserServiceImpl implements UserService {
//user.setPassword(registrationDto.getPassword());
user.setPassword(passwordEncoder.encode(registrationDto.getPassword()));
- Role role = roleRepository.findByName("User");
+ final Role role = roleRepository.findByName("User");
user.setRoles(Arrays.asList(role));
userRepository.save(user);
}
@@ -50,4 +50,16 @@ public class UserServiceImpl implements UserService {
public UserEntity findByUsername(String username) {
return userRepository.findByUsername(username);
}
+
+ public UserEntity getLoggedInUser() {
+ final UserEntity user;
+ String username = SecurityUtil.getSessionUser();
+ if(username != null) {
+ user = this.findByUsername(username);
+ }
+ else {
+ user = new UserEntity();
+ }
+ return user;
+ }
}