add spring security and token management
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.iconplus.smartproc.configuration;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.iconplus.smartproc.exception.ErrorResponse;
|
||||
import com.iconplus.smartproc.util.Constants;
|
||||
import org.springframework.http.MediaType;
|
||||
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 java.io.IOException;
|
||||
|
||||
|
||||
@Component
|
||||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException authException) throws IOException {
|
||||
|
||||
ErrorResponse errorResponse = new ErrorResponse();
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
|
||||
var exception = (Exception) request.getAttribute("exception");
|
||||
|
||||
String message;
|
||||
|
||||
if (exception != null) {
|
||||
|
||||
if (exception.getCause() != null) {
|
||||
message = exception.getCause().toString() + " " + exception.getMessage();
|
||||
} else {
|
||||
message = exception.getMessage();
|
||||
}
|
||||
|
||||
errorResponse.setCode(Constants.ERR_CODE_40051);
|
||||
errorResponse.setTitle(Constants.TITLE_INVALID_NEXT_STEP);
|
||||
errorResponse.setMessage(message);
|
||||
response.getOutputStream()
|
||||
.println(new ObjectMapper().writeValueAsString(errorResponse));
|
||||
} else {
|
||||
errorResponse.setCode(Constants.ERR_CODE_80007);
|
||||
errorResponse.setTitle(Constants.TITLE_INVALID_NEXT_STEP);
|
||||
errorResponse.setMessage("Invalid Access Token");
|
||||
response.getOutputStream()
|
||||
.println(new ObjectMapper().writeValueAsString(errorResponse));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user