1.特点
shiro认证只是对用户名的认证,通过doAuthenticate()方法来验证
2.doAuthenticate方法
此方法是用来获取用户名进行认证的
3.doCredentialsMatch()方法
此方法是获取密码的
自定义realm(数据源),继承AuthorizingRealm 类
(先认证后授权)
其中,
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//获取用户名
String username = (String)authenticationToken.getPrincipal();
//获取密码
//authenticationToken.getCredentials();
Employee employee = employeeService.selectByUserName(username);
if(employee==null){
//如果为空,说明没有此用户,登录失败
return null;
}
//employee 真实名称,用户登录时输入的用户名,由于运行到这里用户输入的用户名为真,所以对象中的用户名就是用户输入的姓名 //employee.getPassword() 真实密码(数据库中对应用户的真实密码)
//this.getName() 数据源名称(Realm对象)
SimpleAuthenticationInfo info= new SimpleAuthenticationInfo(employee,employee.getPassword(),this.getName());
return info;//info中装有所对应用户的所有信息
}
此方法为认证方法
添加依赖
<properties>
<!--版本-->
<shiro.version>1.5.2</shiro.version>
</properties>
<!--依赖-->
<!--shiro 核心-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<!--shiro 的 Web 模块-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<