SM3PasswordEncoder.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package weaver.authentication.handler.encoder;
import org.jasig.cas.authentication.handler.PasswordEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import weaver.loginsso.AES;
import javax.sql.DataSource;
import java.util.Map;
/**
* Created by crazyDream on 2018/9/17.
*/
public class SM3PasswordEncoder implements PasswordEncoder {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public String encode(String password) {
return null;
}
@Override
public String encode(String username, String password) {
/*String yjcust = null;
try {
yjcust = AES.decrypt(password, "yjcust");
} catch (Exception e) {
e.printStackTrace();
}
if (yjcust != null && yjcust.indexOf("-f-g-") > 0) {
password=yjcust.substring(0,yjcust.indexOf("-f-g-"));
}else if (password.indexOf("-h-i-")>0) {
password = password.substring(0, password.indexOf("-h-i-"));
}*/
//取salt
Map map = getJdbcTemplate().queryForMap(this.sql, username);
String salt = (String) map.get("salt");
if (salt != null && salt.indexOf("sm3_new#") >= 0) {
salt = salt.substring(8, salt.length());
}
SM3 sm3 = new SM3();
String encrypt = sm3.getEncrypt(password, salt);
return encrypt;
}
@Override
public String encode(String username, String password, Map<String, Object> otherParams) {
return null;
}
private String sql;
private SimpleJdbcTemplate jdbcTemplate;
private DataSource dataSource;
public void setSql(final String sql) {
this.sql = sql;
}
public final void setDataSource(final DataSource dataSource) {
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.dataSource = dataSource;
}
private final SimpleJdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
private final DataSource getDataSource() {
return this.dataSource;
}
}