File tree Expand file tree Collapse file tree
springboot-springSecurity2/src/main/java/com/us/example Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import com .us .example .dao .UserDao ;
44import com .us .example .domain .SysUser ;
5+ import com .us .example .util .MD5Util ;
56import org .springframework .beans .factory .annotation .Autowired ;
67import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
78import org .springframework .stereotype .Service ;
@@ -17,7 +18,7 @@ public class UserService {
1718 public SysUser create (SysUser sysUser ){
1819 //进行加密
1920 BCryptPasswordEncoder encoder =new BCryptPasswordEncoder ();
20- sysUser .setPassword (encoder .encode (sysUser .getRawPassword ().trim ()));
21+ sysUser .setPassword (encoder .encode (MD5Util . encode ( sysUser .getRawPassword ().trim () )));
2122 userDao .create (sysUser );
2223 return sysUser ;
2324 }
Original file line number Diff line number Diff line change @@ -9,7 +9,21 @@ public class BCryptPasswordEncoderTest {
99 public static void main (String [] args ) {
1010
1111 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
12- System .out .println (encoder .encode ("abel" ));
13- System .out .println (encoder .encode ("admin" ));
12+ System .out .println ("encoder: " + encoder .encode ("abel" ));
13+ System .out .println ("encoder: " + encoder .encode ("admin" ));
14+
15+ if (encoder .matches ("abel" , "$2a$10$IAz6WzJ314LH1NXq7Rf.dOYPP2uvzk08g.eAl9l4DRG4YsxavEV4W" )) {
16+ System .out .println ("encoder: true" );
17+ }
18+
19+
20+ System .out .println ("------------华丽的分割线-----------------------" );
21+ String Md5Password = MD5Util .encode ("abel" );
22+ System .out .println ("Md5Password: " + Md5Password );
23+ System .out .println ("encoder: " + encoder .encode (Md5Password ));
24+ if (encoder .matches (Md5Password , "$2a$10$37MXEfzlbtC6QSsRTlRhIOmykMRJtO5mU8Y.yiJBjy1x4WYWFR5gG" )) {
25+ System .out .println ("Md5Password: true" );
26+ }
27+
1428 }
1529}
Original file line number Diff line number Diff line change 1+ package com .us .example .util ;
2+
3+ import java .security .MessageDigest ;
4+
5+ /**
6+ * Created by yangyibo on 9/8/17.
7+ */
8+ public class MD5Util {
9+ private static final String SALT = "salt" ;
10+
11+ public static String encode (String password ) {
12+ password = password + SALT ;
13+ return processEncode (password );
14+ }
15+
16+ public static String processEncode (String password ) {
17+ MessageDigest md5 = null ;
18+ try {
19+ md5 = MessageDigest .getInstance ("MD5" );
20+ } catch (Exception e ) {
21+ throw new RuntimeException (e );
22+ }
23+ char [] charArray = password .toCharArray ();
24+ byte [] byteArray = new byte [charArray .length ];
25+
26+ for (int i = 0 ; i < charArray .length ; i ++)
27+ byteArray [i ] = (byte ) charArray [i ];
28+ byte [] md5Bytes = md5 .digest (byteArray );
29+ StringBuffer hexValue = new StringBuffer ();
30+ for (int i = 0 ; i < md5Bytes .length ; i ++) {
31+ int val = ((int ) md5Bytes [i ]) & 0xff ;
32+ if (val < 16 ) {
33+ hexValue .append ("0" );
34+ }
35+
36+ hexValue .append (Integer .toHexString (val ));
37+ }
38+ return hexValue .toString ();
39+ }
40+
41+ public static void main (String [] args ) {
42+ System .out .println (MD5Util .encode ("abel" ));
43+ System .out .println (MD5Util .encode ("admin" ));
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments