forked from up1/assignment-java-boot-camp
-
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.
Create filter to token from request header #35
- Loading branch information
1 parent
276408e
commit 919f6c6
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
demo/week01/src/main/java/com/sol/demoecom/common/AuthenticationFilter.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,81 @@ | ||
package com.sol.demoecom.common; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.sol.demoecom.user.model.UserModel; | ||
import com.sol.demoecom.user.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
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 java.io.IOException; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
public class AuthenticationFilter extends OncePerRequestFilter { | ||
private static String SHOULD_NOT_FILTER = "not-filter"; | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try { | ||
String token = request.getHeader("token"); | ||
List<String> urls = new ArrayList<>(); | ||
urls.add("/user/basket"); | ||
urls.add("/user/addresses"); | ||
urls.add("/user/orders"); | ||
urls.add("/user/orders/payment"); | ||
|
||
if(stringContainsItemFromList(request.getRequestURI(), urls).size() > 0) { | ||
if(token == null) { | ||
throw new UnauthorizationException(); | ||
} | ||
|
||
UUID userId = UUID.fromString(token); | ||
Optional<UserModel> user = userRepository.findById(userId); | ||
|
||
if(!user.isPresent()) { | ||
throw new UnauthorizationException(); | ||
} | ||
} | ||
|
||
|
||
filterChain.doFilter(request, response); | ||
|
||
} catch (RuntimeException e) { | ||
ResponseFail responseFail = new ResponseFail(e.getMessage()); | ||
|
||
response.setStatus(HttpStatus.BAD_REQUEST.value()); | ||
response.setContentType("application/json"); | ||
response.getWriter().write(convertObjectToJson(responseFail)); | ||
} | ||
} | ||
|
||
@Override | ||
protected boolean shouldNotFilter(HttpServletRequest request) { | ||
return Boolean.TRUE.equals(request.getAttribute(SHOULD_NOT_FILTER)); | ||
} | ||
|
||
private List<String> stringContainsItemFromList(String search, List<String> list) { | ||
List<String> matchingElements = list.stream() | ||
.filter(str -> search.trim().contains(str)) | ||
.collect(Collectors.toList()); | ||
return matchingElements; | ||
} | ||
|
||
private String convertObjectToJson(Object object) throws JsonProcessingException { | ||
if (object == null) { | ||
return null; | ||
} | ||
ObjectMapper mapper = new ObjectMapper(); | ||
return mapper.writeValueAsString(object); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
demo/week01/src/main/java/com/sol/demoecom/common/UnauthorizationException.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.sol.demoecom.common; | ||
|
||
public class UnauthorizationException extends RuntimeException{ | ||
public UnauthorizationException() { | ||
super("Invalid token."); | ||
} | ||
} |
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