-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from kursph/feat/jwt-security
Feat/jwt security
- Loading branch information
Showing
38 changed files
with
871 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/kursph/auth/AuthenticationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.kursph.auth; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("api/v1/auth") | ||
public class AuthenticationController { | ||
private final AuthenticationService authenticationService; | ||
|
||
public AuthenticationController(AuthenticationService authenticationService) { | ||
this.authenticationService = authenticationService; | ||
} | ||
|
||
@PostMapping("login") | ||
public ResponseEntity<?> login(@RequestBody AuthenticationRequest authenticationRequest) { | ||
AuthenticationResponse authenticationResponse = authenticationService.login(authenticationRequest); | ||
|
||
return ResponseEntity | ||
.ok() | ||
.header(HttpHeaders.AUTHORIZATION, authenticationResponse.token()) | ||
.body(authenticationResponse); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/com/kursph/auth/AuthenticationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.kursph.auth; | ||
|
||
public record AuthenticationRequest( | ||
String username, | ||
String password | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/kursph/auth/AuthenticationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.kursph.auth; | ||
|
||
import com.kursph.customer.CustomerDTO; | ||
|
||
public record AuthenticationResponse( | ||
String token, | ||
CustomerDTO customerDTO | ||
) { | ||
} |
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/com/kursph/auth/AuthenticationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.kursph.auth; | ||
|
||
import com.kursph.customer.Customer; | ||
import com.kursph.customer.CustomerDTO; | ||
import com.kursph.customer.CustomerDTOMapper; | ||
import com.kursph.jwt.JWTUtil; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AuthenticationService { | ||
private final AuthenticationManager authenticationManager; | ||
private final CustomerDTOMapper customerDTOMapper; | ||
private final JWTUtil jwtUtil; | ||
|
||
public AuthenticationService(AuthenticationManager authenticationManager, CustomerDTOMapper customerDTOMapper, JWTUtil jwtUtil) { | ||
this.authenticationManager = authenticationManager; | ||
this.customerDTOMapper = customerDTOMapper; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
public AuthenticationResponse login(AuthenticationRequest authenticationRequest) { | ||
Authentication authentication = authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken( | ||
authenticationRequest.username(), | ||
authenticationRequest.password() | ||
) | ||
); | ||
Customer principal = (Customer) authentication.getPrincipal(); | ||
CustomerDTO customerDTO = customerDTOMapper.apply(principal); | ||
String token = jwtUtil.issueToken(customerDTO.username(), customerDTO.roles()); | ||
|
||
return new AuthenticationResponse(token, customerDTO); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/kursph/customer/CustomerDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.kursph.customer; | ||
|
||
import java.util.List; | ||
|
||
public record CustomerDTO( | ||
Integer id, | ||
String name, | ||
String email, | ||
Integer age, | ||
String gender, | ||
List<String> roles, | ||
String username | ||
) { | ||
} |
26 changes: 26 additions & 0 deletions
26
backend/src/main/java/com/kursph/customer/CustomerDTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.kursph.customer; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class CustomerDTOMapper implements Function<Customer, CustomerDTO> { | ||
@Override | ||
public CustomerDTO apply(Customer customer) { | ||
return new CustomerDTO( | ||
customer.getId(), | ||
customer.getName(), | ||
customer.getEmail(), | ||
customer.getAge(), | ||
customer.getGender(), | ||
customer.getAuthorities() | ||
.stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.collect(Collectors.toList()), | ||
customer.getUsername() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ public class CustomerDataAccessService implements CustomerDAO{ | |
|
||
Customer customer1 = new Customer( | ||
1, | ||
"Alex", | ||
"foobar", "Alex", | ||
"[email protected]", | ||
22, | ||
"MALE" | ||
|
@@ -53,4 +53,10 @@ public void removeCustomer(Customer customer) { | |
public void updateCustomer(Customer customer) { | ||
|
||
} | ||
|
||
@Override | ||
public Optional<Customer> selectUserByEmail(String email) { | ||
return customers.stream().filter(customer -> customer.getUsername().equals(email)) | ||
.findFirst(); | ||
} | ||
} |
Oops, something went wrong.