diff --git a/pom.xml b/pom.xml index eaea39d..c3ef8fc 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.8 + 3.0.2 @@ -182,7 +182,7 @@ org.springframework.boot spring-boot-configuration-processor - 2.7.8 + 3.0.2 diff --git a/src/main/java/com/ohmyclass/api/components/classmember/entity/ClassMember.java b/src/main/java/com/ohmyclass/api/components/classmember/entity/ClassMember.java index 3696d96..161bef2 100644 --- a/src/main/java/com/ohmyclass/api/components/classmember/entity/ClassMember.java +++ b/src/main/java/com/ohmyclass/api/components/classmember/entity/ClassMember.java @@ -1,4 +1,5 @@ package com.ohmyclass.api.components.classmember.entity; + import com.fasterxml.jackson.annotation.JsonManagedReference; import com.ohmyclass.api.components.group.entity.Group; import lombok.Getter; @@ -6,7 +7,7 @@ import java.util.Set; -import javax.persistence.*; +import jakarta.persistence.*; import com.ohmyclass.api.components.user.entity.User; @@ -16,7 +17,7 @@ @Table(name = "classmember") public class ClassMember { - @Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) private Long id; @@ -29,7 +30,7 @@ public class ClassMember { @JsonManagedReference private Group group; -// @ManyToMany(fetch = FetchType.LAZY) -// private List roles; + // @ManyToMany(fetch = FetchType.LAZY) + // private List roles; } diff --git a/src/main/java/com/ohmyclass/api/components/classmember/entity/GroupMember.java b/src/main/java/com/ohmyclass/api/components/classmember/entity/GroupMember.java index ef0e755..1bf043b 100644 --- a/src/main/java/com/ohmyclass/api/components/classmember/entity/GroupMember.java +++ b/src/main/java/com/ohmyclass/api/components/classmember/entity/GroupMember.java @@ -7,17 +7,17 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; -import javax.persistence.ManyToOne; -import javax.persistence.OneToOne; -import javax.persistence.Table; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; import java.util.Set; @Getter @@ -44,7 +44,7 @@ public class GroupMember { private Group group; -// @ManyToMany(fetch = FetchType.LAZY) -// private List roles; + // @ManyToMany(fetch = FetchType.LAZY) + // private List roles; } diff --git a/src/main/java/com/ohmyclass/api/components/comment/entity/Comment.java b/src/main/java/com/ohmyclass/api/components/comment/entity/Comment.java index 9da5326..ccc5cba 100644 --- a/src/main/java/com/ohmyclass/api/components/comment/entity/Comment.java +++ b/src/main/java/com/ohmyclass/api/components/comment/entity/Comment.java @@ -6,16 +6,16 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.Table; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import java.util.Date; import java.util.Set; diff --git a/src/main/java/com/ohmyclass/api/components/group/entity/Group.java b/src/main/java/com/ohmyclass/api/components/group/entity/Group.java index 4bf22c0..16c6fc1 100644 --- a/src/main/java/com/ohmyclass/api/components/group/entity/Group.java +++ b/src/main/java/com/ohmyclass/api/components/group/entity/Group.java @@ -9,7 +9,7 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; import java.util.Set; @Getter @@ -17,7 +17,7 @@ @Entity @Table(name = "group") public class Group { - @Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) private Long id; @@ -31,9 +31,7 @@ public class Group { @OneToMany(fetch = FetchType.LAZY) private Set subjects; - @OneToMany(cascade = {CascadeType.ALL}, - orphanRemoval = true, - mappedBy = "group") + @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "group") @JsonBackReference private Set tasks; @@ -42,7 +40,7 @@ public class Group { @JsonManagedReference private User user; -// @ManyToMany(fetch = FetchType.LAZY) -// private Set roles; - + // @ManyToMany(fetch = FetchType.LAZY) + // private Set roles; + } diff --git a/src/main/java/com/ohmyclass/api/components/preferences/entity/Preferences.java b/src/main/java/com/ohmyclass/api/components/preferences/entity/Preferences.java index c2ebc6a..2db55d5 100644 --- a/src/main/java/com/ohmyclass/api/components/preferences/entity/Preferences.java +++ b/src/main/java/com/ohmyclass/api/components/preferences/entity/Preferences.java @@ -4,7 +4,7 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; @Getter @Setter diff --git a/src/main/java/com/ohmyclass/api/components/role/entity/Role.java b/src/main/java/com/ohmyclass/api/components/role/entity/Role.java index 1337d57..e7206f3 100644 --- a/src/main/java/com/ohmyclass/api/components/role/entity/Role.java +++ b/src/main/java/com/ohmyclass/api/components/role/entity/Role.java @@ -5,15 +5,15 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.Column; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import java.util.List; @Getter @@ -22,9 +22,9 @@ @Table(name = "role") public class Role { - public Role() {} - - public Role(String name) { + public Role() {} + + public Role(String name) { this.name = name; } @@ -45,8 +45,11 @@ public Role(String name) { @JsonManagedReference private User user; - /* @ManyToOne - @JoinColumn(name = "fkGroup") - @JsonManagedReference - private Group fkGroup;*/ + /* + * @ManyToOne + * + * @JoinColumn(name = "fkGroup") + * + * @JsonManagedReference private Group fkGroup; + */ } diff --git a/src/main/java/com/ohmyclass/api/components/subject/entity/Subject.java b/src/main/java/com/ohmyclass/api/components/subject/entity/Subject.java index 0099873..86606c0 100644 --- a/src/main/java/com/ohmyclass/api/components/subject/entity/Subject.java +++ b/src/main/java/com/ohmyclass/api/components/subject/entity/Subject.java @@ -7,16 +7,16 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.Table; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import java.util.Set; @Getter @@ -43,9 +43,7 @@ public class Subject { @JsonManagedReference private Group group; - @OneToMany(cascade = {CascadeType.ALL}, - orphanRemoval = true, - mappedBy = "subject") + @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "subject") @JsonBackReference private Set tasks; } diff --git a/src/main/java/com/ohmyclass/api/components/task/entity/Task.java b/src/main/java/com/ohmyclass/api/components/task/entity/Task.java index 5ec689d..972f052 100644 --- a/src/main/java/com/ohmyclass/api/components/task/entity/Task.java +++ b/src/main/java/com/ohmyclass/api/components/task/entity/Task.java @@ -10,17 +10,17 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.OneToOne; -import javax.persistence.Table; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; import java.util.Date; import java.util.List; import java.util.Set; @@ -30,7 +30,7 @@ @Entity @Table(name = "Task") public class Task { - + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) diff --git a/src/main/java/com/ohmyclass/api/components/tick/entity/Tick.java b/src/main/java/com/ohmyclass/api/components/tick/entity/Tick.java index 0d31c7f..ee1a05e 100644 --- a/src/main/java/com/ohmyclass/api/components/tick/entity/Tick.java +++ b/src/main/java/com/ohmyclass/api/components/tick/entity/Tick.java @@ -5,7 +5,7 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.*; +import jakarta.persistence.*; @Getter @Setter diff --git a/src/main/java/com/ohmyclass/api/components/user/controller/IUserController.java b/src/main/java/com/ohmyclass/api/components/user/controller/IUserController.java index 3441140..f06e070 100644 --- a/src/main/java/com/ohmyclass/api/components/user/controller/IUserController.java +++ b/src/main/java/com/ohmyclass/api/components/user/controller/IUserController.java @@ -15,8 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.Map; @RestController @@ -25,16 +25,18 @@ public interface IUserController { @PostMapping("/auth/register") @Operation(summary = "Register a user into the database") - @ApiResponse(responseCode = "200", description = "The generated", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class)) }) + @ApiResponse(responseCode = "200", description = "The generated", + content = { + @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") Response> register(@RequestBody UserInDTO registration); @PostMapping("/auth/refresh") @Operation(summary = "Refresh the access token") - @ApiResponse(responseCode = "200", description = "The updated token", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class)) }) + @ApiResponse(responseCode = "200", description = "The updated token", + content = { + @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") void refreshToken(HttpServletRequest request, HttpServletResponse response); @@ -42,8 +44,9 @@ public interface IUserController { @Secured("ROLE_USER") @PutMapping("/auth/password-forgotten") @Operation(summary = "Request a URL to change the password") - @ApiResponse(responseCode = "200", description = "Email has been sent", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = UserOutDTO.class)) }) + @ApiResponse(responseCode = "200", description = "Email has been sent", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = UserOutDTO.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") void passwordForgotten(HttpServletRequest request, HttpServletResponse response); @@ -51,8 +54,9 @@ public interface IUserController { @Secured("ROLE_ADMIN") @GetMapping("/user/{username}") @Operation(summary = "Get a user from the database") - @ApiResponse(responseCode = "200", description = "User", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = UserOutDTO.class)) }) + @ApiResponse(responseCode = "200", description = "User", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = UserOutDTO.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") Response getUser(@PathVariable String username); @@ -60,8 +64,9 @@ public interface IUserController { @Secured("ROLE_USER") @PutMapping("/user") @Operation(summary = "Edit user details in database") - @ApiResponse(responseCode = "200", description = "The updated user", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = Boolean.class)) }) + @ApiResponse(responseCode = "200", description = "The updated user", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = Boolean.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") Response updateUser(@RequestBody UserChangeInDTO userChangeIn); @@ -69,8 +74,9 @@ public interface IUserController { @Secured("ROLE_USER") @DeleteMapping("/user") @Operation(summary = "Delete a user from the database") - @ApiResponse(responseCode = "200", description = "The success of the operation", content = { - @Content(mediaType = "application/json", schema = @Schema(implementation = Boolean.class)) }) + @ApiResponse(responseCode = "200", description = "The success of the operation", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = Boolean.class))}) @ApiResponse(responseCode = "401", description = "Authentication failed") @ApiResponse(responseCode = "500", description = "General server error") Response deleteUser(@RequestBody String username); diff --git a/src/main/java/com/ohmyclass/api/components/user/controller/impl/UserController.java b/src/main/java/com/ohmyclass/api/components/user/controller/impl/UserController.java index f9ebc3b..e9a6cc9 100644 --- a/src/main/java/com/ohmyclass/api/components/user/controller/impl/UserController.java +++ b/src/main/java/com/ohmyclass/api/components/user/controller/impl/UserController.java @@ -12,8 +12,8 @@ import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.Map; @RestController diff --git a/src/main/java/com/ohmyclass/api/components/user/entity/User.java b/src/main/java/com/ohmyclass/api/components/user/entity/User.java index b9efc7f..b2e4c81 100644 --- a/src/main/java/com/ohmyclass/api/components/user/entity/User.java +++ b/src/main/java/com/ohmyclass/api/components/user/entity/User.java @@ -12,17 +12,17 @@ import lombok.Getter; import lombok.Setter; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; -import javax.persistence.OneToMany; -import javax.persistence.OneToOne; -import javax.persistence.Table; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; import java.util.ArrayList; @@ -66,15 +66,11 @@ public class User { @JsonManagedReference private Set subjects; - @OneToMany(cascade = {CascadeType.ALL}, - orphanRemoval = true, - mappedBy = "user") + @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "user") @JsonBackReference private Set tasks; - @OneToMany(cascade = {CascadeType.ALL}, - orphanRemoval = true, - mappedBy = "user") + @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "user") @JsonBackReference private List roles; diff --git a/src/main/java/com/ohmyclass/api/components/user/service/crud/IUserService.java b/src/main/java/com/ohmyclass/api/components/user/service/crud/IUserService.java index 7598c8f..b40f6da 100644 --- a/src/main/java/com/ohmyclass/api/components/user/service/crud/IUserService.java +++ b/src/main/java/com/ohmyclass/api/components/user/service/crud/IUserService.java @@ -7,9 +7,9 @@ import com.ohmyclass.api.util.communication.Request; import com.ohmyclass.api.util.communication.Response; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.transaction.Transactional; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.transaction.Transactional; import java.util.Map; public interface IUserService { diff --git a/src/main/java/com/ohmyclass/api/components/user/service/crud/impl/UserService.java b/src/main/java/com/ohmyclass/api/components/user/service/crud/impl/UserService.java index d00b149..346de0f 100644 --- a/src/main/java/com/ohmyclass/api/components/user/service/crud/impl/UserService.java +++ b/src/main/java/com/ohmyclass/api/components/user/service/crud/impl/UserService.java @@ -18,8 +18,8 @@ import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -47,9 +47,8 @@ public Map register(UserInDTO inDTO) { User persistedUser = userSubmissionProcessor.getPersistedEntity(); - List roles = persistedUser.getRoles().stream() - .map(Role::getName) - .collect(Collectors.toList()); + List roles = + persistedUser.getRoles().stream().map(Role::getName).collect(Collectors.toList()); return tokenUtil.generateNewTokenMap(persistedUser.getUsername(), "Registration", roles); } @@ -108,6 +107,5 @@ public Boolean delete(String username) { } @Override - public void passwordForgotten(HttpServletRequest request, HttpServletResponse response) { - } + public void passwordForgotten(HttpServletRequest request, HttpServletResponse response) {} } diff --git a/src/main/java/com/ohmyclass/api/components/user/service/mapper/UserMapper.java b/src/main/java/com/ohmyclass/api/components/user/service/mapper/UserMapper.java index 497954c..ff9d06e 100644 --- a/src/main/java/com/ohmyclass/api/components/user/service/mapper/UserMapper.java +++ b/src/main/java/com/ohmyclass/api/components/user/service/mapper/UserMapper.java @@ -4,9 +4,8 @@ import com.ohmyclass.api.components.user.dto.in.UserInDTO; import com.ohmyclass.api.components.user.dto.out.UserOutDTO; import com.ohmyclass.api.components.user.entity.User; -import com.sun.istack.NotNull; +import jakarta.validation.constraints.NotNull; import org.mapstruct.*; -import org.springframework.security.crypto.password.PasswordEncoder; @Mapper(componentModel = "spring", uses = APreferencesMapper.class) public abstract class UserMapper { diff --git a/src/main/java/com/ohmyclass/api/util/communication/CreateResponseService.java b/src/main/java/com/ohmyclass/api/util/communication/CreateResponseService.java index d8ac212..e9892dd 100644 --- a/src/main/java/com/ohmyclass/api/util/communication/CreateResponseService.java +++ b/src/main/java/com/ohmyclass/api/util/communication/CreateResponseService.java @@ -3,22 +3,25 @@ import com.ohmyclass.api.util.validation.ValidationResult; import com.ohmyclass.api.util.validation.types.ValidationStatus; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; public class CreateResponseService { - public static void newError(T t, String message, ValidationResult validationResult) { + public static void newError(T t, String message, + ValidationResult validationResult) { validationResult.add(ValidationStatus.ERROR, message); t.setValidationResult(validationResult); } - public static void newSuccess(T t, String message, ValidationResult validationResult) { + public static void newSuccess(T t, String message, + ValidationResult validationResult) { validationResult.add(ValidationStatus.OK, message); t.setValidationResult(validationResult); } - public static void newError(T t, int status, String message) throws IOException { + public static void newError(T t, int status, String message) + throws IOException { t.setStatus(status); t.getWriter().write(message); } diff --git a/src/main/java/com/ohmyclass/security/filters/JwtAuthenticationFilter.java b/src/main/java/com/ohmyclass/security/filters/JwtAuthenticationFilter.java index 0ccd809..1310197 100644 --- a/src/main/java/com/ohmyclass/security/filters/JwtAuthenticationFilter.java +++ b/src/main/java/com/ohmyclass/security/filters/JwtAuthenticationFilter.java @@ -13,9 +13,9 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.stereotype.Component; -import javax.servlet.FilterChain; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; diff --git a/src/main/java/com/ohmyclass/security/filters/JwtAuthorizationFilter.java b/src/main/java/com/ohmyclass/security/filters/JwtAuthorizationFilter.java index bdaa1e9..b13385d 100644 --- a/src/main/java/com/ohmyclass/security/filters/JwtAuthorizationFilter.java +++ b/src/main/java/com/ohmyclass/security/filters/JwtAuthorizationFilter.java @@ -1,20 +1,18 @@ package com.ohmyclass.security.filters; import com.auth0.jwt.interfaces.DecodedJWT; -import com.ohmyclass.api.exceptions.ApiException; import com.ohmyclass.security.util.JwtTokenUtil; import com.ohmyclass.server.properties.JwtConstants; -import lombok.extern.log4j.Log4j2; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -29,7 +27,6 @@ * * @author z-100 */ -@Log4j2 @Component public class JwtAuthorizationFilter extends OncePerRequestFilter { @@ -37,8 +34,8 @@ public class JwtAuthorizationFilter extends OncePerRequestFilter { private JwtConstants constants; - private final Predicate isUnprotectedUrl = (req) -> - constants.getUriwhitelist().stream().anyMatch(req::contains); + private final Predicate isUnprotectedUrl = + (req) -> constants.getUriwhitelist().stream().anyMatch(req::contains); public JwtAuthorizationFilter(JwtTokenUtil tokenUtil, JwtConstants constants) { @@ -54,8 +51,8 @@ protected boolean shouldNotFilter(HttpServletRequest request) { } @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) - throws ServletException, IOException { + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, + FilterChain filterChain) throws ServletException, IOException { createSessionFrom(request.getHeader(AUTHORIZATION)); @@ -73,9 +70,9 @@ private void createSessionFrom(String authorizationHeader) { String username = decodedJWT.getSubject(); String[] roles = decodedJWT.getClaim(constants.getClaims().get("roles")).asArray(String.class); - List authorities = roles != null ? Stream.of(roles) - .map(SimpleGrantedAuthority::new) - .collect(Collectors.toList()) : new ArrayList<>(); + List authorities = roles != null + ? Stream.of(roles).map(SimpleGrantedAuthority::new).collect(Collectors.toList()) + : new ArrayList<>(); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, authorities); diff --git a/src/main/java/com/ohmyclass/security/util/handler/JwtAuthenticationEntryPoint.java b/src/main/java/com/ohmyclass/security/util/handler/JwtAuthenticationEntryPoint.java index 3c0e6be..883255e 100644 --- a/src/main/java/com/ohmyclass/security/util/handler/JwtAuthenticationEntryPoint.java +++ b/src/main/java/com/ohmyclass/security/util/handler/JwtAuthenticationEntryPoint.java @@ -3,9 +3,8 @@ import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Serializable; @@ -26,4 +25,5 @@ public void commence(HttpServletRequest request, HttpServletResponse response, response.setContentType(APPLICATION_JSON_VALUE); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "unauthorized"); } + } diff --git a/src/main/java/com/ohmyclass/server/config/SecurityConfig.java b/src/main/java/com/ohmyclass/server/config/SecurityConfig.java index 076c3f2..dc8cb3b 100644 --- a/src/main/java/com/ohmyclass/server/config/SecurityConfig.java +++ b/src/main/java/com/ohmyclass/server/config/SecurityConfig.java @@ -1,5 +1,6 @@ package com.ohmyclass.server.config; +import com.ohmyclass.api.components.user.entity.User; import com.ohmyclass.api.exceptions.ApiExceptionHandler; import com.ohmyclass.security.filters.JwtAuthenticationFilter; import com.ohmyclass.security.filters.JwtAuthorizationFilter; @@ -12,9 +13,12 @@ import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS; @@ -27,7 +31,7 @@ @Configuration @EnableWebSecurity @RequiredArgsConstructor -public class SecurityConfig extends WebSecurityConfigurerAdapter { +public class SecurityConfig { private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; @@ -37,36 +41,21 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { private final JwtAuthorizationFilter jwtAuthorizationFilter; - private static final String[] AUTH_WHITELIST = new String[] { - "/api/v1/auth/login", - "/api/v1/auth/register", - "/api/v1/auth/forgotten", - "/api-docs", - "/swagger-resources/", - "/swagger-ui" - }; - - - @Override - protected void configure(HttpSecurity http) throws Exception { + private static final String[] AUTH_WHITELIST = + new String[] {"/api/v1/auth/login", "/api/v1/auth/register", "/api/v1/auth/forgotten", + "/api-docs", "/swagger-resources/", "/swagger-ui"}; + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { jwtAuthenticationFilter.setFilterProcessesUrl("/api/v1/auth/login"); jwtAuthenticationFilter.setAuthenticationManager(authenticationManagerBean()); - http.csrf().disable() - .sessionManagement() - .sessionCreationPolicy(STATELESS) - .and() - .exceptionHandling() - .authenticationEntryPoint(jwtAuthenticationEntryPoint) - .and() - .antMatcher("/") - .authorizeRequests() - .antMatchers(AUTH_WHITELIST).permitAll() - .anyRequest().authenticated() - .and() - .addFilter(jwtAuthenticationFilter) - .addFilterBefore(jwtAuthorizationFilter, UsernamePasswordAuthenticationFilter.class); + http.csrf().disable().sessionManagement().sessionCreationPolicy(STATELESS).and() + .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and() + .authorizeHttpRequests((auth) -> auth.requestMatchers(AUTH_WHITELIST).permitAll() + .anyRequest().authenticated().and().addFilter(jwtAuthenticationFilter) + .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)); + return http.build(); } @Override diff --git a/src/main/java/com/ohmyclass/server/config/WebConfig.java b/src/main/java/com/ohmyclass/server/config/WebConfig.java index 2acc762..b37e7f8 100644 --- a/src/main/java/com/ohmyclass/server/config/WebConfig.java +++ b/src/main/java/com/ohmyclass/server/config/WebConfig.java @@ -1,7 +1,6 @@ package com.ohmyclass.server.config; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.web.servlet.config.annotation.*; @EnableWebMvc