Commit 4b9fb717 authored by 杨伊博's avatar 杨伊博

MD5 encryption is ok

parent adb725fb
...@@ -50,6 +50,7 @@ public class WebSocketController { ...@@ -50,6 +50,7 @@ public class WebSocketController {
/** /**
* 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。 * 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。
* 通过当前用户,然后查找消息,如果查找到未读消息,则发送给当前用户。
*/ */
if (principal.getName().equals("wyf")) { if (principal.getName().equals("wyf")) {
//通过convertAndSendToUser 向用户发送信息, //通过convertAndSendToUser 向用户发送信息,
......
package com.us.example.config; package com.us.example.config;
import com.us.example.security.CustomUserService; import com.us.example.security.CustomUserService;
import com.us.example.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/** /**
* Created by yangyibo on 17/1/18. * Created by yangyibo on 17/1/18.
*/ */
...@@ -20,8 +25,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -20,8 +25,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService()); //user Details Service验证 auth.userDetailsService(customUserService()).passwordEncoder(new PasswordEncoder(){
@Override
public String encode(CharSequence rawPassword) {
return MD5Util.encode((String)rawPassword);
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encodedPassword.equals(MD5Util.encode((String)rawPassword));
}}); //user Details Service验证
} }
@Override @Override
...@@ -38,5 +52,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -38,5 +52,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
} }
} }
package com.us.example.util;
/**
* Created by yangyibo on 17/2/7.
*/
import java.security.MessageDigest;
/**
* MD5加密工具
*
*/
public class MD5Util {
private static final String SALT = "tamboo";
public static String encode(String password) {
password = password + SALT;
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
throw new RuntimeException(e);
}
char[] charArray = password.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static void main(String[] args) {
System.out.println(MD5Util.encode("abel"));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment