自定义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. mpeg压缩输入格式---打包模式和平面模式

    版本 v1.0,存在内存问题在 void v4l2_process_image(struct buffer buf)中对 v4l2 采集来的一帧进行处理,存在 struct buffer buf 中b ...

  2. asp.net 后端验证

    using EntryRegistration.Filters; using EntryRegistration.Models.Entity; using System; using System.C ...

  3. [mysql] 查询前几条记录

    From: http://www.cnblogs.com/xuxm2007/archive/2010/11/16/1878211.html SELECT   *   FROM   table   LI ...

  4. C++ string(转)

    C++中string是标准库中一种容器,相当于保存元素类型为char的vector容器(自己理解),这个类提供了相当丰富的函数来完成对字符串操作,以及与C风格字符串之间转换,下面是对string一些总 ...

  5. oracle当前月添加一列显示前几个月的累计值

    create table test_leiji(rpt_month_id number(8),                        current_month NUMBER(12,2)); ...

  6. MathType编辑物理单位的方法

    在用MathType编辑物理公式时,由于物理单位很多都是复合单位,所以在编辑时如果能够有这种复合单位直接使用的话,编辑效率就会大大提高.实际上这种想法在MathType中是可行的,MathType中也 ...

  7. 原生js获取元素样式

    摘要: 我们在开发过程中经常会遇到通过js获取或者改变DOM元素的样式,方法有很多,比如:通过更改DOM元素的class.现在我们讨论原生js来获取DOM元素的CSS样式,注意是获取不是设置 在开始之 ...

  8. Python 爬虫知识点 - 淘宝商品检索结果抓包分析

    一.抓包基础 在淘宝上搜索“Python机器学习”之后,试图抓取书名.作者.图片.价格.地址.出版社.书店等信息,查看源码发现html-body中没有这些信息,分析脚本发现,数据存储在了g_page_ ...

  9. linux中的目录和文件的统计

    ls 目录 | wc -l find  ./   -type   d   |   wc   -l      //查找目录个数 find   ./   -type   f   |   wc   -l   ...

  10. Ehcache整合spring

    下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了. 简单的使用真的很简单.但只能做为入门级了. 1.ehcache.xml,可放classpat ...