From 2bcca44555acf2d288393bd2e30396853682fc8b Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 11 Mar 2015 16:13:37 +0200 Subject: [PATCH] Fix access token problem --- .../org/baeldung/web/RedditController.java | 2 ++ .../baeldung/web/RestExceptionHandler.java | 25 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 51ca005752..ea0bc140bd 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -223,6 +223,8 @@ public class RedditController { } else { user.setNeedCaptcha(false); } + user.setAccessToken(token.getValue()); + user.setRefreshToken(token.getRefreshToken().getValue()); userReopsitory.save(user); } diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RestExceptionHandler.java b/spring-security-oauth/src/main/java/org/baeldung/web/RestExceptionHandler.java index c96b7020a2..cb970a4a66 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RestExceptionHandler.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RestExceptionHandler.java @@ -4,8 +4,6 @@ import java.io.Serializable; import javax.servlet.http.HttpServletResponse; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException; import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException; @@ -14,6 +12,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @ControllerAdvice @@ -30,17 +29,23 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler impleme // 4xx @ExceptionHandler({ OAuth2AccessDeniedException.class }) - public ResponseEntity handleOAuth2AccessDeniedException(final OAuth2AccessDeniedException ex, final WebRequest request) { + public ModelAndView handleOAuth2AccessDeniedException(final OAuth2AccessDeniedException ex, final WebRequest request) { logger.error("403 Status Code", ex); final String response = "Error Occurred - Forbidden: " + ex.getMessage(); - return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.FORBIDDEN, request); + final ModelAndView model = new ModelAndView("submissionResponse"); + model.addObject("msg", response); + return model; + // return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.FORBIDDEN, request); } @ExceptionHandler({ HttpClientErrorException.class }) - public ResponseEntity handleHttpClientErrorException(final HttpClientErrorException ex, final WebRequest request) { + public ModelAndView handleHttpClientErrorException(final HttpClientErrorException ex, final WebRequest request) { logger.error("400 Status Code", ex); final String response = "Error Occurred - To Many Requests: " + ex.getMessage(); - return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.TOO_MANY_REQUESTS, request); + final ModelAndView model = new ModelAndView("submissionResponse"); + model.addObject("msg", response); + return model; + // return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.TOO_MANY_REQUESTS, request); } // HttpClientErrorException @@ -53,10 +58,12 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler impleme } @ExceptionHandler({ Exception.class }) - public ResponseEntity handleInternal(final RuntimeException ex, final WebRequest request, final HttpServletResponse response) { + public ModelAndView handleInternal(final RuntimeException ex, final WebRequest request, final HttpServletResponse response) { logger.info(response.getHeader("x-ratelimit-remaining")); logger.error("500 Status Code", ex); - final String message = "Error Occurred: " + ex.getMessage(); - return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); + final String message = "Error Occurred: " + ex.getLocalizedMessage(); + final ModelAndView model = new ModelAndView("submissionResponse"); + model.addObject("msg", message); + return model; } } \ No newline at end of file