Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<dependency>
<groupId>com.originaldreams</groupId>
<artifactId>common</artifactId>
<version>0.0.11</version>
<version>0.0.12</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.originaldreams.common.util.StringUtils;
import com.originaldreams.common.util.ValidUserName;
import com.originaldreams.proxycenter.cache.CacheUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class HttpController {
*/
private final static String SPLIT_KEY_VALUE = ":";

private static final String NULL_STRING = "null";

private static final String API_PREFIX = "/api";

/**
Expand Down Expand Up @@ -430,14 +433,20 @@ private ResponseEntity getResponseFromException(HttpClientErrorException excepti
* @param response
*/
private void setCacheForLogon(ResponseEntity<String> response){
String result = response.getBody();

try{
String result = response.getBody();

JSONObject json = new JSONObject(result);
int success = json.getInt("success");
//登录不成功,不记录session
if(success != 0 ){
return;
}
Object data = json.get("data");
if (NULL_STRING.equals(data.toString())) {
return;
}
int userId = json.getInt("data");
logger.info("logonWithUserName userId:" + userId);
//将userId放入Session
Expand Down Expand Up @@ -467,9 +476,9 @@ private void setCacheForLogon(ResponseEntity<String> response){
//角色名放入缓存
CacheUtils.userRoleMap.put(userId,roleName);
logger.info("logonWithUserName roleName :" + roleName + ", routerIds:" + routerIds);
}catch (Exception e){
}catch (JSONException e){
e.printStackTrace();
logger.error("setCacheForLogon" + e.getMessage());
logger.error("setCacheForLogon {}", e.getMessage());

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.originaldreams.proxycenter.exception;


import com.originaldreams.common.exception.BadRequestException;
import com.originaldreams.common.exception.ResourceNotFoundException;
import com.originaldreams.common.response.MyServiceResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
* 自定义异常
* @author magaofei
*/
@ControllerAdvice
@RestController
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

private static final String NOT_FOUND_MESSAGE = "你访问的资源不存在";
private static final String BAD_REQUEST_MESSAGE = "参数错误";

private static ResponseEntity<Object> buildBadRequestEntity(Exception ex, WebRequest request) {
String message = ex.getMessage();
if (message == null) {
message = BAD_REQUEST_MESSAGE;
}

return new ResponseEntity<>(
new MyServiceResponse(MyServiceResponse.SUCCESS_CODE_FAILED, message),
HttpStatus.BAD_REQUEST);
}

@Override
protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return buildBadRequestEntity(ex, request);
}


@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return buildBadRequestEntity(ex, request);
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return buildBadRequestEntity(ex, request);
}

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return buildBadRequestEntity(ex,request);
}

@Override
protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return buildBadRequestEntity(ex, request);
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handleNotFoundException(ResourceNotFoundException ex, WebRequest request) {

String message = ex.getMessage();
if (message == null) {
message = NOT_FOUND_MESSAGE;
}

return new ResponseEntity<>(
new MyServiceResponse(MyServiceResponse.SUCCESS_CODE_FAILED, message),
HttpStatus.NOT_FOUND);
}

@ExceptionHandler({BadRequestException.class, MethodArgumentTypeMismatchException.class})
public ResponseEntity<Object> handleBadRequestException(Exception ex, WebRequest request) {
return buildBadRequestEntity(ex, request);
}





@ExceptionHandler(Exception.class)
public ResponseEntity<MyServiceResponse> handleAllExceptions(Exception ex, WebRequest request) {

MyServiceResponse errorDetails = new MyServiceResponse(
MyServiceResponse.SUCCESS_CODE_FAILED,
ex.getMessage());
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}


}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package com.originaldreams.proxycenter;

import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.Map;

public class ProxycenterApplicationTests {

@Test
public void test() throws Exception{
String a = "{\"success\":0,\"data\":null,\"message\":null}";
JSONObject json = new JSONObject(a);
json.getJSONObject("data");
System.out.println(json.getJSONObject("data"));
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map;
try {
map = mapper.readValue(a, Map.class);
} catch (IOException e) {
e.printStackTrace();
return;
}
System.out.println(map.get("data"));


}


Expand Down