Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/springproject/project/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WebMvcConfig implements WebMvcConfigurer {

@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
return new BCryptPasswordEncoder();
}

@Bean
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/springproject/project/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -12,38 +11,13 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import springproject.project.service.FacebookSignUpService;
import springproject.project.service.FacebookSignInAdapterService;

import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInController;


import javax.sql.DataSource;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

/*
TODO
Error cant find out the problem and btw the solution

@Autowired
private ConnectionFactoryLocator connectionFactoryLocator;

@Autowired
private UsersConnectionRepository usersConnectionRepository;

@Autowired
private FacebookSignUpService facebookSignUpService;
*/



@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

Expand Down Expand Up @@ -85,21 +59,6 @@ protected void configure(HttpSecurity http) throws Exception {
.accessDeniedPage("/access-denied");
}

/*
TODO
Same as before

@Bean
public ProviderSignInController providerSignInController() {
((InMemoryUsersConnectionRepository) usersConnectionRepository)
.setConnectionSignUp(facebookSignUpService);

return new ProviderSignInController(
connectionFactoryLocator,
usersConnectionRepository,
new FacebookSignInAdapterService());
}*/

@Override
public void configure(WebSecurity web) throws Exception {
web
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package springproject.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import springproject.project.model.User;
import springproject.project.model.bundle.ABundle;
import springproject.project.service.UserService;

@Controller
public class BucketController {

@Autowired
private UserService userService;

@Autowired
public BucketController(UserService userService) {
this.userService = userService;
}

@GetMapping(value = "/bucket")
public ModelAndView getBucket() {
Expand All @@ -30,9 +29,7 @@ public ModelAndView getBucket() {

@GetMapping(value = "/bucket/{type}")
public RedirectView applyBucket(@PathVariable(value = "type") String type) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());

User user = userService.getLoggedUser();

user.setBundle(type);
userService.validate(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
@Controller
public class FrontController {

@Autowired
private ImageService imageService;

@GetMapping(value= {"/", "/home"})
@Autowired
public FrontController(ImageService imageService) {
this.imageService = imageService;
}

@GetMapping(value = {"/", "/home"})
public ModelAndView homePage() {
ModelAndView _new = new ModelAndView();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package springproject.project.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -21,29 +19,32 @@
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

@Controller
public class GalleryController {

@Autowired
private ImageService imageService;

@Autowired
private UserService userService;

@Autowired
private ImageEditor imageEditor;

@Autowired
public GalleryController(ImageService imageService,
UserService userService,
ImageEditor imageEditor) {
this.imageEditor = imageEditor;
this.imageService = imageService;
this.userService = userService;
}

@GetMapping(value = "/gallery")
public ModelAndView getGallery() {
ModelAndView _new = new ModelAndView();

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
User user = userService.getLoggedUser();

Image image = new Image();
_new.addObject("image", image);
Expand All @@ -56,8 +57,7 @@ public ModelAndView getGallery() {
@PostMapping(value = "/upload")
public RedirectView addPhoto(@Valid Image image,
@RequestParam("file") MultipartFile file) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
User user = userService.getLoggedUser();

if (user.imageCounter() >= user.getBundleObject().numberOfFile())
return new RedirectView("gallery");
Expand All @@ -83,8 +83,7 @@ public RedirectView editPhoto(Image image) {

@GetMapping(value = "delete/{imgId}")
public RedirectView deletePhoto(@PathVariable(value = "imgId") int id) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
User user = userService.getLoggedUser();
Image image = imageService.getById(id);

userService.deleteImage(user, image);
Expand All @@ -97,21 +96,20 @@ public RedirectView deletePhoto(@PathVariable(value = "imgId") int id) {
return new RedirectView("/gallery");
}

@GetMapping(value = "download/{imgId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@GetMapping(value = "download/{imgId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public FileSystemResource downloadPhoto(@PathVariable(value = "imgId") int id,
HttpServletResponse response) {
HttpServletResponse response) {
Image img = imageService.getById(id);
File initialFile = new File(imageService.getImagePath() + img.getUser().getId() + "/" + img.getFilename());
return new FileSystemResource(initialFile);
}
File initialFile = new File(imageService.getImagePath() + img.getUser().getId() + "/" + img.getFilename());
return new FileSystemResource(initialFile);
}

@GetMapping(value = "details/{imageId}")
public ModelAndView detailsPhoto(@PathVariable(value = "imageId") int id) throws IOException {
public ModelAndView detailsPhoto(@PathVariable(value = "imageId") int id) {
ModelAndView _new = new ModelAndView();

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
User user = userService.getLoggedUser();
Image image = imageService.getById(id);

_new.addObject("image", image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springproject.project.model.Image;
Expand All @@ -20,21 +18,21 @@
import springproject.project.service.UserService;
import springproject.project.validator.TokenConstraint;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@org.springframework.web.bind.annotation.RestController
@Validated
public class RestAdminController {

@Autowired
private UserService userService;

@Autowired
private ImageService imageService;

@Autowired
public RestAdminController(UserService userService,
ImageService imageService) {
this.userService = userService;
this.imageService = imageService;
}

@RequestMapping("/rest/user")
public Object userInfo(@TokenConstraint @RequestHeader("token") String token,
@RequestParam(value = "id", defaultValue = "0") int id) {
Expand Down Expand Up @@ -108,7 +106,6 @@ public Object addImage(@TokenConstraint @RequestHeader("token") String token,

@RequestMapping(value = "/rest/bundle", produces = "application/json")
public Object getUserBundle(@RequestParam(value = "name", defaultValue = "") String name) {
System.out.println("A request is received with: " + name);
if (name.isEmpty())
return null;

Expand All @@ -120,10 +117,7 @@ public Object getUserBundle(@RequestParam(value = "name", defaultValue = "") Str
produces = MediaType.IMAGE_JPEG_VALUE
)
public ResponseEntity<byte[]> imageById(@TokenConstraint @RequestHeader("token") String token,
@RequestParam(value = "id", defaultValue = "0") int id,
HttpServletResponse response) throws IOException {
String path = imageService.getPathById(id);

@RequestParam(value = "id", defaultValue = "0") int id) throws IOException {
File serverFile = new File(imageService.getPathById(id));

byte[] bytes = Files.readAllBytes(serverFile.toPath());
Expand All @@ -132,12 +126,4 @@ public ResponseEntity<byte[]> imageById(@TokenConstraint @RequestHeader("token")
.contentType(MediaType.IMAGE_JPEG)
.body(bytes);
}


public ResponseEntity<String> key(HttpServletRequest request) {
if (request.getHeader("authorization") != "toto") {
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<String>(HttpStatus.OK);
}
}
22 changes: 11 additions & 11 deletions src/main/java/springproject/project/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@

import javax.validation.Valid;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import springproject.project.model.User;
import springproject.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

@Autowired
private UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping(value = "/login")
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}

@GetMapping(value="/registration")
public ModelAndView registration(){
@GetMapping(value = "/registration")
public ModelAndView registration() {
ModelAndView modelAndView = new ModelAndView();
User user = new User();
modelAndView.addObject("user", user);
Expand Down Expand Up @@ -57,7 +58,7 @@ public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult)
return modelAndView;
}

@PostMapping(value= "/update")
@PostMapping(value = "/update")
public ModelAndView updateUser(@Valid User user, BindingResult bindingResult) {
User userExists = userService.findUserByEmail(user.getEmail());
ModelAndView modelAndView = new ModelAndView();
Expand All @@ -75,12 +76,11 @@ public ModelAndView updateUser(@Valid User user, BindingResult bindingResult) {
return modelAndView;
}

@GetMapping(value="/profile")
public ModelAndView profile(Model model) {
@GetMapping(value = "/profile")
public ModelAndView profile() {
ModelAndView modelAndView = new ModelAndView();

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
User user = userService.getLoggedUser();

modelAndView.addObject("user", user);
modelAndView.setViewName("user/profile");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/springproject/project/model/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Data
@Entity
@Table(name = "image")
@EqualsAndHashCode(exclude="user")
@EqualsAndHashCode(exclude = "user")
public class Image {

@Id
Expand Down Expand Up @@ -44,7 +44,7 @@ public class Image {

@JsonIgnore
@ManyToOne
@JoinColumn(name="user_id", nullable=false)
@JoinColumn(name = "user_id", nullable = false)
private User user;

public boolean isIsPublic() {
Expand Down
Loading