Shiro 笔记 Realm(域)

https://www.w3cschool.cn/shiro/xgj31if4.html

package com.dudu.course1;

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;

/**
 * 单 Realm (域)的实现
 * @author hp
 *
 */
public class ShiroTest1Realm implements Realm{
	
	//返回一个唯一的Realm名字
	public String getName() {
		return "shiroTest1Realm";
	}
	
	//判断此Realm是否支持此Token
	public boolean supports(AuthenticationToken token) {
		//仅支持UsernamePasswordToken类型的Token  用户名密码认证
		return token instanceof UsernamePasswordToken;
	}
	
	//根据Token获取认证信息
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String userName = (String) token.getPrincipal();
		String password = new String((char[])token.getCredentials());
		if (!"zhang".equals(userName)) {
			throw new UnknownAccountException();  //如果用户名错误
		}
		if (!"123".equals(password)) {
			throw new IncorrectCredentialsException(); //如果密码错误
		}
		return new SimpleAuthenticationInfo(userName,password, getName());
	}

}

猜你喜欢

转载自blog.csdn.net/qq_37538698/article/details/84026316