首页 > 其他 > 详细

shiro-02

时间:2018-11-03 14:32:34      阅读:137      评论:0      收藏:0      [点我收藏+]

MyRealm.java

package cn.mldn.realm;

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;

public class MyRealm implements Realm {

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String)token.getPrincipal() ;   // 取得用户名
        String password = new String( (char [])token.getCredentials()) ; //取得密码
        // 此时直接使用固定的用户名密码进行验证处理操作
        if(!"admin".equals(username)) {
            throw new UnknownAccountException("用户不存在");
        }
        if(!"hello".equals(password)) {
            throw new IncorrectCredentialsException("密码不正确");
        }
        return new SimpleAuthenticationInfo(username, password, this.getName());
    }

    @Override
    public String getName() {
        return "MyRealm";    // 名字随便给一个,只要能唯一标记即可
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        // 本次将在之前的程序基础之上继续使用UsernamePasswordToken完成信息的传递
        return token instanceof UsernamePasswordToken;
    }

}

shiro.ini

myRealm=cn.mldn.realm.MyRealm
# 整个Shiro的验证处理都是由SecurityManager接口负责的
securityManager.realms=$myRealm

 

shiro-02

原文:https://www.cnblogs.com/blog-747674599/p/9900676.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!