Skip to content

Commit

Permalink
代码扫描规范调整
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed Mar 14, 2019
1 parent 1c0de30 commit 5637435
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 125 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/wang/config/ExceptionAdvice.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private Map<String, Object> getValidError(List<FieldError> fieldErrors) {
StringBuffer errorMsg = new StringBuffer("校验异常(ValidException):");
for (FieldError error : fieldErrors) {
errorList.add(error.getField() + "-" + error.getDefaultMessage());
errorMsg.append(error.getField() + "-" + error.getDefaultMessage() + ".");
errorMsg.append(error.getField()).append("-").append(error.getDefaultMessage()).append(".");
}
result.put("errorList", errorList);
result.put("errorMsg", errorMsg);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/wang/config/shiro/ShiroConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ShiroConfig {
* @author Wang926454
* @date 2018/8/31 10:55
*/
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean("securityManager")
public DefaultWebSecurityManager getManager(UserRealm userRealm) {
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/wang/config/shiro/UserRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal
userDto.setAccount(account);
// 查询用户角色
List<RoleDto> roleDtos = roleMapper.findRoleByUser(userDto);
for (int i = 0, roleLen = roleDtos.size(); i < roleLen; i++) {
RoleDto roleDto = roleDtos.get(i);
for (RoleDto roleDto : roleDtos) {
// 添加角色
simpleAuthorizationInfo.addRole(roleDto.getName());
// 根据用户角色查询权限
List<PermissionDto> permissionDtos = permissionMapper.findPermissionByRole(roleDto);
for (int j = 0, perLen = permissionDtos.size(); j < perLen; j++) {
PermissionDto permissionDto = permissionDtos.get(j);
for (PermissionDto permissionDto : permissionDtos) {
// 添加权限
simpleAuthorizationInfo.addStringPermission(permissionDto.getPerCode());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/wang/config/shiro/cache/CustomCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public Object remove(Object key) throws CacheException {
*/
@Override
public void clear() throws CacheException {
JedisUtil.getJedis().flushDB();
Objects.requireNonNull(JedisUtil.getJedis()).flushDB();
}

/**
* 缓存的个数
*/
@Override
public int size() {
Long size = JedisUtil.getJedis().dbSize();
Long size = Objects.requireNonNull(JedisUtil.getJedis()).dbSize();
return size.intValue();
}

Expand All @@ -85,7 +85,7 @@ public int size() {
*/
@Override
public Set keys() {
Set<byte[]> keys = JedisUtil.getJedis().keys(new String("*").getBytes());
Set<byte[]> keys = Objects.requireNonNull(JedisUtil.getJedis()).keys("*".getBytes());
Set<Object> set = new HashSet<Object>();
for (byte[] bs : keys) {
set.add(SerializableUtil.unserializable(bs));
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/com/wang/config/shiro/jwt/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ protected boolean isAccessAllowed(ServletRequest request, ServletResponse respon
String msg = e.getMessage();
// 获取应用异常(该Cause是导致抛出此throwable(异常)的throwable(异常))
Throwable throwable = e.getCause();
if (throwable != null && throwable instanceof SignatureVerificationException) {
if (throwable instanceof SignatureVerificationException) {
// 该异常为JWT的AccessToken认证失败(Token或者密钥不正确)
msg = "Token或者密钥不正确(" + throwable.getMessage() + ")";
} else if (throwable != null && throwable instanceof TokenExpiredException) {
} else if (throwable instanceof TokenExpiredException) {
// 该异常为JWT的AccessToken已过期,判断RefreshToken未过期就进行AccessToken刷新
if (this.refreshToken(request, response)) {
return true;
Expand All @@ -71,11 +71,11 @@ protected boolean isAccessAllowed(ServletRequest request, ServletResponse respon
msg = throwable.getMessage();
}
}
/**
* 错误两种处理方式
* 1. 将非法请求转发到/401的Controller处理,抛出自定义无权访问异常被全局捕捉再返回Response信息
* 2. 无需转发,直接返回Response信息
* 一般使用第二种(更方便)
/*
错误两种处理方式
1. 将非法请求转发到/401的Controller处理,抛出自定义无权访问异常被全局捕捉再返回Response信息
2. 无需转发,直接返回Response信息
一般使用第二种(更方便)
*/
// 直接返回Response信息
this.response401(request, response, msg);
Expand Down Expand Up @@ -164,18 +164,12 @@ private void response401(ServletRequest req, ServletResponse resp, String msg) {
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json; charset=utf-8");
PrintWriter out = null;
try {
out = httpServletResponse.getWriter();
try (PrintWriter out = httpServletResponse.getWriter()) {
String data = JsonConvertUtil.objectToJson(new ResponseBean(HttpStatus.UNAUTHORIZED.value(), "无权访问(Unauthorized):" + msg, null));
out.append(data);
} catch (IOException e) {
LOGGER.error("直接返回Response信息出现IOException异常:" + e.getMessage());
throw new CustomException("直接返回Response信息出现IOException异常:" + e.getMessage());
} finally {
if (out != null) {
out.close();
}
}
}

Expand Down
20 changes: 4 additions & 16 deletions src/main/java/com/wang/util/AesCipherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,16 @@ public static String enCrypto(String str) {
byte[] cipherByte = c.doFinal(src);
// 先将二进制转换成16进制,再返回Base64加密后的String
return Base64ConvertUtil.encode(HexConvertUtil.parseByte2HexStr(cipherByte));
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
LOGGER.error("getInstance()方法异常:" + e.getMessage());
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
} catch (UnsupportedEncodingException e) {
LOGGER.error("Base64加密异常:" + e.getMessage());
throw new CustomUnauthorizedException("Base64加密异常:" + e.getMessage());
} catch (NoSuchPaddingException e) {
LOGGER.error("getInstance()方法异常:" + e.getMessage());
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
} catch (InvalidKeyException e) {
LOGGER.error("初始化Cipher对象异常:" + e.getMessage());
throw new CustomUnauthorizedException("初始化Cipher对象异常:" + e.getMessage());
} catch (IllegalBlockSizeException e) {
LOGGER.error("加密异常,密钥有误:" + e.getMessage());
throw new CustomUnauthorizedException("加密异常,密钥有误:" + e.getMessage());
} catch (BadPaddingException e) {
} catch (IllegalBlockSizeException | BadPaddingException e) {
LOGGER.error("加密异常,密钥有误:" + e.getMessage());
throw new CustomUnauthorizedException("加密异常,密钥有误:" + e.getMessage());
}
Expand Down Expand Up @@ -114,22 +108,16 @@ public static String deCrypto(String str) {
// 该字节数组负责保存加密的结果,先对str进行Base64解密,将16进制转换为二进制
byte[] cipherByte = c.doFinal(HexConvertUtil.parseHexStr2Byte(Base64ConvertUtil.decode(str)));
return new String(cipherByte);
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
LOGGER.error("getInstance()方法异常:" + e.getMessage());
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
} catch (UnsupportedEncodingException e) {
LOGGER.error("Base64加密异常:" + e.getMessage());
throw new CustomUnauthorizedException("Base64加密异常:" + e.getMessage());
} catch (NoSuchPaddingException e) {
LOGGER.error("getInstance()方法异常:" + e.getMessage());
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
} catch (InvalidKeyException e) {
LOGGER.error("初始化Cipher对象异常:" + e.getMessage());
throw new CustomUnauthorizedException("初始化Cipher对象异常:" + e.getMessage());
} catch (IllegalBlockSizeException e) {
LOGGER.error("解密异常,密钥有误:" + e.getMessage());
throw new CustomUnauthorizedException("解密异常,密钥有误:" + e.getMessage());
} catch (BadPaddingException e) {
} catch (IllegalBlockSizeException | BadPaddingException e) {
LOGGER.error("解密异常,密钥有误:" + e.getMessage());
throw new CustomUnauthorizedException("解密异常,密钥有误:" + e.getMessage());
}
Expand Down
95 changes: 14 additions & 81 deletions src/main/java/com/wang/util/JedisUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void setJedisPool(JedisPool jedisPool) {
public static synchronized Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
return jedisPool.getResource();
} else {
return null;
}
Expand Down Expand Up @@ -75,19 +74,13 @@ public static void closePool() {
* @date 2018/9/4 15:47
*/
public static Object getObject(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
byte[] bytes = jedis.get(key.getBytes());
if (StringUtil.isNotNull(bytes)) {
return SerializableUtil.unserializable(bytes);
}
} catch (Exception e) {
throw new CustomException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
return null;
}
Expand All @@ -101,16 +94,10 @@ public static Object getObject(String key) {
* @date 2018/9/4 15:49
*/
public static String setObject(String key, Object value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
} catch (Exception e) {
throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -124,21 +111,15 @@ public static String setObject(String key, Object value) {
* @date 2018/9/4 15:50
*/
public static String setObject(String key, Object value, int expiretime) {
String result = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String result;
try (Jedis jedis = jedisPool.getResource()) {
result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));
if (Constant.OK.equals(result)) {
jedis.expire(key.getBytes(), expiretime);
}
return result;
} catch (Exception e) {
throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -150,16 +131,10 @@ public static String setObject(String key, Object value, int expiretime) {
* @date 2018/9/4 15:47
*/
public static String getJson(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.get(key);
} catch (Exception e) {
throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -172,16 +147,10 @@ public static String getJson(String key) {
* @date 2018/9/4 15:49
*/
public static String setJson(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.set(key, value);
} catch (Exception e) {
throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -195,21 +164,15 @@ public static String setJson(String key, String value) {
* @date 2018/9/4 15:50
*/
public static String setJson(String key, String value, int expiretime) {
String result = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String result;
try (Jedis jedis = jedisPool.getResource()) {
result = jedis.set(key, value);
if (Constant.OK.equals(result)) {
jedis.expire(key, expiretime);
}
return result;
} catch (Exception e) {
throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -221,16 +184,10 @@ public static String setJson(String key, String value, int expiretime) {
* @date 2018/9/4 15:50
*/
public static Long delKey(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.del(key.getBytes());
} catch (Exception e) {
throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -242,16 +199,10 @@ public static Long delKey(String key) {
* @date 2018/9/4 15:51
*/
public static Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.exists(key.getBytes());
} catch (Exception e) {
throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -263,16 +214,10 @@ public static Boolean exists(String key) {
* @date 2018/9/6 9:43
*/
public static Set<String> keysS(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.keys(key);
} catch (Exception e) {
throw new CustomException("模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -284,16 +229,10 @@ public static Set<String> keysS(String key) {
* @date 2018/9/6 9:43
*/
public static Set<byte[]> keysB(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
return jedis.keys(key.getBytes());
} catch (Exception e) {
throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}

Expand All @@ -306,17 +245,11 @@ public static Set<byte[]> keysB(String key) {
*/
public static Long ttl(String key) {
Long result = -2L;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
try (Jedis jedis = jedisPool.getResource()) {
result = jedis.ttl(key);
return result;
} catch (Exception e) {
throw new CustomException("获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}
Loading

0 comments on commit 5637435

Please sign in to comment.