Skip to content

Commit 3543c26

Browse files
committed
修改代码规范,if for try空格添加,代码更美观
1 parent 00ce41a commit 3543c26

File tree

13 files changed

+89
-89
lines changed

13 files changed

+89
-89
lines changed

src/main/java/com/wang/config/ExceptionAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ private HttpStatus getStatus(HttpServletRequest request) {
147147
* @param fieldErrors
148148
* @return
149149
*/
150-
private Map<String, Object> getValidError(List<FieldError> fieldErrors){
150+
private Map<String, Object> getValidError(List<FieldError> fieldErrors) {
151151
Map<String, Object> result = new HashMap<String, Object>(16);
152152
List<String> errorList = new ArrayList<String>();
153153
StringBuffer errorMsg = new StringBuffer("校验异常(ValidException):");
154-
for (FieldError error : fieldErrors){
154+
for (FieldError error : fieldErrors) {
155155
errorList.add(error.getField() + "-" + error.getDefaultMessage());
156156
errorMsg.append(error.getField() + "-" + error.getDefaultMessage() + ".");
157157
}

src/main/java/com/wang/config/redis/JedisConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class JedisConfig {
4949
private int minIdle;
5050

5151
@Bean
52-
public JedisPool redisPoolFactory(){
52+
public JedisPool redisPoolFactory() {
5353
try {
5454
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
5555
jedisPoolConfig.setMaxIdle(maxIdle);

src/main/java/com/wang/config/shiro/UserRealm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) t
9797
throw new AuthenticationException("该帐号不存在(The account does not exist.)");
9898
}
9999
// 开始认证,要AccessToken认证通过,且Redis中存在RefreshToken,且两个Token时间戳一致
100-
if(JwtUtil.verify(token) && JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)){
100+
if (JwtUtil.verify(token) && JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)) {
101101
// 获取RefreshToken的时间戳
102102
String currentTimeMillisRedis = JedisUtil.getObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account).toString();
103103
// 获取AccessToken时间戳,与RefreshToken的时间戳对比
104-
if(JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)){
104+
if (JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)) {
105105
return new SimpleAuthenticationInfo(token, token, "userRealm");
106106
}
107107
}

src/main/java/com/wang/config/shiro/cache/CustomCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class CustomCache<K,V> implements Cache<K,V> {
2424
* @author Wang926454
2525
* @date 2018/9/4 18:33
2626
*/
27-
private String getKey(Object key){
27+
private String getKey(Object key) {
2828
return Constant.PREFIX_SHIRO_CACHE + JwtUtil.getClaim(key.toString(), Constant.ACCOUNT);
2929
}
3030

src/main/java/com/wang/config/shiro/jwt/JwtFilter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ protected boolean isAccessAllowed(ServletRequest request, ServletResponse respon
5454
String msg = e.getMessage();
5555
// 获取应用异常(该Cause是导致抛出此throwable(异常)的throwable(异常))
5656
Throwable throwable = e.getCause();
57-
if(throwable != null && throwable instanceof SignatureVerificationException ){
57+
if (throwable != null && throwable instanceof SignatureVerificationException) {
5858
// 该异常为JWT的AccessToken认证失败(Token或者密钥不正确)
5959
msg = "Token或者密钥不正确(" + throwable.getMessage() + ")";
60-
} else if(throwable != null && throwable instanceof TokenExpiredException){
60+
} else if (throwable != null && throwable instanceof TokenExpiredException) {
6161
// 该异常为JWT的AccessToken已过期,判断RefreshToken未过期就进行AccessToken刷新
62-
if(this.refreshToken(request, response)){
62+
if (this.refreshToken(request, response)) {
6363
return true;
64-
}else{
64+
} else {
6565
msg = "Token已过期(" + throwable.getMessage() + ")";
6666
}
67-
} else{
67+
} else {
6868
// 应用异常不为空
69-
if(throwable != null) {
69+
if (throwable != null) {
7070
// 获取应用异常msg
7171
msg = throwable.getMessage();
7272
}
@@ -128,11 +128,11 @@ private boolean refreshToken(ServletRequest request, ServletResponse response) {
128128
// 获取当前Token的帐号信息
129129
String account = JwtUtil.getClaim(token, Constant.ACCOUNT);
130130
// 判断Redis中RefreshToken是否存在
131-
if(JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)){
131+
if (JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account)) {
132132
// Redis中RefreshToken还存在,获取RefreshToken的时间戳
133133
String currentTimeMillisRedis = JedisUtil.getObject(Constant.PREFIX_SHIRO_REFRESH_TOKEN + account).toString();
134134
// 获取当前AccessToken中的时间戳,与RefreshToken的时间戳对比,如果当前时间戳一致,进行AccessToken刷新
135-
if(JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)){
135+
if (JwtUtil.getClaim(token, Constant.CURRENT_TIME_MILLIS).equals(currentTimeMillisRedis)) {
136136
// 获取当前最新时间戳
137137
String currentTimeMillis = String.valueOf(System.currentTimeMillis());
138138
// 读取配置文件,获取refreshTokenExpireTime属性

src/main/java/com/wang/controller/UserController.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public UserController(IUserService userService) {
6262
*/
6363
@GetMapping
6464
@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
65-
public ResponseBean user(@Validated BaseDto baseDto){
66-
if(baseDto.getPage() == null || baseDto.getRows() == null){
65+
public ResponseBean user(@Validated BaseDto baseDto) {
66+
if (baseDto.getPage() == null || baseDto.getRows() == null) {
6767
baseDto.setPage(1);
6868
baseDto.setRows(10);
6969
}
7070
PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
7171
List<UserDto> userDtos = userService.selectAll();
7272
PageInfo<UserDto> selectPage = new PageInfo<UserDto>(userDtos);
73-
if(userDtos == null || userDtos.size() <= 0){
73+
if (userDtos == null || userDtos.size() <= 0) {
7474
throw new CustomException("查询失败(Query Failure)");
7575
}
7676
Map<String, Object> result = new HashMap<String, Object>(16);
@@ -88,12 +88,12 @@ public ResponseBean user(@Validated BaseDto baseDto){
8888
*/
8989
@GetMapping("/online")
9090
@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
91-
public ResponseBean online(){
91+
public ResponseBean online() {
9292
List<Object> userDtos = new ArrayList<Object>();
9393
// 查询所有Redis键
9494
Set<String> keys = JedisUtil.keysS(Constant.PREFIX_SHIRO_REFRESH_TOKEN + "*");
9595
for (String key : keys) {
96-
if(JedisUtil.exists(key)){
96+
if (JedisUtil.exists(key)) {
9797
// 根据:分割key,获取最后一个字符(帐号)
9898
String[] strArray = key.split(":");
9999
UserDto userDto = new UserDto();
@@ -104,7 +104,7 @@ public ResponseBean online(){
104104
userDtos.add(userDto);
105105
}
106106
}
107-
if(userDtos == null || userDtos.size() <= 0){
107+
if (userDtos == null || userDtos.size() <= 0) {
108108
throw new CustomException("查询失败(Query Failure)");
109109
}
110110
return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", userDtos);
@@ -123,15 +123,15 @@ public ResponseBean login(@Validated(UserLoginValidGroup.class) @RequestBody Use
123123
UserDto userDtoTemp = new UserDto();
124124
userDtoTemp.setAccount(userDto.getAccount());
125125
userDtoTemp = userService.selectOne(userDtoTemp);
126-
if(userDtoTemp == null){
126+
if (userDtoTemp == null) {
127127
throw new CustomUnauthorizedException("该帐号不存在(The account does not exist.)");
128128
}
129129
// 密码进行AES解密
130130
String key = AesCipherUtil.deCrypto(userDtoTemp.getPassword());
131131
// 因为密码加密是以帐号+密码的形式进行加密的,所以解密后的对比是帐号+密码
132132
if (key.equals(userDto.getAccount() + userDto.getPassword())) {
133133
// 清除可能存在的Shiro权限信息缓存
134-
if(JedisUtil.exists(Constant.PREFIX_SHIRO_CACHE + userDto.getAccount())){
134+
if (JedisUtil.exists(Constant.PREFIX_SHIRO_CACHE + userDto.getAccount())) {
135135
JedisUtil.delKey(Constant.PREFIX_SHIRO_CACHE + userDto.getAccount());
136136
}
137137
// 设置RefreshToken,时间戳为当前时间戳,直接设置即可(不用先删后设,会覆盖已有的RefreshToken)
@@ -187,9 +187,9 @@ public ResponseBean requireAuth() {
187187
*/
188188
@GetMapping("/{id}")
189189
@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
190-
public ResponseBean findById(@PathVariable("id") Integer id){
190+
public ResponseBean findById(@PathVariable("id") Integer id) {
191191
UserDto userDto = userService.selectByPrimaryKey(id);
192-
if(userDto == null){
192+
if (userDto == null) {
193193
throw new CustomException("查询失败(Query Failure)");
194194
}
195195
return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", userDto);
@@ -209,18 +209,18 @@ public ResponseBean add(@Validated(UserEditValidGroup.class) @RequestBody UserDt
209209
UserDto userDtoTemp = new UserDto();
210210
userDtoTemp.setAccount(userDto.getAccount());
211211
userDtoTemp = userService.selectOne(userDtoTemp);
212-
if(userDtoTemp != null && StringUtil.isNotBlank(userDtoTemp.getPassword())){
212+
if (userDtoTemp != null && StringUtil.isNotBlank(userDtoTemp.getPassword())) {
213213
throw new CustomUnauthorizedException("该帐号已存在(Account exist.)");
214214
}
215215
userDto.setRegTime(new Date());
216216
// 密码以帐号+密码的形式进行AES加密
217-
if(userDto.getPassword().length() > Constant.PASSWORD_MAX_LEN){
217+
if (userDto.getPassword().length() > Constant.PASSWORD_MAX_LEN) {
218218
throw new CustomException("密码最多8位(Password up to 8 bits.)");
219219
}
220220
String key = AesCipherUtil.enCrypto(userDto.getAccount() + userDto.getPassword());
221221
userDto.setPassword(key);
222222
int count = userService.insert(userDto);
223-
if(count <= 0){
223+
if (count <= 0) {
224224
throw new CustomException("新增失败(Insert Failure)");
225225
}
226226
return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", userDto);
@@ -240,22 +240,22 @@ public ResponseBean update(@Validated(UserEditValidGroup.class) @RequestBody Use
240240
UserDto userDtoTemp = new UserDto();
241241
userDtoTemp.setAccount(userDto.getAccount());
242242
userDtoTemp = userService.selectOne(userDtoTemp);
243-
if(userDtoTemp == null){
243+
if (userDtoTemp == null) {
244244
throw new CustomUnauthorizedException("该帐号不存在(Account not exist.)");
245-
}else{
245+
} else {
246246
userDto.setId(userDtoTemp.getId());
247247
}
248248
// FIXME: 如果不一样就说明用户修改了密码,重新加密密码(这个处理不太好,但是没有想到好的处理方式)
249-
if(!userDtoTemp.getPassword().equals(userDto.getPassword())){
249+
if (!userDtoTemp.getPassword().equals(userDto.getPassword())) {
250250
// 密码以帐号+密码的形式进行AES加密
251-
if(userDto.getPassword().length() > Constant.PASSWORD_MAX_LEN){
251+
if (userDto.getPassword().length() > Constant.PASSWORD_MAX_LEN) {
252252
throw new CustomException("密码最多8位(Password up to 8 bits.)");
253253
}
254254
String key = AesCipherUtil.enCrypto(userDto.getAccount() + userDto.getPassword());
255255
userDto.setPassword(key);
256256
}
257257
int count = userService.updateByPrimaryKeySelective(userDto);
258-
if(count <= 0){
258+
if (count <= 0) {
259259
throw new CustomException("更新失败(Update Failure)");
260260
}
261261
return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", userDto);
@@ -270,9 +270,9 @@ public ResponseBean update(@Validated(UserEditValidGroup.class) @RequestBody Use
270270
*/
271271
@DeleteMapping("/{id}")
272272
@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
273-
public ResponseBean delete(@PathVariable("id") Integer id){
273+
public ResponseBean delete(@PathVariable("id") Integer id) {
274274
int count = userService.deleteByPrimaryKey(id);
275-
if(count <= 0){
275+
if (count <= 0) {
276276
throw new CustomException("删除失败,ID不存在(Deletion Failed. ID does not exist.)");
277277
}
278278
return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
@@ -287,10 +287,10 @@ public ResponseBean delete(@PathVariable("id") Integer id){
287287
*/
288288
@DeleteMapping("/online/{id}")
289289
@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
290-
public ResponseBean deleteOnline(@PathVariable("id") Integer id){
290+
public ResponseBean deleteOnline(@PathVariable("id") Integer id) {
291291
UserDto userDto = userService.selectByPrimaryKey(id);
292-
if(JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + userDto.getAccount())){
293-
if(JedisUtil.delKey(Constant.PREFIX_SHIRO_REFRESH_TOKEN + userDto.getAccount()) > 0){
292+
if (JedisUtil.exists(Constant.PREFIX_SHIRO_REFRESH_TOKEN + userDto.getAccount())) {
293+
if (JedisUtil.delKey(Constant.PREFIX_SHIRO_REFRESH_TOKEN + userDto.getAccount()) > 0) {
294294
return new ResponseBean(HttpStatus.OK.value(), "剔除成功(Delete Success)", null);
295295
}
296296
}

src/main/java/com/wang/service/IUserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
* @author Wang926454
88
* @date 2018/8/9 15:44
99
*/
10-
public interface IUserService extends IBaseService<UserDto>{
10+
public interface IUserService extends IBaseService<UserDto> {
1111
}

src/main/java/com/wang/util/AesCipherUtil.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void setEncryptAESKey(String encryptAESKey) {
4747
* @date 2018/8/31 16:56
4848
*/
4949
public static String enCrypto(String str) {
50-
try{
50+
try {
5151
Security.addProvider(new com.sun.crypto.provider.SunJCE());
5252
// 实例化支持AES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
5353
// KeyGenerator 提供对称密钥生成器的功能,支持各种算法
@@ -67,22 +67,22 @@ public static String enCrypto(String str) {
6767
byte[] cipherByte = c.doFinal(src);
6868
// 先将二进制转换成16进制,再返回Bsae64加密后的String
6969
return Base64ConvertUtil.encode(HexConvertUtil.parseByte2HexStr(cipherByte));
70-
} catch (NoSuchAlgorithmException e){
70+
} catch (NoSuchAlgorithmException e) {
7171
LOGGER.error("getInstance()方法异常:" + e.getMessage());
7272
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
73-
} catch (UnsupportedEncodingException e){
73+
} catch (UnsupportedEncodingException e) {
7474
LOGGER.error("Bsae64加密异常:" + e.getMessage());
7575
throw new CustomUnauthorizedException("Bsae64加密异常:" + e.getMessage());
76-
} catch (NoSuchPaddingException e){
76+
} catch (NoSuchPaddingException e) {
7777
LOGGER.error("getInstance()方法异常:" + e.getMessage());
7878
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
79-
} catch (InvalidKeyException e){
79+
} catch (InvalidKeyException e) {
8080
LOGGER.error("初始化Cipher对象异常:" + e.getMessage());
8181
throw new CustomUnauthorizedException("初始化Cipher对象异常:" + e.getMessage());
82-
} catch (IllegalBlockSizeException e){
82+
} catch (IllegalBlockSizeException e) {
8383
LOGGER.error("加密异常,密钥有误:" + e.getMessage());
8484
throw new CustomUnauthorizedException("加密异常,密钥有误:" + e.getMessage());
85-
} catch (BadPaddingException e){
85+
} catch (BadPaddingException e) {
8686
LOGGER.error("加密异常,密钥有误:" + e.getMessage());
8787
throw new CustomUnauthorizedException("加密异常,密钥有误:" + e.getMessage());
8888
}
@@ -96,7 +96,7 @@ public static String enCrypto(String str) {
9696
* @date 2018/8/31 16:56
9797
*/
9898
public static String deCrypto(String str) {
99-
try{
99+
try {
100100
Security.addProvider(new com.sun.crypto.provider.SunJCE());
101101
// 实例化支持AES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
102102
// KeyGenerator 提供对称密钥生成器的功能,支持各种算法
@@ -114,22 +114,22 @@ public static String deCrypto(String str) {
114114
// 该字节数组负责保存加密的结果,先对str进行Bsae64解密,将16进制转换为二进制
115115
byte[] cipherByte = c.doFinal(HexConvertUtil.parseHexStr2Byte(Base64ConvertUtil.decode(str)));
116116
return new String(cipherByte);
117-
} catch (NoSuchAlgorithmException e){
117+
} catch (NoSuchAlgorithmException e) {
118118
LOGGER.error("getInstance()方法异常:" + e.getMessage());
119119
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
120-
} catch (UnsupportedEncodingException e){
120+
} catch (UnsupportedEncodingException e) {
121121
LOGGER.error("Bsae64加密异常:" + e.getMessage());
122122
throw new CustomUnauthorizedException("Bsae64加密异常:" + e.getMessage());
123-
} catch (NoSuchPaddingException e){
123+
} catch (NoSuchPaddingException e) {
124124
LOGGER.error("getInstance()方法异常:" + e.getMessage());
125125
throw new CustomUnauthorizedException("getInstance()方法异常:" + e.getMessage());
126-
} catch (InvalidKeyException e){
126+
} catch (InvalidKeyException e) {
127127
LOGGER.error("初始化Cipher对象异常:" + e.getMessage());
128128
throw new CustomUnauthorizedException("初始化Cipher对象异常:" + e.getMessage());
129-
} catch (IllegalBlockSizeException e){
129+
} catch (IllegalBlockSizeException e) {
130130
LOGGER.error("解密异常,密钥有误:" + e.getMessage());
131131
throw new CustomUnauthorizedException("解密异常,密钥有误:" + e.getMessage());
132-
} catch (BadPaddingException e){
132+
} catch (BadPaddingException e) {
133133
LOGGER.error("解密异常,密钥有误:" + e.getMessage());
134134
throw new CustomUnauthorizedException("解密异常,密钥有误:" + e.getMessage());
135135
}

0 commit comments

Comments
 (0)