自定义Realm解析--------------------------------------->
/*
* Copyright 2005-2013 shopxx.net. All rights reserved.
* Support: http://www.shopxx.net
* License: http://www.shopxx.net/license
*/
package net.shopxx; import java.util.Date;
import java.util.List; import javax.annotation.Resource; import net.shopxx.Setting.AccountLockType;
import net.shopxx.Setting.CaptchaType;
import net.shopxx.entity.Admin;
import net.shopxx.service.AdminService;
import net.shopxx.service.CaptchaService;
import net.shopxx.util.SettingUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; /**
* 权限认证
*
* @author LinkCity Team
* @version 3.0
*/
public class AuthenticationRealm extends AuthorizingRealm { @Resource(name = "captchaServiceImpl")
private CaptchaService captchaService;
@Resource(name = "adminServiceImpl")
private AdminService adminService; /**
* 获取认证信息
*
* @param token
* 令牌
* @return 认证信息
*/
@Override
/*
* 验证当前用户的合法性---也就是登录验证
*/
protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
//获取当前登录令牌(令牌包括的信息有username、password、remember、captcha验证码、登录IP)
AuthenticationToken authenticationToken = (AuthenticationToken) token;
String username = authenticationToken.getUsername();
String password = new String(authenticationToken.getPassword());
String captchaId = authenticationToken.getCaptchaId();
String captcha = authenticationToken.getCaptcha();
String ip = authenticationToken.getHost();
//验证验证码是否正确
if (!captchaService.isValid(CaptchaType.adminLogin, captchaId, captcha)) {
throw new UnsupportedTokenException();
}
if (username != null && password != null) {
//根据当前username获取数据库的admin用户
Admin admin = adminService.findByUsername(username);
//判断是否有此用户
if (admin == null) {
throw new UnknownAccountException();
}
//判断当前用户是否可用
if (!admin.getIsEnabled()) {
throw new DisabledAccountException();
}
Setting setting = SettingUtils.get();
//判断用户是否被锁定
if (admin.getIsLocked()) {
if (ArrayUtils.contains(setting.getAccountLockTypes(), AccountLockType.admin)) {
int loginFailureLockTime = setting.getAccountLockTime();
if (loginFailureLockTime == 0) {
throw new LockedAccountException();
}
Date lockedDate = admin.getLockedDate();
Date unlockDate = DateUtils.addMinutes(lockedDate, loginFailureLockTime);
if (new Date().after(unlockDate)) {
admin.setLoginFailureCount(0);
admin.setIsLocked(false);
admin.setLockedDate(null);
adminService.update(admin);
} else {
throw new LockedAccountException();
}
} else {
admin.setLoginFailureCount(0);
admin.setIsLocked(false);
admin.setLockedDate(null);
adminService.update(admin);
}
}
//判断密码是否正确
if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) {
int loginFailureCount = admin.getLoginFailureCount() + 1;
if (loginFailureCount >= setting.getAccountLockCount()) {
admin.setIsLocked(true);
admin.setLockedDate(new Date());
}
admin.setLoginFailureCount(loginFailureCount);
adminService.update(admin);
throw new IncorrectCredentialsException();
}
admin.setLoginIp(ip);
admin.setLoginDate(new Date());
admin.setLoginFailureCount(0);
//更新用户登录信息
adminService.update(admin);
//返回一个封装了用户信息的AuthenticationInfo实例
return new SimpleAuthenticationInfo(new Principal(admin.getId(), username), password, getName());
}
throw new UnknownAccountException();
} /**
* 获取授权信息
*
* @param principals
* principals
* @return 授权信息
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//获取当前登录用户的信息
Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
if (principal != null) {
//查找当前用户的角色
List<String> authorities = adminService.findAuthorities(principal.getId());
if (authorities != null) {
//获取当前用户的登录令牌
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//为当前用户令牌添加权限集合
authorizationInfo.addStringPermissions(authorities);
return authorizationInfo;
}
}
return null;
} }

自定义Realm解析的更多相关文章

  1. Shrio认证详解+自定义Realm

    Authentication(身份认证)是Shiro权限控制的第一步,用来告诉系统你就是你. 在提交认证的时候,我们需要给系统提交两个信息: Principals:是一个表示用户的唯一属性,可以是用户 ...

  2. Java-Shiro(五):Shiro Realm讲解(二)IniRealm的用法、JdbcRelam的用法、自定义Realm

    引入 上一篇在讲解Realm简介时,介绍过Realm包含大概4类缺省的Realm,本章主要讲解: 1)IniRealm的用法: 2)JdbcRealm基于mysql   默认表及查询语句实现认证.授权 ...

  3. 权限框架 - shiro 自定义realm

    上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...

  4. angularjs directive (自定义标签解析)

    angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...

  5. Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】

    什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...

  6. shiro(二)自定义realm,模拟数据库查询验证

    自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...

  7. shiro自定义Realm

    1.1 自定义Realm 上边的程序使用的是shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. ...

  8. SpringMVC自动封装List对象 —— 自定义参数解析器

    前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...

  9. Shiro入门 - 通过自定义Realm连数数据库进行授权

    shiro-realm.ini [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Spring中的注 ...

随机推荐

  1. 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化

    DbConnection dbc = database.CreateConnection(); DbTransaction dbtt = null; try { dbc.Open(); dbtt = ...

  2. 控件activeX开发之项目ffmpeg解码h264——总结

    1. 编译好ffmpeg的lib库和dll库 2. 播放器作为一个dilog类player,然后在ctrol中的oncreate重写方法中用全局属性cplayer *player里new cplaye ...

  3. c/c++函数指针(3)

    原文地址:http://blog.csdn.net/qingshuiyangfan/article/details/7692647 学习要点: 1,函数地址的一般定义和typedef简化定义;     ...

  4. erlang的简单模拟半包的产生

     gen_tcp:linsten()/2使用的是{packet,2/4/8},则gen_tcp模块在接受或者发送时自动除去包头或者自动加上包头. 本例中使用的是{packet,0}. -module( ...

  5. window设置TortoiseGit连接git不用每次输入用户名和密码

    1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行(快捷键:win+r)”中打开%Home%,然后在目录下新建一个名为“_netrc ...

  6. UIScrollView 的代理方法简单注解

    //减速停止了时执行,手触摸时执行执行 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;    //只要滚动了就会触发 ...

  7. win7 键盘

    请在任务栏的空白处右击,在弹出的选项中选择“工具栏”,再在“Table PC输入面板”选项中打勾,这里任务栏的最右边就会出现一个Table PC 输入面板”的图标

  8. sudo 之后 unable to resolve host的问题解决办法

    gedit /etc/hosts #127.0.0.1 localhost #127.0.0.1 Masterback或者其他 把后面的Masterback 或者其他改成新的主机名,应该是最近修改过主 ...

  9. [mysql] Install/Remove of the Service Denied

    在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld --install 报错: 信息如下: Install/Remove of the Service Deni ...

  10. ios开发之--pop到指定页面

    1 推出到根视图控制器 [self.navigationController popToRootViewControllerAnimated:YES]; 2 推出到指定的视图控制器 for (UIVi ...