- 在BOS项目中应用shiro框架进行认证
第一步:引入shiro框架相关的jar
第二步:在web.xml中配置spring框架提供的用于整合shiro框架的过滤器
启动tomcat服务器,抛出异常:spring工厂中不存在一个名称为“shiroFilter”的bean对象
第三步:在spring配置文件中配置bean,id为shiroFilter
框架提供的过滤器:
第四步:配置安全管理器
第五步:修改UserAction中的login方法,使用shiro提供的方式进行认证操作
第六步:自定义realm,并注入给安全管理器
public class BOSRealm extends AuthorizingRealm{
@Autowired
private IUserDao userDao;
//认证方法
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("realm中的认证方法执行了。。。。");
UsernamePasswordToken mytoken = (UsernamePasswordToken)token;
String username = mytoken.getUsername();
//根据用户名查询数据库中的密码
User user = userDao.findUserByUserName(username);
if(user == null){
//用户名不存在
return null;
}
//如果能查询到,再由框架比对数据库中查询到的密码和页面提交的密码是否一致
AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
return info;
}
//授权方法
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
}
这里注意 login方法先调用SecurityMangager,这里的realm是自动被 SecurityManager 调用的。
这里可以使用encache缓存权限数据,注入对象后就不用再管了。
- 使用ehcache缓存权限数据
ehcache是专门缓存插件,可以缓存Java对象,提高系统性能。
- ehcache提供的jar包:
第一步:在pom.xml文件中引入ehcache的依赖
第二步:在项目中提供ehcache的配置文件
第三步:在spring配置文件中配置缓存管理器对象,并注入给安全管理器对象