自定义Realm解析
自定义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解析的更多相关文章
- Shrio认证详解+自定义Realm
Authentication(身份认证)是Shiro权限控制的第一步,用来告诉系统你就是你. 在提交认证的时候,我们需要给系统提交两个信息: Principals:是一个表示用户的唯一属性,可以是用户 ...
- Java-Shiro(五):Shiro Realm讲解(二)IniRealm的用法、JdbcRelam的用法、自定义Realm
引入 上一篇在讲解Realm简介时,介绍过Realm包含大概4类缺省的Realm,本章主要讲解: 1)IniRealm的用法: 2)JdbcRealm基于mysql 默认表及查询语句实现认证.授权 ...
- 权限框架 - shiro 自定义realm
上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...
- angularjs directive (自定义标签解析)
angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...
- Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】
什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...
- shiro(二)自定义realm,模拟数据库查询验证
自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...
- shiro自定义Realm
1.1 自定义Realm 上边的程序使用的是shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. ...
- SpringMVC自动封装List对象 —— 自定义参数解析器
前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...
- Shiro入门 - 通过自定义Realm连数数据库进行授权
shiro-realm.ini [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Spring中的注 ...
随机推荐
- 利用base64展示图片
其实很简单,格式如下: <img src="data:image/jpg;base64,具体的编码值" /> 支持的类型有: data:,文本数据 data:text/ ...
- 网络协议之bt---bt协议详解 DHT篇(下)
-------------------------author:pkf -------------------------------qq:1327706646 ------------------- ...
- MySQL无法远程连接解决方案
1.查看/etc/mysql/my.cnf配置文件是否只允许本地连接 注释配置:#bind-address = 127.0.0.1,重启MySQL Server 2.防火墙(我用的是iptables) ...
- linux常用命令中篇
1.打印当月的日期
- JavaSE(八)之Map总结
上一篇是总结了Collection接口的各种实现类,这一篇我将分享的是Map的总结,希望大家点评! 一.Map接口 1.1.为什么Collection不能满足集合的所有功能? Collection接口 ...
- URAL 1203 Scientific Conference(贪心 || DP)
Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校.补多校的题目.刷一下一直薄弱的DP.多校假设有计算几何一定要干掉-.- 题意:给 ...
- 批量快速的导入导出Oracle的数据(spool缓冲池、java实现)
1. Java代码实现思路 BufferedWriter writefile = new BufferedWriter(new FileWriter(file)); writefile.write( ...
- 【HMM】隐马尔科夫模型
http://www.hankcs.com/nlp/hmm-and-segmentation-tagging-named-entity-recognition.html
- unity如何停止不用字符串方式开启协程的方法
通常我们知道开启协程用StartCoroutine("Method"); 停止协程用StopCoroutine("Method"); 如果我们想要终止所有的协程 ...
- MathType使用中的四个小技巧
MathType是一种比较常见的数学公式编辑器,常常与office搭配着使用,我们在使用的时候有一些要注意的小技巧,下面我们就来给大家介绍介绍MathType使用中的四个小技巧? 技巧一:调整工具栏显 ...