Skip to content

Commit

Permalink
upgraded to spring boot 3
Browse files Browse the repository at this point in the history
  • Loading branch information
tofiksa committed Jan 28, 2024
1 parent a1a05d8 commit debd700
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 83 deletions.
41 changes: 29 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<version>3.1.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<spring-boot.version>2.7.0</spring-boot.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<spring-boot.version>3.1.4</spring-boot.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -50,23 +50,22 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.7.1</version>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.7.2</version>
<version>6.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.7.1</version>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
Expand All @@ -78,6 +77,11 @@
<artifactId>javax.persistence</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -106,15 +110,29 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.13</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -125,12 +143,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
<version>5.10.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/no/josefushighscore/configure/CorsFilter.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package no.josefushighscore.configure;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
public class CorsFilter implements Filter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/register/**").hasRole("ANONYMOUS")
.antMatchers("/auth/**").hasRole("ANONYMOUS").anyRequest().authenticated().and()
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/register/**").hasRole("ANONYMOUS")
.requestMatchers("/auth/**").hasRole("ANONYMOUS").anyRequest().authenticated().and()
.apply(new JwtSecurityConfigurer(jwtTokenProvider));

return http.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<LoginUserDto> signin(@RequestBody LoginUserDto data) throw

@Secured("ROLE_ANONYMOUS")
@PostMapping("/register")
public ResponseEntity registerNewUserAccount(@Valid @RequestBody UserDto accountDto) {
public ResponseEntity registerNewUserAccount(@Valid @RequestBody UserDto accountDto) throws Exception{

APIResponse apiResponse = new APIResponse();
try {
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/no/josefushighscore/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package no.josefushighscore.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.tomcat.jni.User;
import no.josefushighscore.model.User;


public class UserDto {

@JsonProperty("username")
private String username;

@JsonProperty("password")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;

@JsonProperty("firstname")
Expand Down Expand Up @@ -70,5 +71,20 @@ public void setEmail(String email) {
}


public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import jakarta.servlet.ServletException;
import no.josefushighscore.service.APIResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,8 +12,6 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.ServletException;

@RestControllerAdvice
public class GlobalExceptionHandler {

Expand Down Expand Up @@ -65,15 +64,7 @@ public ResponseEntity handleUsernameNotFoundException(UsernameNotFoundException
return ResponseEntity.status(apiResponse.getStatus()).body(apiResponse);
}

@ExceptionHandler
public ResponseEntity handleEmailExistsException(EmailExistsException e){

APIResponse apiResponse = new APIResponse();
apiResponse.setStatus(HttpStatus.BAD_REQUEST);
apiResponse.setError("Email already exists!");

return ResponseEntity.status(apiResponse.getStatus()).body(apiResponse);
}

@ExceptionHandler
public ResponseEntity handleSignatureException(SignatureException e){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.josefushighscore.exception;


public class UsernameExistsException extends RuntimeException {
public UsernameExistsException(String message) {
super(message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/no/josefushighscore/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/josefushighscore/model/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/no/josefushighscore/model/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.josefushighscore.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -8,7 +9,6 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -29,12 +29,14 @@ public class User implements UserDetails {
Long userId;

@NotEmpty
@Column(name="`username`", unique = true)
private String username;

@NotEmpty
private String password;

@NotEmpty
@Column(name="`email`", unique = true)
private String email;

@NotEmpty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package no.josefushighscore.security.jwt;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
Expand All @@ -17,10 +16,7 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException {
log.error("Jwt authentication failed:" + authException);
response.sendError(HttpServletResponse.SC_UNAUTHORIZED , "Jwt authentication failed");

log.error("Jwt authentication failed: " + authException);
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Jwt authentication failed");
}

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.josefushighscore.security.jwt;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
Expand All @@ -9,7 +10,8 @@
@Data
public class JwtProperties {

private String secretKey = "shniniupel";
@Value("${spring.jwt.secret}")
private String secretKey;

//validity in milliseconds
//private long validityInMs = 3600000; // 1h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import no.josefushighscore.exception.BadRequestException;
import no.josefushighscore.service.APIResponse;
import org.slf4j.Logger;
Expand All @@ -12,9 +15,6 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.Date;
import java.util.Enumeration;
Expand Down
Loading

0 comments on commit debd700

Please sign in to comment.