Implement user-specific tutorial access and enhance authentication handling
This commit is contained in:
@@ -13,6 +13,8 @@ import com.jambotronGroup.jambotron.repository.UserRepository;
|
||||
import com.jambotronGroup.jambotron.security.jwt.JwtUtils;
|
||||
import com.jambotronGroup.jambotron.security.services.UserDetailsImpl;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
@@ -34,6 +36,8 @@ import java.util.stream.Collectors;
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
public class AuthController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
||||
@Autowired
|
||||
AuthenticationManager authenticationManager;
|
||||
|
||||
@@ -65,12 +69,16 @@ public class AuthController {
|
||||
.map(item -> item.getAuthority())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
logger.info("User {} authenticated successfully with roles: {}", userDetails.getUsername(), roles);
|
||||
|
||||
return ResponseEntity.ok().header(HttpHeaders.SET_COOKIE, jwtCookie.toString())
|
||||
.body(new UserInfoResponse(
|
||||
userDetails.getId(),
|
||||
userDetails.getUsername(),
|
||||
userDetails.getEmail(),
|
||||
roles));
|
||||
roles,
|
||||
jwtCookie.toString()
|
||||
));
|
||||
}
|
||||
|
||||
@PostMapping("/signup")
|
||||
|
||||
@@ -2,10 +2,20 @@ package com.jambotronGroup.jambotron.controllers;
|
||||
|
||||
|
||||
import com.jambotronGroup.jambotron.model.Tutorial;
|
||||
import com.jambotronGroup.jambotron.model.User;
|
||||
import com.jambotronGroup.jambotron.repository.TutorialRepository;
|
||||
import com.jambotronGroup.jambotron.repository.UserRepository;
|
||||
import com.jambotronGroup.jambotron.security.AuthenticationFacade;
|
||||
import com.jambotronGroup.jambotron.security.services.UserDetailsImpl;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -19,6 +29,11 @@ import java.util.Optional;
|
||||
@RequestMapping("/api")
|
||||
public class TutorialController {
|
||||
|
||||
@Autowired
|
||||
AuthenticationFacade authenticationFacade;
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
TutorialRepository tutorialRepository;
|
||||
|
||||
@@ -27,10 +42,29 @@ public class TutorialController {
|
||||
try {
|
||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
||||
|
||||
if (title == null)
|
||||
tutorialRepository.findAll().forEach(tutorials::add);
|
||||
else
|
||||
tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
String name = authenticationFacade.getAuthentication().getName();
|
||||
|
||||
User user = userRepository.findById(((UserDetailsImpl)userDetails).getId()).get();
|
||||
if (user.getRoles().stream().anyMatch(role -> role.getName().name().equals("ROLE_ADMIN"))) {
|
||||
if (title == null)
|
||||
tutorialRepository.findAll().forEach(tutorials::add);
|
||||
else
|
||||
tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);
|
||||
|
||||
} else {
|
||||
|
||||
// If the user is not an admin, filter tutorials by user
|
||||
tutorialRepository.findByUserId(user.getId()).forEach(tutorials::add);
|
||||
if (tutorials.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>(tutorials, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (tutorials.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
@@ -55,9 +89,15 @@ public class TutorialController {
|
||||
|
||||
@PostMapping("/tutorials")
|
||||
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
|
||||
User user = userRepository.findById(((UserDetailsImpl)userDetails).getId()).get();
|
||||
|
||||
try {
|
||||
Tutorial _tutorial = tutorialRepository
|
||||
.save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false));
|
||||
.save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false, user));
|
||||
return new ResponseEntity<>(_tutorial, HttpStatus.CREATED);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
||||
@@ -21,14 +21,19 @@ public class Tutorial {
|
||||
@Column(name = "published")
|
||||
private boolean published;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "userID", nullable = false)
|
||||
private User user;
|
||||
|
||||
public Tutorial() {
|
||||
|
||||
}
|
||||
|
||||
public Tutorial(String title, String description, boolean published) {
|
||||
public Tutorial(String title, String description, boolean published, User user) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.published = published;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
|
||||
@@ -10,11 +10,14 @@ public class UserInfoResponse {
|
||||
private String email;
|
||||
private List<String> roles;
|
||||
|
||||
public UserInfoResponse(Long id, String username, String email, List<String> roles) {
|
||||
private String token;
|
||||
|
||||
public UserInfoResponse(Long id, String username, String email, List<String> roles, String token) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.roles = roles;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@@ -44,4 +47,12 @@ public class UserInfoResponse {
|
||||
public List<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface TutorialRepository extends JpaRepository<Tutorial, Long> {
|
||||
|
||||
List<Tutorial> findByUserId(Long userId);
|
||||
List<Tutorial> findByPublished(boolean published);
|
||||
List<Tutorial> findByTitleContaining(String title);
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
|
||||
Optional<User> findByUsername(String username);
|
||||
|
||||
Boolean existsByUsername(String username);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.jambotronGroup.jambotron.security;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AuthenticationFacade implements IAuthenticationFacade {
|
||||
|
||||
@Override
|
||||
public Authentication getAuthentication() {
|
||||
|
||||
return SecurityContextHolder.getContext().getAuthentication();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.jambotronGroup.jambotron.security;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
public interface IAuthenticationFacade {
|
||||
Authentication getAuthentication();
|
||||
}
|
||||
@@ -43,10 +43,7 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
|
||||
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
// Do not add any mappings to enable complete disabling of CORS.
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public DaoAuthenticationProvider authenticationProvider() {
|
||||
@@ -118,7 +115,8 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
|
||||
|
||||
.requestMatchers("/main/**").permitAll()
|
||||
.requestMatchers("/api/test/**").permitAll()
|
||||
.requestMatchers("/api/tutorials").permitAll()
|
||||
|
||||
.requestMatchers("/api/tutorials").hasRole("ADMIN")
|
||||
|
||||
.requestMatchers("/api/zhipuai/image/**").permitAll()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user