Shiro自定义Ream
来源:互联网 发布:什么是scratch编程 编辑:程序博客网 时间:2024/05/29 18:25
- 项目目录结构
2.代码:
package com.sun.shiro;import org.apache.commons.lang3.StringUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.realm.Realm;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 自定义realm的实现 * @author suntan * @dade 2017年3月3日下午7:27:42 * @version v1.0 */public class MyRealmDemo1 implements Realm{ /** * 日志 */ private Logger logger = LoggerFactory.getLogger(MyRealmDemo1.class); public String getName() { return "myReamDemo1"; } public boolean supports(AuthenticationToken token) { return token instanceof UsernamePasswordToken; } public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //获取用户名和密码 String userName = (String) token.getPrincipal(); //String passWord = (String) token.getCredentials(); String passWord = new String((char[])token.getCredentials()); if(!StringUtils.equals(userName, "sunny")) { logger.info("=======>用户名错误"); throw new UnknownAccountException(); } if(!StringUtils.equals(passWord, "1127")) { logger.info("====>密码错误"); throw new IncorrectCredentialsException(); } //用户名密码都对 return new SimpleAuthenticationInfo(userName, passWord, getName()); }}
3.测试代码
package com.sun.shiro;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class MyRealmDemo1Test { /** * 日志 */ private Logger logger = LoggerFactory.getLogger(MyRealmDemo1Test.class); @Test public void testGetAuthenticationInfo() { //1.加载配置文件 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro_realm.ini"); //2.实例化对象 SecurityManager securityManager = factory.getInstance(); //绑定 SecurityUtils.setSecurityManager(securityManager); //的到当前用户 Subject subject = SecurityUtils.getSubject(); //根据用户名密码登陆 UsernamePasswordToken token = new UsernamePasswordToken("sunny", "1127"); try { logger.info("=======>执行登陆"); subject.login(token); logger.info("=======>登陆成功"); } catch (Exception e) { logger.error("========>登陆失败:" + e); } //登出 subject.logout(); }}
执行结果:
1 0
- Shiro自定义Ream
- apache shiro自定义shiro
- 自定义shiro
- Ream的入门使用
- shiro自定义Realm
- shiro 自定义logout路径
- Shiro自定义Realms
- shiro 自定义认证filter
- Shiro入门-自定义realm
- shiro自定义密码验证
- shiro自定义过滤器
- Shiro自定义Realm
- 自定义Shiro标签Tag
- shiro自定义Realm实现
- Shiro 自定义realm认证
- 自定义两个shiro过滤器
- 【shiro】--- 自定义realm
- shiro自定义fileter
- eclipse 调式 step into/step over/Drop to Frame
- leetcode53
- 删除记录遇到约束问题
- Google的Python开源Fire工具使用笔记
- 生活本就这样
- Shiro自定义Ream
- 几年来学习的曲折路线
- 运用Autoconf和Automake生产Makefile的学习之路
- LeetCode 64. Minimum Path Sum
- bzoj 4503: 两个串 (FFT+DP)
- JVM源码分析之FinalReference完全解读
- IIC总线、SPI总线、串口232通信、422通信、485通信、TCP/IP协议
- Android Studio 更新失败
- D