Skip to content

Commit e26eab8

Browse files
committedMay 30, 2018
#398 code refactor
1 parent 3366087 commit e26eab8

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed
 

‎saturn-console-api/src/main/java/com/vip/saturn/job/console/controller/gui/AuthenticationController.java

-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public SuccessResponseEntity login(@RequestParam String username, @RequestParam
3131
HttpServletRequest request) throws SaturnJobConsoleException {
3232

3333
User user = authenticationService.authenticate(username, password);
34-
if (user == null) {
35-
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
36-
}
3734

3835
request.getSession().setAttribute(SessionAttributeKeys.LOGIN_USER_NAME, user.getUserName());
3936
request.getSession().setAttribute(SessionAttributeKeys.LOGIN_USER_REAL_NAME, user.getRealName());

‎saturn-console-api/src/main/java/com/vip/saturn/job/console/service/impl/AuthenticationServiceImpl.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.vip.saturn.job.console.utils.PasswordUtils;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.transaction.annotation.Transactional;
1011
import org.springframework.util.StringUtils;
1112

1213
public class AuthenticationServiceImpl implements AuthenticationService {
@@ -17,18 +18,21 @@ public class AuthenticationServiceImpl implements AuthenticationService {
1718
@Value("${authentication.hash:plaintext}")
1819
private String hashMethod;
1920

21+
@Transactional(readOnly = true)
2022
@Override
2123
public User authenticate(String username, String password) throws SaturnJobConsoleException {
2224
if (StringUtils.isEmpty(password)) {
23-
return null;
25+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "密码不能为空");
2426
}
2527

2628
User user = userRepository.select(username);
2729
if (user == null) {
28-
return null;
30+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
2931
}
3032

31-
return PasswordUtils.validate(password, user.getPassword(), hashMethod) ? user : null;
33+
PasswordUtils.validate(password, user.getPassword(), hashMethod);
34+
35+
return user;
3236
}
3337

3438
public void setHashMethod(String hashMethod) {

‎saturn-console-api/src/main/java/com/vip/saturn/job/console/utils/PasswordUtils.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,35 @@ public static String hash(String password, byte[] salt) throws NoSuchAlgorithmEx
5353
return Hex.encodeHexString(key.getEncoded());
5454
}
5555

56-
public static boolean validate(String password, String passwordInDB, String hashMethod)
56+
public static void validate(String password, String passwordInDB, String hashMethod)
5757
throws SaturnJobConsoleException {
5858
if (!isHashMethodSupported(hashMethod)) {
5959
throw new SaturnJobConsoleException(String.format("hash method [%s] is not supported", hashMethod));
6060
}
6161

6262
if (PasswordUtils.HASH_METHOD_PLANTEXT.equals(hashMethod)) {
63-
return password.equals(passwordInDB);
63+
if (!password.equals(passwordInDB)) {
64+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
65+
}
66+
return;
6467
}
6568

6669
String[] saltAndPassword = passwordInDB.split("\\$");
6770
if (saltAndPassword.length != 2) {
6871
log.debug("malformed password in db");
69-
return false;
72+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
7073
}
7174

7275
String hashOfRequestPassword;
7376
try {
7477
hashOfRequestPassword = hash(password, getSalt(saltAndPassword[1]));
7578
} catch (Exception e) {
76-
return false;
79+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
80+
}
81+
82+
if (!hashOfRequestPassword.equals(new String(saltAndPassword[0]))) {
83+
throw new SaturnJobConsoleException(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, "用户名或密码不正确");
7784
}
78-
return hashOfRequestPassword.equals(new String(saltAndPassword[0]));
7985
}
8086

8187
public static boolean isHashMethodSupported(String hashMethod) {

‎saturn-console-api/src/test/java/com/vip/saturn/job/console/utils/PasswordUtilsTest.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vip.saturn.job.console.utils;
22

3+
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
34
import org.junit.Test;
45

56
import static org.junit.Assert.*;
@@ -16,15 +17,36 @@ public void testGenSaltedPassword() throws Exception {
1617
public void testValidate() throws Exception {
1718
String passwordInDB = "a2c2646186828474b754591a547c18f132d88d744c152655a470161a1a052135$73616c74";
1819

19-
assertTrue(PasswordUtils.validate("password", passwordInDB, "PBKDF2WithHmacSHA1"));
20-
assertFalse(PasswordUtils.validate("password1", passwordInDB, "PBKDF2WithHmacSHA1"));
21-
assertTrue(PasswordUtils.validate("password", "password", "plaintext"));
22-
assertFalse(PasswordUtils.validate("password1", "password", "plaintext"));
20+
PasswordUtils.validate("password", passwordInDB, "PBKDF2WithHmacSHA1");
21+
PasswordUtils.validate("password", "password", "plaintext");
22+
23+
int count = 0;
24+
try {
25+
PasswordUtils.validate("password1", passwordInDB, "PBKDF2WithHmacSHA1");
26+
} catch (SaturnJobConsoleException e) {
27+
count++;
28+
assertEquals(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, e.getErrorCode());
29+
}
30+
try {
31+
PasswordUtils.validate("password1", "password", "plaintext");
32+
} catch (SaturnJobConsoleException e) {
33+
count++;
34+
assertEquals(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, e.getErrorCode());
35+
}
36+
37+
assertEquals(2, count);
2338
}
2439

2540
@Test
2641
public void testValidateWherePasswordInDBisMalfomred() throws Exception {
2742
int count = 0;
28-
assertFalse(PasswordUtils.validate("password", "password", "PBKDF2WithHmacSHA1"));
43+
try {
44+
PasswordUtils.validate("password", "password", "PBKDF2WithHmacSHA1");
45+
} catch (SaturnJobConsoleException e) {
46+
count++;
47+
assertEquals(SaturnJobConsoleException.ERROR_CODE_AUTHN_FAIL, e.getErrorCode());
48+
}
49+
50+
assertEquals(1, count);
2951
}
3052
}

0 commit comments

Comments
 (0)
Please sign in to comment.