MD5 만나기
md5는 데이터 보안을 보호하는 암호화 알고리즘입니다 (안전하지 않은 것으로 입증되었습니다). md5 암호화 알고리즘
솔트 는 shiro에서 제공되므로 솔트 를 추가해야합니다. 솔트 를 추가하는 방법 은 데이터 보안을 더욱 향상시킬 수 있습니다. 데이터베이스의 users 테이블에서 디자인 할 때 id, username 및 password 외에도 password_salt 목록이 있습니다.
//md5加密
Md5Hash hash=new Md5Hash("123");
System.out.println(hash.toString());
//md5加盐
hash=new Md5Hash("123","wit");
System.out.println(hash.toString());
//md5加盐 散列两次,散列次数越多,密码越安全
hash=new Md5Hash("123","wit",2);
System.out.println(hash.toString());
hash=new Md5Hash("456","wit",2);
System.out.println(hash.toString());
md5를 사용하려면 먼저 데이터베이스의 암호가 암호화되었는지 확인해야합니다 (도구 사용).
커스텀 영역
데이터베이스에서 데이터 사용자 ( id, username, password, password_salt ) 개체를 가져온 후 다음 코드 줄이 shiro에 전달되고 shiro는 필요한 데이터가 암호화되었음을 알고 해시 수와 같은 일부 매개 변수가 있습니다. , 암호화 방법 등. 영역을 구성해야합니다.
SimpleAuthenticationInfo(username,password,ByteSource.Util.bytes(salt),getName());
package cn.wit.realm;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import cn.wit.users.Users;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class UserRealm extends AuthorizingRealm{
@Override
public String getName() {
// TODO Auto-generated method stub
return "uesrRealm";
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken taken) throws AuthenticationException {
String username=(String) taken.getPrincipal();
Users users=null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
ComboPooledDataSource cpds=null;
try {
//c3p0获取数据库连接conn
cpds= new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/login");
cpds.setUser("root");
cpds.setPassword("wityy");
conn = cpds.getConnection();
String sql="select *from users where username=?";
ps= conn.prepareStatement(sql);
ps.setObject(1,username);
rs=ps.executeQuery();
while(rs.next()){
users=new Users();
users.setId(rs.getInt("id"));
users.setUsername(rs.getString("username"));
users.setPassword(rs.getString("password"));
users.setPassword_salt(rs.getString("password_salt"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
username=users.getUsername();
String password=users.getPassword();
String salt=users.getPassword_salt();
System.out.println(username);
System.out.println(password);
System.out.println(salt);
//从数据库中拿到的密码是经过加密的(并且加了盐),所以数据库中才设计盐这个属性,想要解密不仅要拿到密码,还要从数据库拿到盐
//除此之外,需要设置realm 告知shiro需要用md5的方式进行解密
SimpleAuthenticationInfo info=new
SimpleAuthenticationInfo(username,password,ByteSource.Util.bytes(salt),getName());
return info;
}
}
이
hashAlgorithmName은 암호화 방법을 나타내며 hashIterations는 해시 수를 나타내며 credentialsMatcher는 암호화를 설정하고이를 영역에 할당하는 클래스입니다. 암호화 된 소스 코드 추적 기사의 끝에 소개가 있습니다.
[main]
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2
userRealm=cn.wit.realm.UserRealm
userRealm.credentialsMatcher=$credentialsMatcher
securityManager.realm=$userRealm
md5 소스 코드 추적
로그인의 소스 코드, 주제 구현 클래스 DelegatingSubject를 입력하고 자체 및 가져온 SecurityManager, 구현 클래스는 DefaultSecurityManager,이 유형의 로그인 메소드는 Authenticator의 Authenticator 메소드를 호출하고, Authenticator를 입력하고, 구현 클래스는 ModularRealmAuthenticator이며, setRealms, setAuthenticationStrategy가 있습니다. 인증 전략 및 영역을 설정하고 Realm 인터페이스를 입력합니다. 구현 클래스 AuthenticatingRealm에는 암호화를 의미하는 credentialsMatcher 속성이
있습니다. 클래스 수준보기. 아래에는 다양한 복호화 방법에 대한 구현 클래스가 있습니다.
이러한 암호화 방법은 아래이 클래스의 설정 방법 인 HashedCredentialsMatcher에서 상속되며, 솔트 정보 보호를 위해 이러한 속성을 ini에 설정하므로 ini를 설정하면 암호화 방법과 해시 수만 설정됩니다. 사용자 정의 영역의 계정 비밀번호로