server/src/main/java/com/example/server/jwt/JwtAccessDeniedHandler.java
<reponame>Sfarm18/Sfarmpackage com.example.server.jwt;


import io.jsonwebtoken.io.IOException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 유효하지 않은 접근을할 때 response에 error를 만들어주는 컴포넌트
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 필요한 권한이 없이 접근하려 할때 403
        try {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
