this
[main]
userRealm=cn.wit.realm.UserRealm
securityManager.realm=$userRealm
Get the username according to the taken, then query the database with username, return User
object information, assign username and password, SimpleAuthenticationInfo object passes username, password, realm name (the parent class provides the getName method, rewrite this method), and then return this info Object. Indicates that the user information of the database is given to shiro, and shiro will compare the logged-in information (in the taken), if they are consistent, the login is successful
UserRealm extends AuthorizingRealm
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 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();
String pwd="";
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 users=new Users();
users.setId(rs.getInt("id"));
users.setUsername(rs.getString("username"));
users.setPassword(rs.getString("password"));
pwd=users.getPassword();
username=users.getUsername();
}
} 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();
}
}
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,getName());
return info;
}
}
main
package cn.wit.shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
/**
* 完成用户认证功能
* @author Administrator
*
*/
public class Authentication {
public static void main(String[] args) {
//拿到SecurityManager并将它放到环境当中
Factory<SecurityManager>factory=new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//拿到subject接口
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken taken=new UsernamePasswordToken("zhangsan","123");
try {
if(taken!=null){
subject.login(taken);
}
if(subject.isAuthenticated()){
System.out.println("登录成功");
}
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("账号或密码错误");
}catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("账号或密码错误");
}
}
}
Trace source code
From login to SecurityManager, to its implementation class defaultSecurityManager, call authenticator's authenticate method, enter authenticator, its implementation class ModularRealmAuthenticator
has setRealms method, enter Realm interface,
Realm class hierarchy is as follows, which has implemented a variety of realms For example, the previous jdbcRealm is already defined here. If you want to customize the realm, you need to inherit the AuthorizingRealm, and the custom authorization is also inherited from this class
AuthorizingRealm (abstract) is a subclass of
AuthenticatingRealm. There is a doGetAuthenticationInfo method in AuthenticatingRealm (abstract), which can customize authentication
. There is a doGetAuthorizationInfo method in AuthorizingRealm, which allows you to customize authorization.
Inherit the AuthenticatingRealm method, AuthorizingRealm does not override doGetAuthenticationInfo, so the inherited class must override these two methods